forked from pchiusano/scalaz7-experimental
/
AnyVal.scala
359 lines (262 loc) · 9.4 KB
/
AnyVal.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package scalaz
package std
import scalaz._
trait AnyValInstances {
implicit object unitInstance extends Monoid[Unit] with Order[Unit] with Show[Unit] {
def show(f: Unit): List[Char] = ().toString.toList
def append(f1: Unit, f2: => Unit): Unit = ()
def zero: Unit = ()
def order(x: Unit, y: Unit): Ordering = Ordering.EQ
}
implicit object booleanInstance extends Order[Boolean] with Show[Boolean] {
def show(f: Boolean): List[Char] = f.toString.toList
def order(x: Boolean, y: Boolean): Ordering = if (x < y) Ordering.LT else if (x == y) Ordering.EQ else Ordering.GT
object conjunction extends Monoid[Boolean] {
def append(f1: Boolean, f2: => Boolean): Boolean = f1 && f2
def zero: Boolean = true
}
object disjunction extends Monoid[Boolean] {
def append(f1: Boolean, f2: => Boolean): Boolean = f1 || f2
def zero: Boolean = false
}
}
sealed trait Disjunction
sealed trait Conjunction
implicit object booleanDisjunctionNewTypeInstance extends Monoid[Boolean @@ Disjunction] {
def append(f1: Boolean @@ Disjunction, f2: => Boolean @@ Disjunction) = Tag(f1 || f2)
def zero: Boolean @@ Disjunction = Tag(false)
}
implicit object booleanConjunctionNewTypeInstance extends Monoid[Boolean @@ Conjunction] {
def append(f1: Boolean @@ Conjunction, f2: => Boolean @@ Conjunction) = Tag(f1 && f2)
def zero: Boolean @@ Conjunction = Tag(true)
}
implicit object byteInstance extends Monoid[Byte] with Order[Byte] with Show[Byte] {
def show(f: Byte): List[Char] = f.toString.toList
def append(f1: Byte, f2: => Byte): Byte = (f1 + f2).toByte
def zero: Byte = 0
def order(x: Byte, y: Byte): Ordering = if (x < y) Ordering.LT else if (x == y) Ordering.EQ else Ordering.GT
object multiplication extends Monoid[Byte] {
def append(f1: Byte, f2: => Byte): Byte = (f1 * f2).toByte
def zero: Byte = 1
}
}
import Tags.{Multiplication}
implicit object byteMultiplicationNewType extends Monoid[Byte @@ Multiplication] {
def append(f1: Byte @@ Multiplication, f2: => Byte @@ Multiplication): Byte @@ Multiplication = Tag((f1 * f2).toByte)
def zero: Byte @@ Multiplication = Tag(1)
}
implicit object char extends Monoid[Char] with Order[Char] with Show[Char] {
def show(f: Char): List[Char] = f.toString.toList
def append(f1: Char, f2: => Char): Char = (f1 + f2).toChar
def zero: Char = 0
def order(x: Char, y: Char): Ordering = if (x < y) Ordering.LT else if (x == y) Ordering.EQ else Ordering.GT
object multiplication extends Monoid[Char] {
def append(f1: Char, f2: => Char): Char = (f1 * f2).toChar
def zero: Char = 1
}
}
implicit object charMultiplicationNewType extends Monoid[Char @@ Multiplication] {
def append(f1: Char @@ Multiplication, f2: => Char @@ Multiplication): Char @@ Multiplication = Tag((f1 * f2).toChar)
def zero: Char @@ Multiplication = Tag(1)
}
implicit object shortInstance extends Monoid[Short] with Order[Short] with Show[Short] {
def show(f: Short): List[Char] = f.toString.toList
def append(f1: Short, f2: => Short): Short = (f1 + f2).toShort
def zero: Short = 0
def order(x: Short, y: Short): Ordering = if (x < y) Ordering.LT else if (x == y) Ordering.EQ else Ordering.GT
object multiplication extends Monoid[Short] {
def append(f1: Short, f2: => Short): Short = (f1 * f2).toShort
def zero: Short = 1
}
}
implicit object shortMultiplicationNewType extends Monoid[Short @@ Multiplication] {
def append(f1: Short @@ Multiplication, f2: => Short @@ Multiplication): Short @@ Multiplication = Tag((f1 * f2).toShort)
def zero: Short @@ Multiplication = Tag(1)
}
implicit object intInstance extends Monoid[Int] with Order[Int] with Show[Int] {
def show(f: Int): List[Char] = f.toString.toList
def append(f1: Int, f2: => Int): Int = f1 + f2
def zero: Int = 0
def order(x: Int, y: Int): Ordering = if (x < y) Ordering.LT else if (x == y) Ordering.EQ else Ordering.GT
object multiplication extends Monoid[Int] {
def append(f1: Int, f2: => Int): Int = f1 * f2
def zero: Int = 1
}
}
implicit object intMultiplicationNewType extends Monoid[Int @@ Multiplication] {
def append(f1: Int @@ Multiplication, f2: => Int @@ Multiplication): Int @@ Multiplication = Tag(f1 * f2)
def zero: Int @@ Multiplication = Tag(1)
}
implicit object longInstance extends Monoid[Long] with Order[Long] with Show[Long] {
def show(f: Long): List[Char] = f.toString.toList
def append(f1: Long, f2: => Long): Long = f1 + f2
def zero: Long = 0L
def order(x: Long, y: Long): Ordering = if (x < y) Ordering.LT else if (x == y) Ordering.EQ else Ordering.GT
object multiplication extends Monoid[Long] {
def append(f1: Long, f2: => Long): Long = f1 * f2
def zero: Long = 1
}
}
implicit object longMultiplicationNewType extends Monoid[Long @@ Multiplication] {
def append(f1: Long @@ Multiplication, f2: => Long @@ Multiplication): Long @@ Multiplication = Tag(f1 * f2)
def zero: Long @@ Multiplication = Tag(1)
}
implicit object floatInstance extends Monoid[Float] with Order[Float] with Show[Float] {
def show(f: Float): List[Char] = f.toString.toList
def append(f1: Float, f2: => Float): Float = f1 + f2
def zero: Float = 0f
def order(x: Float, y: Float): Ordering = if (x < y) Ordering.LT else if (x == y) Ordering.EQ else Ordering.GT
}
implicit object doubleInstance extends Monoid[Double] with Order[Double] with Show[Double] {
def show(f: Double): List[Char] = f.toString.toList
def append(f1: Double, f2: => Double): Double = f1 + f2
def zero: Double = 0d
def order(x: Double, y: Double): Ordering = if (x < y) Ordering.LT else if (x == y) Ordering.EQ else Ordering.GT
}
}
trait BooleanFunctions {
/**
* Conjunction. (AND)
*
* <pre>
* p q p ∧ q
* 0 0 0
* 0 1 0
* 1 0 0
* 1 1 1
* </pre>
*/
final def conjunction(p: Boolean, q: => Boolean) = p && q
/**
* Disjunction. (OR)
*
* <pre>
* p q p ∨ q
* 0 0 0
* 0 1 1
* 1 0 1
* 1 1 1
* </pre>
*/
final def disjunction(p: Boolean, q: => Boolean) = p || q
/**
* Negation of Conjunction. (NOR)
*
* <pre>
* p q p !&& q
* 0 0 1
* 0 1 1
* 1 0 1
* 1 1 0
* </pre>
*/
final def nor(p: Boolean, q: => Boolean) = !p || !q
/**
* Negation of Disjunction. (NAND)
*
* <pre>
* p q p !|| q
* 0 0 1
* 0 1 0
* 1 0 0
* 1 1 0
* </pre>
*/
final def nand(p: Boolean, q: => Boolean) = !p && !q
/**
* Conditional.
*
* <pre>
* p q p --> q
* 0 0 1
* 0 1 1
* 1 0 0
* 1 1 1
* </pre>
*/
final def conditional(p: Boolean, q: => Boolean) = !p || q
/**
* Inverse Conditional.
*
* <pre>
* p q p <-- q
* 0 0 1
* 0 1 0
* 1 0 1
* 1 1 1
* </pre>
*/
final def inverseConditional(p: Boolean, q: => Boolean) = p || !q
/**
* Negational of Conditional.
*
* <pre>
* p q p ⇏ q
* 0 0 0
* 0 1 0
* 1 0 1
* 1 1 0
* </pre>
*/
final def negConditional(p: Boolean, q: => Boolean) = p && !q
/**
* Negation of Inverse Conditional.
*
* <pre>
* p q p <\- q
* 0 0 0
* 0 1 1
* 1 0 0
* 1 1 0
* </pre>
*/
final def negInverseConditional(p: Boolean, q: => Boolean) = !p && q
/**
* Executes the given side-effect if `cond` is <code>false</code>.
*/
final def unless(cond: Boolean)(f: => Unit) = if (!cond) f
/**
* Executes the given side-effect if `cond` is <code>true</code>.
*/
final def when(cond: Boolean)(f: => Unit) = if (cond) f
/**
* @return `t` if `cond` is `true`, `f` otherwise
*/
final def fold[A](cond: Boolean, t: => A, f: => A): A = if (cond) t else f
/**
* Returns the given argument in <code>Some</code> if `cond` is `true`, `None` otherwise.
*/
final def option[A](cond: Boolean, a: => A): Option[A] = if (cond) Some(a) else None
/**
* Returns the given argument if `cond` is `true`, otherwise, the zero element for the type of the given
* argument.
*/
final def valueOrZero[A](cond: Boolean)(value: => A)(implicit z: Monoid[A]): A = if (cond) value else z.zero
/**
* Returns the given argument if `cond` is `false`, otherwise, the zero element for the type of the given
* argument.
*/
final def zeroOrValue[A](cond: Boolean)(value: => A)(implicit z: Monoid[A]): A = if (!cond) value else z.zero
/**
* Returns the value `a` lifted into the context `M` if `cond` is `true`, otherwise, the empty value
* for `M`.
*/
final def pointOrEmpty[M[_], A](cond: Boolean)(a: => A)(implicit M: Pointed[M], M0: Empty[M]): M[A] =
if (cond) M.point(a) else M0.empty
/**
* Returns the value `a` lifted into the context `M` if `cond` is `false`, otherwise, the empty value
* for `M`.
*/
final def emptyOrPure[M[_], A](cond: Boolean)(a: => A)(implicit M: Pointed[M], M0: Empty[M]): M[A] =
if (!cond) M.point(a) else M0.empty
final def pointOrEmptyNT[M[_]](cond: Boolean)(implicit M: Pointed[M], M0: Empty[M]): (Id ~> M) =
new (Id ~> M) {
def apply[A](a: A): M[A] = pointOrEmpty(cond)(a)
}
final def emptyOrPureNT[M[_]](cond: Boolean)(implicit M: Pointed[M], M0: Empty[M]): (Id ~> M) =
new (Id ~> M) {
def apply[A](a: A): M[A] = emptyOrPure(cond)(a)
}
}
object anyVal extends AnyValInstances
object boolean extends BooleanFunctions