-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathFeatures.scala
87 lines (73 loc) · 1.77 KB
/
Features.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package app.impl.scalaz
import org.junit.Test
import scalaz.Functor
import scalaz.Scalaz._
/**
* Created by pabloperezgarcia on 12/03/2017.
*
* Some of good features of ScalaZ
*/
class Features {
@Test
def ternaryOperator(): Unit = {
val boolV = 10 < 5
println(boolV ? "A" | "B")
}
@Test
def validation(): Unit = {
val validation = "6".parseInt
println(validation.isSuccess)
println(validation.isFailure)
println(validation.toOption.get)
}
@Test
def allParis(): Unit = {
val pairList = List(1, 2, 3, 4).allPairs
println(pairList)
}
@Test
def some(): Unit = {
val some = "Some value".some
println(some.get)
}
@Test
def typeClass(): Unit = {
trait Ord[T] {
def compare(a: T, b: T): Boolean
}
implicit object intOrd extends Ord[Int] {
def compare(a: Int, b: Int): Boolean = a <= b
}
def areEquals[T](v1: T, v2: T)(implicit ord: Ord[T]): Boolean = {
ord.compare(v1, v2)
}
println(areEquals[Int](2, 2))
}
/**
* A Functor is something that can be mapped.
* we use this mapper to transform from a value received [A]
* into another [B] using this functor transformer function
*
*/
@Test
def functor(): Unit = {
def addHundred[F[_]](toAdd: F[Int])(implicit mapper: Functor[F]): F[Int] = {
mapper.map(toAdd)(value => value + 100)
}
println(addHundred(10.some).get)
}
@Test
def monoid(): Unit = {
//Concat
val letters = "A" |+| "B"
println(letters)
//Sun
val numbers = 1 |+| 2 |+| 3
println(numbers)
//Merge maps
val m1 = Map(1 -> List("A", "B", "C"), 2 -> List("AA", "BB"))
val m2 = Map(1 -> List("Z"), 2 -> List("CC"), 3 -> List("YYY"))
val mergedMap = m1 |+| m2
println(mergedMap)
}
}