tur/typeclass-bifunctor
stdlib/typeclass-bifunctor.tur
minimal Bifunctor typeclass stub.
Auto-loaded (and loadable directly) so a module can declare (definstance Bifunctor [...] ...) without importing the full typeclass.tur. The full typeclass.tur (with every typeclass) remains available on demand via (load "stdlib/typeclass.tur"); defclass redefinition is idempotent, so the two coexist.
Since: stdlib-hkt-consolidation T1
defclass
Bifunctor
(defclass Bifunctor [^^f])
map over both type arguments of a two-parameter type at once.
Type parameters
| ^^f | the type constructor (kind * -> * -> *), e.g. Result |
Methods
(bimap [container :(f a b) fn-left :(fn [a] c) fn-right :(fn [b] d)] :(f c d))-
containerwithfn-leftapplied on one side andfn-righton the other. Which side is "left" is the instance's: Result's follows Haskell's Either, sofn-leftmaps an err andfn-rightan ok.
Laws
(bimap c (fn [x] x) (fn [y] y)) = c
Example
(ok-val (bimap (:: (ok 20) (Result int int)) (fn [e : int] : int (- 0 e)) (fn [x : int] : int (+ x 1)))) ; => 21 (err-val (bimap (:: (err 5) (Result int int)) (fn [e : int] : int (- 0 e)) (fn [x : int] : int (+ x 1)))) ; => -5
Instances
Since: Phase HKT H5