-
Notifications
You must be signed in to change notification settings - Fork 705
/
Either.scala
647 lines (551 loc) · 20.4 KB
/
Either.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
package scalaz
import scala.util.control.NonFatal
import Liskov.<~<
/** Represents a disjunction: a result that is either an `A` or a `B`.
*
* An instance of `A` [[\/]] B is either a [[-\/]]`[A]` (aka a "left") or a [[\/-]]`[B]` (aka a "right").
*
* A common use of a disjunction is to explicitly represent the possibility of failure in a result as opposed to
* throwing an exception. By convention, the left is used for errors and the right is reserved for successes.
* For example, a function that attempts to parse an integer from a string may have a return type of
* `NumberFormatException` [[\/]] `Int`. However, since there is no need to actually throw an exception, the type (`A`)
* chosen for the "left" could be any type representing an error and has no need to actually extend `Exception`.
*
* `A` [[\/]] `B` is isomorphic to `scala.Either[A, B]`, but [[\/]] is right-biased for all Scala versions, so methods
* such as `map` and `flatMap` apply only in the context of the "right" case. This right bias makes [[\/]] more
* convenient to use than `scala.Either` in a monadic context in Scala versions <2.12. Methods such as `swap`,
* `swapped`, and `leftMap` provide functionality that `scala.Either` exposes through left projections.
*
* `A` [[\/]] `B` is also isomorphic to [[Validation]]`[A, B]`. The subtle but important difference is that [[Applicative]]
* instances for [[Validation]] accumulates errors ("lefts") while [[Applicative]] instances for [[\/]] fail fast on the
* first "left" they evaluate. This fail-fast behavior allows [[\/]] to have lawful [[Monad]] instances that are consistent
* with their [[Applicative]] instances, while [[Validation]] cannot.
*/
sealed abstract class \/[A, B] extends Product with Serializable {
final class SwitchingDisjunction[X](r: => X) {
def <<?:(left: X): X =
foldConst(left, r)
}
/** If this disjunction is right, return the given X value, otherwise, return the X value given to the return value. */
def :?>>[X](right: => X): SwitchingDisjunction[X] =
new SwitchingDisjunction[X](right)
/** Return `true` if this disjunction is left. */
def isLeft: Boolean =
this match {
case -\/(_) => true
case \/-(_) => false
}
/** Return `true` if this disjunction is right. */
def isRight: Boolean =
this match {
case -\/(_) => false
case \/-(_) => true
}
/** Catamorphism. Run the first given function if left, otherwise, the second given function. */
def fold[X](l: A => X, r: B => X): X =
this match {
case -\/(a) => l(a)
case \/-(b) => r(b)
}
/** Evaluate `l` and return if left, otherwise, `r`. */
def foldConst[X](l: => X, r: => X): X =
this match {
case -\/(a) => l
case \/-(b) => r
}
/** Spin in tail-position on the right value of this disjunction. */
def loopr[X](left: A => X, right: B => X \/ (A \/ B)): X =
\/.loopRight(this, left, right)
/** Spin in tail-position on the left value of this disjunction. */
def loopl[X](left: A => X \/ (A \/ B), right: B => X): X =
\/.loopLeft(this, left, right)
/** Flip the left/right values in this disjunction. Alias for `unary_~` */
def swap: (B \/ A) =
this match {
case -\/(a) => \/-(a)
case \/-(b) => -\/(b)
}
/** Flip the left/right values in this disjunction. Alias for `swap` */
def unary_~ : (B \/ A) =
swap
/** Run the given function on this swapped value. Alias for `~` */
def swapped[AA, BB](k: (B \/ A) => (BB \/ AA)): (AA \/ BB) =
k(swap).swap
/** Run the given function on this swapped value. Alias for `swapped` */
def ~[AA, BB](k: (B \/ A) => (BB \/ AA)): (AA \/ BB) =
swapped(k)
/** Binary functor map on this disjunction. */
def bimap[C, D](f: A => C, g: B => D): (C \/ D) =
this match {
case -\/(a) => -\/(f(a))
case \/-(b) => \/-(g(b))
}
/** Run the given function on the left value. */
def leftMap[C](f: A => C): (C \/ B) =
this match {
case -\/(a) => -\/(f(a))
case b @ \/-(_) => b.coerceLeft
}
/** Binary functor traverse on this disjunction. */
def bitraverse[F[_]: Functor, C, D](f: A => F[C], g: B => F[D]): F[C \/ D] =
this match {
case -\/(a) => Functor[F].map(f(a))(\/.left)
case \/-(b) => Functor[F].map(g(b))(\/.right)
}
/** Map on the right of this disjunction. */
def map[D](g: B => D): (A \/ D) =
this match {
case \/-(b) => \/-(g(b))
case a @ -\/(_) => a.coerceRight
}
/** Traverse on the right of this disjunction. */
def traverse[F[_]: Applicative, D](g: B => F[D]): F[A \/ D] =
this match {
case a @ -\/(_) => Applicative[F].point(a.coerceRight)
case \/-(b) => Functor[F].map(g(b))(\/.right)
}
/** Run the side-effect on the right of this disjunction. */
def foreach(g: B => Unit): Unit =
fold(_ => (), g)
/** Apply a function in the environment of the right of this disjunction. */
def ap[C](f: => A \/ (B => C)): (A \/ C) =
f flatMap (ff => map(ff(_)))
/** Bind through the right of this disjunction. */
def flatMap[D](g: B => (A \/ D)): (A \/ D) =
this match {
case a @ -\/(_) => a.coerceRight
case \/-(b) => g(b)
}
/** Fold on the right of this disjunction. */
def foldRight[Z](z: => Z)(f: (B, => Z) => Z): Z =
this match {
case -\/(_) => z
case \/-(a) => f(a, z)
}
/** Filter on the right of this disjunction. */
def filter(p: B => Boolean)(implicit M: Monoid[A]): (A \/ B) =
this match {
case -\/(_) => this
case \/-(b) => if(p(b)) this else -\/(M.zero)
}
/** Return `true` if this disjunction is a right value satisfying the given predicate. */
def exists[BB >: B](p: BB => Boolean): Boolean =
this match {
case -\/(_) => false
case \/-(b) => p(b)
}
/** Return `true` if this disjunction is a left value or the right value satisfies the given predicate. */
def forall[BB >: B](p: BB => Boolean): Boolean =
this match {
case -\/(_) => true
case \/-(b) => p(b)
}
/** Return an empty list or list with one element on the right of this disjunction. */
def toList: List[B] =
this match {
case -\/(_) => Nil
case \/-(b) => b :: Nil
}
/** Return an empty list or list with one element on the right of this disjunction. */
def toIList[BB >: B]: IList[BB] =
this match {
case -\/(_) => INil()
case \/-(b) => b :: INil()
}
/** Return an empty LazyList or stream with one element on the right of this disjunction. */
def toLazyList: LazyList[B] =
this match {
case -\/(_) => LazyList.empty[B]
case \/-(b) => LazyList(b)
}
/** Return an empty option or option with one element on the right of this disjunction. Useful to sweep errors under the carpet. */
def toOption: Option[B] =
this match {
case -\/(_) => None
case \/-(b) => Some(b)
}
/** Return an empty maybe or option with one element on the right of this disjunction. Useful to sweep errors under the carpet. */
def toMaybe[BB >: B]: Maybe[BB] =
this match {
case -\/(_) => Maybe.empty
case \/-(b) => Maybe.just(b)
}
/** Convert to a core `scala.Either` at your own peril. */
def toEither: Either[A, B] =
this match {
case -\/(a) => Left(a)
case \/-(b) => Right(b)
}
/** Return the right value of this disjunction or the given default if left. Alias for `|` */
def getOrElse[BB >: B](x: => BB): BB =
this match {
case -\/(_) => x
case \/-(b) => b
}
/** Return the right value of this disjunction or the given default if left. Alias for `getOrElse` */
def |[BB >: B](x: => BB): BB =
getOrElse(x)
/** Return the right value of this disjunction or run the given function on the left. */
def valueOr[BB >: B](x: A => BB): BB =
this match {
case -\/(a) => x(a)
case \/-(b) => b
}
/** Return this if it is a right, otherwise, return the given value. Alias for `|||` */
def orElse[C](x: => C \/ B): C \/ B =
this match {
case -\/(_) => x
case right@ \/-(_) => right.coerceLeft
}
/** Return this if it is a right, otherwise, return the given value. Alias for `orElse` */
def |||[C](x: => C \/ B): C \/ B =
orElse(x)
/**
* Sums up values inside disjunction, if both are left or right. Returns first left otherwise.
* {{{
* \/-(v1) +++ \/-(v2) → \/-(v1 + v2)
* \/-(v1) +++ -\/(v2) → -\/(v2)
* -\/(v1) +++ \/-(v2) → -\/(v1)
* -\/(v1) +++ -\/(v2) → -\/(v1 + v2)
* }}}
*/
def +++(x: => A \/ B)(implicit M1: Semigroup[B], M2: Semigroup[A]): A \/ B =
this match {
case -\/(a1) => x match {
case -\/(a2) => -\/(M2.append(a1, a2))
case \/-(_) => this
}
case \/-(b1) => x match {
case b2 @ -\/(_) => b2
case \/-(b2) => \/-(M1.append(b1, b2))
}
}
/** Ensures that the right value of this disjunction satisfies the given predicate, or returns left with the given value. */
def ensure(onLeft: => A)(f: B => Boolean): (A \/ B) = this match {
case \/-(b) => if (f(b)) this else -\/(onLeft)
case -\/(_) => this
}
/** Run the given function on the left and return right with the result. */
def recover(pf: PartialFunction[A, B]): (A \/ B) = this match {
case -\/(a) if (pf isDefinedAt a) => \/-(pf(a))
case _ => this
}
/** Run the given function on the left and return the result. */
def recoverWith(pf: PartialFunction[A, A \/ B]): (A \/ B) = this match {
case -\/(a) if (pf isDefinedAt a) => pf(a)
case _ => this
}
/** Compare two disjunction values for equality. */
def ===[AA >: A, BB >: B](x: AA \/ BB)(implicit EA: Equal[AA], EB: Equal[BB]): Boolean =
this match {
case -\/(a1) => x match {
case -\/(a2) => Equal[AA].equal(a1, a2)
case \/-(_) => false
}
case \/-(b1) => x match {
case \/-(b2) => Equal[BB].equal(b1, b2)
case -\/(_) => false
}
}
/** Compare two disjunction values for ordering. */
def compare[AA >: A, BB >: B](x: AA \/ BB)(implicit EA: Order[AA], EB: Order[BB]): Ordering =
this match {
case -\/(a1) => x match {
case -\/(a2) => Order[AA].apply(a1, a2)
case \/-(_) => Ordering.LT
}
case \/-(b1) => x match {
case \/-(b2) => Order[BB].apply(b1, b2)
case -\/(_) => Ordering.GT
}
}
import syntax.show._
/** Show for a disjunction value. */
def show[AA >: A, BB >: B](implicit SA: Show[AA], SB: Show[BB]): Cord =
this match {
case -\/(a) => cord"-\\/(${SA.show(a)})"
case \/-(b) => cord"\\/-(${SB.show(b)})"
}
/** Convert to a Validation. */
def toValidation: Validation[A, B] =
this match {
case -\/(a) => Failure(a)
case \/-(b) => Success(b)
}
/** Convert to a ValidationNel. */
def toValidationNel[AA>:A] : ValidationNel[AA,B] =
this match {
case -\/(a) => Failure(NonEmptyList(a))
case \/-(b) => Success(b)
}
/** Run a validation function and back to disjunction again. Alias for `@\?/` */
def validationed[AA, BB](k: Validation[A, B] => Validation[AA, BB]): AA \/ BB =
k(toValidation).toDisjunction
/** Run a validation function and back to disjunction again. Alias for `validationed` */
def @\?/[AA, BB](k: Validation[A, B] => Validation[AA, BB]): AA \/ BB =
validationed(k)
/** Return the value from whichever side of the disjunction is defined, given a commonly assignable type. */
def merge[AA >: A](implicit ev: B <~< AA): AA =
this match {
case -\/(a) => a
case \/-(b) => ev(b)
}
/** Convert to a These. */
def toThese: A \&/ B =
fold(
a => \&/.This(a),
b => \&/.That(b)
)
def orRaiseError[F[_]](implicit F: MonadError[F, A]): F[B] =
fold(
a => F.raiseError(a),
b => F.point(b)
)
}
/** A left disjunction
*
* Often used to represent the failure case of a result
*/
final case class -\/[A, B](a: A) extends (A \/ B) {
def coerceRight[C]: A \/ C = this.asInstanceOf[A \/ C]
}
object -\/ {
/** Override smart constructor to return less specific type. */
def apply[A, B](a: A): A \/ B = new -\/(a)
}
/** A right disjunction
*
* Often used to represent the success case of a result
*/
final case class \/-[A, B](b: B) extends (A \/ B) {
def coerceLeft[C]: C \/ B = this.asInstanceOf[C \/ B]
}
object \/- {
/** Override smart constructor to return less specific type. */
def apply[A, B](b: B): A \/ B = new \/-(b)
}
object \/ extends DisjunctionInstances {
/** Construct a left disjunction value. */
def left[A, B]: A => A \/ B =
-\/(_)
/** Construct a right disjunction value. */
def right[A, B]: B => A \/ B =
\/-(_)
/** Construct a left disjunction value but specify only the `right` type param
*
* @example {{{
* val x = \/.l[String](42)
* x: Int \/ String
* }}}
*/
def l[B]: L[B] =
new L[B]
private[scalaz] final class L[B] private[scalaz] (private val dummy: Boolean = true) extends AnyVal {
def apply[A](left: A): A \/ B = -\/(left)
}
/** Construct a right disjunction value but specify only the `left` type param
*
* @example {{{
* val x = \/.r[String](42)
* x: String \/ Int
* }}}
*/
def r[B]: R[B] =
new R[B]
private[scalaz] final class R[A] private[scalaz] (private val dummy: Boolean = true) extends AnyVal {
def apply[B](right: B): A \/ B = \/-(right)
}
/** Construct a disjunction value from a standard `scala.Either`. */
def fromEither[A, B](e: Either[A, B]): A \/ B =
e.fold(left, right)
/** Construct a disjunction value from a standard `scala.Option`. */
def fromOption[A, B](ifNone: => A)(o: Option[B]): A \/ B =
o.fold(left[A, B](ifNone))(right)
/**
* Wrap a call to a deterministic partial function, making a total function.
* May be used to interface with legacy methods that do not have an FP
* equivalent.
*
* The `err` callback must convert the non-referentially transparent
* `Throwable` (which is anything caught by the `NonFatal` construct) into a
* data type. The caller is trusted not to allow the stack trace to escape
* into the `A` data type.
*
* Note that exceptions are extremely inefficient. Callers should consider
* validating the input to their partial function and exiting early.
*
* If no useful information can be obtained from the `Throwable`, prefer
* [[scalaz.Maybe#attempt]].
*
* For interfacing with non-deterministic blocks of code that may or may not
* throw `Throwable`, use [[scalaz.effect.IO]].
*
* For interfacing with deterministic functions that violate the type system
* by returning `null`, use [[scalaz.Maybe#fromNullable]].
*/
def attempt[A, B](f: => B)(err: Throwable => A): A \/ B =
try \/-(f) catch {
case NonFatal(t) => -\/(err(t))
}
/** Spin in tail-position on the right value of the given disjunction. */
@annotation.tailrec
final def loopRight[A, B, X](d: A \/ B, left: A => X, right: B => X \/ (A \/ B)): X =
d match {
case -\/(a) => left(a)
case \/-(b) => right(b) match {
case -\/(x) => x
case \/-(q) => loopRight(q, left, right)
}
}
/** Spin in tail-position on the left value of the given disjunction. */
@annotation.tailrec
final def loopLeft[A, B, X](d: A \/ B, left: A => X \/ (A \/ B), right: B => X): X =
d match {
case -\/(a) => left(a) match {
case -\/(x) => x
case \/-(q) => loopLeft(q, left, right)
}
case \/-(b) => right(b)
}
}
sealed abstract class DisjunctionInstances extends DisjunctionInstances0 {
implicit def DisjunctionOrder[A: Order, B: Order]: Order[A \/ B] =
new Order[A \/ B] {
def order(a1: A \/ B, a2: A \/ B) =
a1 compare a2
override def equal(a1: A \/ B, a2: A \/ B) =
a1 === a2
}
implicit def DisjunctionMonoid[A: Semigroup, B: Monoid]: Monoid[A \/ B] =
new Monoid[A \/ B] {
def append(a1: A \/ B, a2: => A \/ B) =
a1 +++ a2
def zero =
\/-(Monoid[B].zero)
}
}
sealed abstract class DisjunctionInstances0 extends DisjunctionInstances1 {
implicit def DisjunctionEqual[A: Equal, B: Equal]: Equal[A \/ B] =
new Equal[A \/ B] {
def equal(a1: A \/ B, a2: A \/ B) =
a1 === a2
}
implicit def DisjunctionShow[A: Show, B: Show]: Show[A \/ B] =
Show.show(_.show)
implicit def DisjunctionSemigroup[A: Semigroup, B: Semigroup]: Semigroup[A \/ B] =
new Semigroup[A \/ B] {
def append(a1: A \/ B, a2: => A \/ B) =
a1 +++ a2
}
}
sealed abstract class DisjunctionInstances1 extends DisjunctionInstances2 {
implicit def DisjunctionBand[A: Band, B: Band]: Band[A \/ B] =
new Band[A \/ B] {
def append(a1: A \/ B, a2: => A \/ B) =
a1 +++ a2
}
implicit def DisjunctionInstances1[L]: Traverse[\/[L, *]] & Monad[\/[L, *]] & BindRec[\/[L, *]] & Cozip[\/[L, *]] & Plus[\/[L, *]] & Alt[\/[L, *]] & Optional[\/[L, *]] & MonadError[\/[L, *], L] =
new Traverse[\/[L, *]] with Monad[\/[L, *]] with BindRec[\/[L, *]] with Cozip[\/[L, *]] with Plus[\/[L, *]] with Alt[\/[L, *]] with Optional[\/[L, *]] with MonadError[\/[L, *], L] {
override def map[A, B](fa: L \/ A)(f: A => B) =
fa map f
override def ap[A,B](fa: => L \/ A)(f: => L \/ (A => B)): L \/ B = fa.ap(f)
override def apply2[A, B, C](fa: => L \/ A, fb: => L \/ B)(f: (A, B) => C): L \/ C =
fa match {
case \/-(a) =>
fb match {
case \/-(b) => \/-(f(a, b))
case e => e.asInstanceOf[L \/ C]
}
case e => e.asInstanceOf[L \/ C]
}
@scala.annotation.tailrec
def tailrecM[A, B](a: A)(f: A => L \/ (A \/ B)): L \/ B =
f(a) match {
case l @ -\/(_) => l.coerceRight
case \/-(-\/(a0)) => tailrecM(a0)(f)
case \/-(rb @ \/-(_)) => rb.coerceLeft
}
def bind[A, B](fa: L \/ A)(f: A => L \/ B) =
fa flatMap f
override def emap[A, B](fa: L \/ A)(f: A => L \/ B) = bind(fa)(f)
def point[A](a: => A) =
\/-(a)
def traverseImpl[G[_] : Applicative, A, B](fa: L \/ A)(f: A => G[B]) =
fa.traverse(f)
override def foldRight[A, B](fa: L \/ A, z: => B)(f: (A, => B) => B) =
fa.foldRight(z)(f)
def cozip[A, B](x: L \/ (A \/ B)) =
x match {
case l @ -\/(_) => -\/(l.coerceRight)
case \/-(e) => e match {
case -\/(a) => -\/(\/-(a))
case b @ \/-(_) => \/-(b.coerceLeft)
}
}
def plus[A](a: L \/ A, b: => L \/ A) =
a orElse b
def alt[A](a: => L \/ A, b: => L \/ A) =
plus(a, b)
def pextract[B, A](fa: L \/ A): (L \/ B) \/ A = fa match {
case l@ -\/(_) => -\/(l.coerceRight)
case r@ \/-(_) => r.coerceLeft
}
def raiseError[A](e: L): L \/ A =
-\/(e)
def handleError[A](fa: L \/ A)(f: L => L \/ A): L \/ A = fa match {
case -\/(e) => f(e)
case r => r
}
override def unfoldrOpt[S, A, B](seed: S)(f: S => Maybe[(L \/ A, S)])(implicit r: Reducer[A, B]): Maybe[L \/ B] = {
@annotation.tailrec
def go(acc: B, s1: S): L \/ B = f(s1) match {
case Maybe.Just((ma, s2)) =>
ma match {
case \/-(a) =>
go(r.snoc(acc, a), s2)
case l @ -\/(_) =>
l.coerceRight[B]
}
case _ =>
\/-(acc)
}
f(seed).map {
case (\/-(a), s) =>
go(r.unit(a), s)
case (l @ -\/(_), _) =>
l.coerceRight[B]
}
}
}
}
sealed abstract class DisjunctionInstances2 {
implicit def DisjunctionIsCovariantRight[A]: IsCovariant[\/[A, *]] =
IsCovariant.force[\/[A, *]]
implicit def DisjunctionIsCovariantLeft[A]: IsCovariant[\/[*, A]] =
IsCovariant.force[\/[*, A]]
implicit val DisjunctionInstances2 : Bitraverse[\/] = new Bitraverse[\/] {
override def bimap[A, B, C, D](fab: A \/ B)
(f: A => C, g: B => D) = fab.bimap(f, g)
def bitraverseImpl[G[_] : Applicative, A, B, C, D](fab: A \/ B)
(f: A => G[C], g: B => G[D]) =
fab.bitraverse(f, g)
}
implicit val DisjunctionAssociative: Associative[\/] = new Associative[\/] {
def reassociateLeft[A, B, C](f: \/[A, \/[B, C]]) =
f.fold(
a => -\/(-\/(a)),
_.fold(
b => -\/(\/-(b)),
\/.right
)
)
def reassociateRight[A, B, C](f: \/[\/[A, B], C]) =
f.fold(
_.fold(
\/.left,
b => \/-(-\/(b))
),
c => \/-(\/-(c))
)
}
}