forked from markhibberd/introduction-to-fp-in-scala
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Applicative.scala
185 lines (167 loc) · 5.31 KB
/
Applicative.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package intro
trait Applicative[F[_]] {
def point[A](a: => A): F[A]
def ap[A, B](a: F[A])(f: F[A => B]): F[B]
}
object Applicative {
/**
* Convenience for summoning an Applicative instance.
*
* usage: Applicative[Int].pure(1)
*/
def apply[F[_]: Applicative]: Applicative[F] =
implicitly[Applicative[F]]
/**
* Applies a function on the Id applicative.
*
* scala> Applicative[Id].ap(Id(1))(Id((i: Int) => i + 1))
* resX: Id[Int] = Id(2)
*/
implicit def IdApplicative: Applicative[Id] =
new Applicative[Id] {
def point[A](a: => A): Id[A] =
???
def ap[A, B](a: Id[A])(f: Id[A => B]): Id[B] =
???
}
/**
* Applies a function on an Optional.
*
* scala> Applicative[Option].ap(Some(1))(Some((i: Int) => i + 1))
* resX: Option[Int] = Some(2)
*/
implicit def OptionApplicative: Applicative[Option] =
new Applicative[Option] {
def point[A](a: => A): Option[A] =
???
def ap[A, B](a: Option[A])(f: Option[A => B]): Option[B] =
???
}
/**
* Applies a function on a List.
*
* scala> Applicative[List].ap(List(1, 2, 3))(List((i: Int) => i + 1))
* resX: List[Int] = List(2, 3, 4)
*
* scala> Applicative[List].ap(List(1, 2, 3))(List((i: Int) => i + 1, (i: Int) => i - 1))
* resX: List[Int] = List(2, 3, 4, 0, 1, 2)
*/
implicit def ListApplicative: Applicative[List] =
new Applicative[List] {
def point[A](a: => A): List[A] =
???
def ap[A, B](a: List[A])(f: List[A => B]): List[B] =
???
}
/**
* Apply a binary function in the environment
*
* scala> Applicative.liftA2(Option(1), Option(2))((i, j) => i + j)
* resX: Option[Int] = Some(2)
*
* scala> Applicative.liftA2(Option(1), None)((i, j) => i + j)
* resX: Option[Int] = None
*/
def liftA2[F[_]: Applicative, A, B, C](a: F[A], b: F[B])(f: (A, B) => C): F[C] =
???
/**
* Apply a ternary function in the environment
*
* scala> Applicative.liftA2(Option(1), Option(2), Option(3))((i, j, k) => i + j + k)
* resX: Option[Int] = Some(6)
*
* scala> Applicative.liftA2(Option(1), None, Option(3))((i, j, k) => i + j + k)
* resX: Option[Int] = None
*/
def liftA3[F[_]: Applicative, A, B, C, D](a: F[A], b: F[B], c: F[C])(f: (A, B, C) => D): F[D] =
???
/**
* Apply a unary function in the environment, also known as `map`
*
* scala> Applicative.liftA1(Option(1))(_ + 1)
* resX: Option[Int] = Some(2)
*
* scala> Applicative.liftA1(Option.empty[Int])(_ + 1)
* resX: Option[Int] = None
*/
def liftA1[F[_]: Applicative, A, B](a: F[A])(f: A => B): F[B] =
???
/**
* Apply, discarding the value of the first argument.
*
* scala> Applicative.apRight(Option(1), Option(2))
* resX: List[Int] = Some(2)
*
* scala> Applicative.apRight(Option(1), None)
* resX: List[Int] = None
*
* scala> Applicative.apRight(List(1, 2), List(3, 4, 5))
* resX: List[List] = List(3, 4, 5, 3, 4, 5)
*/
def apRight[F[_]: Applicative, A, B](a: F[A], b: F[B]): F[B] =
???
/**
* Sequences a list of structures to a structure of list.
*
* scala> Applicative.apLeft(Option(1), Option(2))
* resX: List[Int] = Some(1)
*
* scala> Applicative.apLeft(Option(1), None)
* resX: List[Int] = None
*
* scala> Applicative.apLeft(List(1, 2), List(3, 4, 5))
* resX: List[List] = List(1, 1, 1, 2, 2, 2)
*/
def apLeft[F[_]: Applicative, A, B](a: F[A], b: F[B]): F[B] =
???
/**
* Sequences a list of structures to a structure of list.
*
* scala> Applicative.sequence(List(Option(1), Option(2), Option(3)))
* resX: Option[List[Int]] = Some(List(1, 2, 3))
*
* scala> Applicative.sequence(List(Option(1), None, Option(3)))
* resX: Option[List[Int]] = None
*
* scala> Applicative.sequence(List[Option[Int]())
* resX: Option[List[Int]] = Some(List())
*
* scala> Applicative.sequence(List(List(1, 2, 3), List(1, 2)))
* resX: List[List[Int]] = List(List(1, 1), List(1, 2), List(2, 1), List(2, 2), List(3, 1), List(3, 2))
*/
def sequence[F[_]: Applicative, A](xs: List[F[A]]): F[List[A]] =
???
/**
* Applies a function to a list, preserving the resulting structure
*
* scala> Applicative.sequence(List(1, 2, 3), (i: Int) => Option(i + 1))
* resX: Option[List[Int]] = Some(List(1, 2, 3))
*/
def traverse[F[_]: Applicative, A, B](xs: List[A], f: A => F[B]): F[List[B]] =
???
/**
* Replicate an effect a given number of times.
*
* scala> Applicative.replicateA(3, Option(1))
* resX: Option[List[Int]] = Some(List(1, 1, 1))
*
* scala> Applicative.replicateA(3, Option.empty[Int])
* resX: Option[List[Int]] = None
*
* scala> Applicative.replicateA(2, List(1, 2))
* resX: List[List[Int]] = List(List(1, 1), List(1, 2), List(2, 1), List(2, 2))
*/
def replicateA[F[_]: Applicative, A](n: Int, xs: F[A]): F[List[A]] =
???
/**
* Filter a list with a predicate that produces an effect.
*
* scala> Applicative.filterM(List(1, 2, 3), (a: Int) => if (a < 9) Some(a % 2 != 0) else None)
* resX: Option[List[Int]] = Some(List(1, 3))
*
* scala> Applicative.filterM(List(1, 2, 3), (a: Int) => if (a < 3) Some(a % 2 != 0) else None)
* resX: Option[List[Int]] = None
*/
def filterM[F[_]: Applicative, A](xs: List[A], p: A => F[Boolean]): F[List[A]] =
???
}