Module: applicative

Methods

(static) ap(f, a) → {Object}

Apply a function within an applicative context to an applicative functor.
Haskell> (<*>) :: f (a -> b) -> f a -> f b

Parameters:
Name Type Description
f function

A function lifted into an applicative context

a Object

An applicative functor

Source:
Returns:

A new applicative functor of the same type, the result of the application

Type
Object
Example
const lst = list(1,2,3);
const p = pure(lst, id);     // lift id function into applicative context
ap(p, lst);                  // => [1:2:3:[]] // proves identity
const f = x => x * 10;
const g = x => x * 3;
const pf = pure(lst, f);
const pg = pure(lst, g);
const p$ = pure(lst, $);
ap(ap(ap(p$)(pf))(pg))(lst); // => [30:60:90:[]] // not pretty
ap(ap(ap(p$, pf), pg), lst); // => [30:60:90:[]] // but
ap(pf, ap(pg, lst));         // => [30:60:90:[]] // proves composition
ap(pf, pure(lst, 10));       // => [100:[]]
pure(lst, f(10));            // => [100:[]] // proves homomorphism
ap(pf, pure(lst, 3));        // => [30:[]]
const a = pure(lst, 3) ;
ap(pf, a);                   // => [30:[]] // proves interchange (not actually possible?)

(static) apFlip(f, a, b) → {Object}

A variant of ap with the arguments reversed.
Haskell> (<**>) :: Applicative f => f a -> f (a -> b) -> f b

Parameters:
Name Type Description
f function

A function lifted into an applicative context

a Object

The first argument to f

b Object

The second argument to f

Source:
Returns:

A new applicative functor of the same type, the result of the application

Type
Object
Example
const lst1 = list(1,2,3);
const lst2 = list(10,10,10);
const f1 = (x, y) => x * y;
const f2 = (x, y) => x + y;
const f3 = (x, y) => x - y;
apFlip(f1, lst1, lst2);      // => [10:10:10:20:20:20:30:30:30:[]]
apFlip(f2, lst1, lst2);      // => [11:11:11:12:12:12:13:13:13:[]]
apFlip(f3, lst1, lst2);      // => [9:9:9:8:8:8:7:7:7:[]]

(static) Applicative() → {boolean}

Applicative functors are functors that support function application within their contexts. They must define pure and ap methods and also be instances of Functor.

Parameters:
Type Description
*

Any object

Source:
Returns:

true if an object is an instance of Applicative and false otherwise

Type
boolean

(static) liftA(f, a) → {Object}

Lift a function into an applicative context.
Haskell> liftA :: Applicative f => (a -> b) -> f a -> f b

Parameters:
Name Type Description
f function

The function to lift into an applicative context

a Object

An applicative functor, the context to lift the function into

Source:
Returns:

The result of applying the lifted function

Type
Object
Example
const lst = list(1,2,3);
const mb = just(1);
const f = x => x * 10;
liftA(f, lst);           // => [10:20:30:[]]
liftA(f, mb);            // => Just 10

(static) liftA2(f, a, b) → {Object}

Lift a binary function to actions.
Haskell> liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c

Parameters:
Name Type Description
f function

The function to lift into an applicative context

a Object

An applicative functor, the first argument to f

b Object

An applicative functor, the second argument to f

Source:
Returns:

The result of applying the lifted function

Type
Object
Example
const mb1 = just(1);
const mb2 = just(10);
const lst1 = list(1,2,3);
const lst2 = list(10,10,10);
const f = (x, y) => {
  const k1_ = (x, y) => x * y;
  return partial(k1_, x, y);
 }
liftA2(f, mb1, mb2);           // => Just 10
liftA2(f, lst1, lst2);         // => [10:10:10:20:20:20:30:30:30:[]]

(static) pure(f, a) → {Object}

Lift a value into an applicative context.
Haskell> pure :: a -> f a

Parameters:
Name Type Description
f Object

An applicative functor

a *

Any object

Source:
Returns:

An applicative functor with the value injected

Type
Object
Example
const lst = list(1,2,3); // => [1:2:3:[]]
pure(lst, 5);            // => [5:[]]

(static) skip(a1, a2) → {Object}

Sequence actions, discarding the value of the second argument.
Haskell> (<*) :: f a -> f b -> f a

Parameters:
Name Type Description
a1 Object

The action to perform

a2 Object

The action to skip

Source:
Returns:

A new applicative functor, the result of sequencing the actions

Type
Object
Example
const l1 = list(1,2,3);
const l2 = list(4,5,6);
skip(l1, l2);           // => [1:1:1:2:2:2:3:3:3:[]]

(static) then(a1, a2) → {Object}

Sequence actions, discarding the value of the first argument.
Haskell> (*>) :: f a -> f b -> f b

Parameters:
Name Type Description
a1 Object

The action to skip

a2 Object

The action to perform

Source:
Returns:

A new applicative functor, the result of sequencing the actions

Type
Object
Example
const l1 = list(1,2,3);
const l2 = list(4,5,6);
then(l1, l2);           // => [4:5:6:4:5:6:4:5:6:[]]