Category Theory abstractions for Clojure
Category Theory abstractions for Clojure
(<=< mg mf x)
Right-to-left composition of monads.
Same as >=>
with its first two arguments flipped.
Right-to-left composition of monads. Same as `>=>` with its first two arguments flipped.
(=<< f mv)
Same as the two argument version of >>=
but with the
arguments interchanged.
Same as the two argument version of `>>=` but with the arguments interchanged.
(>=> mf mg x)
Left-to-right composition of monads.
Left-to-right composition of monads.
(>> mv mv')
(>> mv mv' & mvs)
Performs a Haskell-style left-associative bind, ignoring the values produced by the monad computations.
Performs a Haskell-style left-associative bind, ignoring the values produced by the monad computations.
(>>= mv f)
(>>= mv f & fs)
Performs a Haskell-style left-associative bind.
Let see it in action:
(>>= (just 1) (comp just inc) (comp just inc))
;; => #<Just [3]>
Performs a Haskell-style left-associative bind. Let see it in action: (>>= (just 1) (comp just inc) (comp just inc)) ;; => #<Just [3]>
(bind mv f)
Given a value inside monadic context mv
and any function,
applies a function to value of mv.
(bind (either/right 1) (fn [v]
(return (inc v))))
;; => #<Right [2]>
For convenience, you may prefer use a mlet
macro
that add a beautiful, let like syntax for
compose operations with bind
function.
Given a value inside monadic context `mv` and any function, applies a function to value of mv. (bind (either/right 1) (fn [v] (return (inc v)))) ;; => #<Right [2]> For convenience, you may prefer use a `mlet` macro that add a beautiful, let like syntax for compose operations with `bind` function.
(extract v)
Generic function for unwrap/extract the inner value of a container.
Generic function for unwrap/extract the inner value of a container.
(fapply af & avs)
Given function inside af's context and value inside av's context, applies the function to value and return a result wrapped in context of same type of av context.
This function is variadic, so it can be used like a haskell style left-associative fapply.
Given function inside af's context and value inside av's context, applies the function to value and return a result wrapped in context of same type of av context. This function is variadic, so it can be used like a haskell style left-associative fapply.
(filter p mv)
Applies a predicate to a value in a MonadZero
instance,
returning the identity element when the predicate yields false.
Otherwise, returns the instance unchanged.
(require '[cats.monad.moaybe :as maybe])
(require '[cats.core :as m])
(m/filter (partial < 2) (maybe/just 3))
;=> <Just [3]>
(m/filter (partial < 4) (maybe/just 3))
;=> <Nothing>
Applies a predicate to a value in a `MonadZero` instance, returning the identity element when the predicate yields false. Otherwise, returns the instance unchanged. (require '[cats.monad.moaybe :as maybe]) (require '[cats.core :as m]) (m/filter (partial < 2) (maybe/just 3)) ;=> <Just [3]> (m/filter (partial < 4) (maybe/just 3)) ;=> <Nothing>
(fmap f)
(fmap f fv)
Apply a function f to the value inside functor's fv preserving the context type.
Apply a function f to the value inside functor's fv preserving the context type.
(forseq vs mf)
Same as mapseq but with the arguments in reverse order.
Let se a little example:
(m/forseq [2 3] maybe/just)
;; => <Just [[2 3]]>
Yet an other example that fails:
(m/forseq [1 2]
(fn [v]
(if (odd? v)
(maybe/just v)
(maybe/nothing))))
;; => <Nothing>
Same as mapseq but with the arguments in reverse order. Let se a little example: (m/forseq [2 3] maybe/just) ;; => <Just [[2 3]]> Yet an other example that fails: (m/forseq [1 2] (fn [v] (if (odd? v) (maybe/just v) (maybe/nothing)))) ;; => <Nothing>
(join mv)
Remove one level of monadic structure.
This is same as that (bind mv identity)
Remove one level of monadic structure. This is same as that `(bind mv identity)`
(lift mv)
(lift m mv)
Lift a value from the inner monad of a monad transformer into a value of the monad transformer.
Lift a value from the inner monad of a monad transformer into a value of the monad transformer.
(lift-m n f)
Lifts a function with the given fixed number of arguments to a monadic context.
(def monad+ (lift-m 2 +))
(monad+ (maybe/just 1) (maybe/just 2))
;; => <Just [3]>
(monad+ (maybe/just 1) (maybe/nothing))
;; => <Nothing>
(monad+ [0 2 4] [1 2])
;; => [1 2 3 4 5 6]
Lifts a function with the given fixed number of arguments to a monadic context. (def monad+ (lift-m 2 +)) (monad+ (maybe/just 1) (maybe/just 2)) ;; => <Just [3]> (monad+ (maybe/just 1) (maybe/nothing)) ;; => <Nothing> (monad+ [0 2 4] [1 2]) ;; => [1 2 3 4 5 6]
(mapseq mf coll)
Given a function that takes a value and puts it into a monadic context, map it into the given collection calling sequence on the results.
(require '[cats.monad.maybe :as maybe])
(require '[cats.core :as m])
(m/mapseq maybe/just [2 3])
;=> <Just [[2 3]]>
(m/mapseq (fn [v]
(if (odd? v)
(maybe/just v)
(maybe/nothing)))
[1 2])
;=> <Nothing>
Given a function that takes a value and puts it into a monadic context, map it into the given collection calling sequence on the results. (require '[cats.monad.maybe :as maybe]) (require '[cats.core :as m]) (m/mapseq maybe/just [2 3]) ;=> <Just [[2 3]]> (m/mapseq (fn [v] (if (odd? v) (maybe/just v) (maybe/nothing))) [1 2]) ;=> <Nothing>
(mlet bindings & body)
Monad composition macro that works like clojure let. This allows much easy composition of monadic computations.
Let see one example for understand how it works, this is a code using bind for compose few number of operations:
(bind (just 1)
(fn [a]
(bind (just (inc a))
(fn [b]
(return (* b 2))))))
;=> #<Just [4]>
Now see how this code can be more clear if you are using mlet macro for do it:
(mlet [a (just 1)
b (just (inc a))]
(return (* b 2)))
;=> #<Just [4]>
Monad composition macro that works like clojure let. This allows much easy composition of monadic computations. Let see one example for understand how it works, this is a code using bind for compose few number of operations: (bind (just 1) (fn [a] (bind (just (inc a)) (fn [b] (return (* b 2)))))) ;=> #<Just [4]> Now see how this code can be more clear if you are using mlet macro for do it: (mlet [a (just 1) b (just (inc a))] (return (* b 2))) ;=> #<Just [4]>
(pure v)
(pure ctx v)
Given any value v, return it wrapped in default/effect free context.
This is multiarity function that with arity pure/1
it uses the dynamic scope to resolve the current
context. With pure/2
, you can force a specific context
value.
Example:
(with-monad either/either-monad
(pure 1)
;; => #<Right [1]>
Given any value v, return it wrapped in default/effect free context. This is multiarity function that with arity pure/1 it uses the dynamic scope to resolve the current context. With `pure/2`, you can force a specific context value. Example: (with-monad either/either-monad (pure 1) ;; => #<Right [1]>
(return v)
(return ctx v)
This is a monad version of pure and it works identically to it.
This is a monad version of pure and it works identically to it.
(sequence mvs)
Given a non-empty collection of monadic values, collect their values in a vector returned in the monadic context.
(sequence [(maybe/just 2) (maybe/just 3)])
;; => <Just [[2, 3]]>
(sequence [(maybe/nothing) (maybe/just 3)])
;; => <Nothing>
Given a non-empty collection of monadic values, collect their values in a vector returned in the monadic context. (sequence [(maybe/just 2) (maybe/just 3)]) ;; => <Just [[2, 3]]> (sequence [(maybe/nothing) (maybe/just 3)]) ;; => <Nothing>
(unless b mv)
If the expression is false, returns the monadic value. Otherwise, yields nil in a monadic context.
If the expression is false, returns the monadic value. Otherwise, yields nil in a monadic context.
(when b mv)
(when ctx b mv)
If the expression is true, returns the monadic value. Otherwise, yields nil in a monadic context.
If the expression is true, returns the monadic value. Otherwise, yields nil in a monadic context.
(with-monad ctx & body)
Set current context to specific monad.
Set current context to specific monad.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close