A functor with application, providing operations to
Further, any definition must satisfy the following:
The other members have the following default definitions, which may be overridden with equivalent specialized implementations:
As a consequence of these laws, the functor instance for f will satisfy
It may be useful to note that supposing, for all x and y,
it follows from the above that
If f is also a Monad, it should satisfy
Some functors support an implementation of liftA2 that is more efficient than the default one. In particular, if fmap is an expensive operation, it is likely better to use liftA2 than to fmap over the structure and then use <*>.
Eager applicative chains get noisy as they grow because Racket functions must be curried explicitly.
|(require algebraic/data/functor)||package: algebraic|
The functor class is used for types that can be mapped over.
Minimal instance: fmap
Convert from a Maybe Int to a Maybe String using ~a:
> (with-instance maybe-functor (<$> ~a ))
> (with-instance maybe-functor (<$> ~a ( 3)))
Double each element of a list:
> (with-instance list-functor (<$> (>> * 2) '(1 2 3)))
'(2 4 6)
|(require algebraic/control/monad)||package: algebraic|
Basic operations on monads, and a generic do-notation.
Monads and Applicatives should relate as follows:
The above laws imply:
Minimal instance: >>=
(do do-expr ... expr)
(do~ do-expr ... expr)
do-expr = formals <- monad-expr | let id expr | let-values formals expr | monad-expr
do composes a seies of do-exprs with a final expr to determine its result.
do~ is similar, except it expects each do-expr to be warpped in a thunk, and it produces a thunk.
A do-expr has one of the following forms:
formals <- monad-expr
Evaluates the monad-expr and binds the monad’s contents according to formals.Examples:
let id expr
Evaluates the expr and binds the result to id.
> (with-instance ((do~ let `(a . ,b) '(a 1 2 3) (return b))))
'(1 2 3)
let-values formals expr
Evaluates the expr and binds the result according to formals.
Evaluates the monad-expr and discards the result.Examples:
|(require algebraic/control/monoid)||package: algebraic|
The class of monoids (types with an associative binary operation that has an identity).
The method names refer to the monoid of lists under concatenation, but there are many other instances.
Some types can be viewed as a in more than one way, e.g. both addition and multiplication on numbers.
NOTE: This method is redundant and has the default implementation = <>.
For most types, the default definition for will be used, but the function is included in the class definition so that an optimized version can be provided for specific types.
|(require algebraic/data/semigroup)||package: algebraic|
The class of semigroups (types with an associative binary operation).
The default definition should be sufficient, but this can be overridden for efficiency.
Given that this works on a semigroup it is allowed to fail if you request 0 or fewer repetitions, and the default definition wll do so.