Allows you to apply (thus ap) a value to a function without having to take
either out of the context of their Maybes. This does mean that the
transforming function is itself within a Maybe, which can be hard to grok at
first but lets you do some very elegant things. For example, ap allows you
to this:
And this kind of thing comes up quite often once you're using Maybe to
handle optionality throughout your application.
For another example, imagine you need to compare the equality of two
ImmutableJS data structures, where a === comparison won't work. With ap,
that's as simple as this:
importMaybefrom'true-myth/maybe'; import { isasimmutableIs, Set } from'immutable';
In summary: anywhere you have two Maybe instances and need to perform an
operation that uses both of them, ap is your friend.
Two things to note, both regarding currying:
All functions passed to ap must be curried. That is, they must be of the
form (for add) (a: number) => (b: number) => a + b, not the more usual
(a: number, b: number) => a + b you see in JavaScript more generally.
(Unfortunately, these do not currently work with lodash or Ramda's curry
helper functions. A future update to the type definitions may make that
work, but the intermediate types produced by those helpers and the more
general function types expected by this function do not currently align.)
You will need to call ap as many times as there are arguments to the
function you're dealing with. So in the case of this add3 function,
which has the "arity" (function argument count) of 3 (a and b), you'll
need to call ap twice: once for a, and once for b. To see why, let's
look at what the result in each phase is:
One other scenario which doesn't come up quite as often but is conceivable
is where you have something that may or may not actually construct a function
for handling a specific Maybe scenario. In that case, you can wrap the
possibly-present in ap and then wrap the values to apply to the function to
in Maybe themselves.
Aside:ap is not named apply because of the overlap with JavaScript's
existing apply function – and although strictly speaking, there isn't any
direct overlap (Maybe.apply and Function.prototype.apply don't intersect
at all) it's useful to have a different name to avoid implying that they're
the same.
Allows you to apply (thus
ap
) a value to a function without having to take either out of the context of theirMaybe
s. This does mean that the transforming function is itself within aMaybe
, which can be hard to grok at first but lets you do some very elegant things. For example,ap
allows you to this:Without
ap
, you'd need to do something like a nestedmatch
:And this kind of thing comes up quite often once you're using
Maybe
to handle optionality throughout your application.For another example, imagine you need to compare the equality of two ImmutableJS data structures, where a
===
comparison won't work. Withap
, that's as simple as this:Without
ap
, we're back to that gnarly nestedmatch
:In summary: anywhere you have two
Maybe
instances and need to perform an operation that uses both of them,ap
is your friend.Two things to note, both regarding currying:
All functions passed to
ap
must be curried. That is, they must be of the form (for add)(a: number) => (b: number) => a + b
, not the more usual(a: number, b: number) => a + b
you see in JavaScript more generally.(Unfortunately, these do not currently work with lodash or Ramda's
curry
helper functions. A future update to the type definitions may make that work, but the intermediate types produced by those helpers and the more general function types expected by this function do not currently align.)You will need to call
ap
as many times as there are arguments to the function you're dealing with. So in the case of thisadd3
function, which has the "arity" (function argument count) of 3 (a
andb
), you'll need to callap
twice: once fora
, and once forb
. To see why, let's look at what the result in each phase is:So for
toString
, which just takes a single argument, you would only need to callap
once.One other scenario which doesn't come up quite as often but is conceivable is where you have something that may or may not actually construct a function for handling a specific
Maybe
scenario. In that case, you can wrap the possibly-present inap
and then wrap the values to apply to the function to inMaybe
themselves.Aside:
ap
is not namedapply
because of the overlap with JavaScript's existingapply
function – and although strictly speaking, there isn't any direct overlap (Maybe.apply
andFunction.prototype.apply
don't intersect at all) it's useful to have a different name to avoid implying that they're the same.