tur/either
binary sum type Either[L R]: Left l or Right r.
Since: Phase sum-types-either
Either
(defdata Either :copy [L R] (Left) (Right))
left?
(left? [e : int] :)
is this Either a Left?
| e | an Either[L R] handle |
true when e is (Left _), false when (Right _).
(left? (Left 3)) ; => true (left? (Right 3)) ; => false
Since: Phase sum-types-either
right?
(right? [e : int] :)
is this Either a Right?
| e | an Either[L R] handle |
true when e is (Right _), false when (Left _).
(right? (Right 3)) ; => true (right? (Left 3)) ; => false
Since: Phase sum-types-either
from-left
(from-left [dflt : int e : int] :)
extract the Left payload, or a default when e is a Right.
| dflt | value returned when e is a Right | |
| e | an Either[L R] handle |
The L payload when e is (Left l), else dflt.
(from-left 0 (Left 7)) ; => 7 (from-left 0 (Right 7)) ; => 0
Since: Phase sum-types-either
from-right
(from-right [dflt : int e : int] :)
extract the Right payload, or a default when e is a Left.
| dflt | value returned when e is a Left | |
| e | an Either[L R] handle |
The R payload when e is (Right r), else dflt.
(from-right 0 (Right 7)) ; => 7 (from-right 0 (Left 7)) ; => 0
Since: Phase sum-types-either
either
(either [^fat on-left : (fn [int])
eliminate an Either by applying one of two functions.
| on-left | function applied to the Left payload (carried as a fat closure) | |
| on-right | function applied to the Right payload (carried as a fat closure) | |
| e | an Either[L R] handle |
(on-left l) when e is (Left l), else (on-right r).
(either negate id (Left 5)) ; => -5 (either negate id (Right 5)) ; => 5
Since: Phase sum-types-either
either-map
(either-map [^fat f : (fn [int])
map a function over the Right arm (right-biased).
| f | function applied to the Right payload (carried as a fat closure) | |
| e | an Either[L R] handle |
(Right (f r)) when e is (Right r), else e unchanged.
(either-map inc (Right 4)) ; => (Right 5) (either-map inc (Left 4)) ; => (Left 4)
Since: Phase sum-types-either
either-map-left
(either-map-left [^fat f : (fn [int])
map a function over the Left arm.
| f | function applied to the Left payload (carried as a fat closure) | |
| e | an Either[L R] handle |
(Left (f l)) when e is (Left l), else e unchanged.
(either-map-left inc (Left 4)) ; => (Left 5) (either-map-left inc (Right 4)) ; => (Right 4)
Since: Phase sum-types-either
Functor[]
(definstance Functor [])
right-biased Functor for Either: map over Right.
Since: Phase sum-types-either