FunLand.Applicative behaviour (fun_land v0.10.0)

Link to this section Summary

Functions

Calls Applicative.apply/2, but afterwards discards the value that was the result of the leftmost argument. (the one evaluated the first).

Calls Applicative.apply/2, but afterwards discards the value that was the result of the rightmost argument. (the one evaluated the last).

Creates a new Algebraic Data Type that contains value. The first parameter can either be the module name of the Algebraic Data Type that you want to create, or it can be an instance of the same data type, such as [] for List, {} for Tuple, %YourModule{} for YourModule.

Callbacks

A structure is Applicative if it is Appliable, as well as having the ability to create a new structure from any value, by newping it.

Link to this section Types

Link to this type

applicative(a)

Specs

applicative(a) :: FunLand.adt(a)

Link to this section Functions

Link to this function

apply_discard_left(a, b)

Calls Applicative.apply/2, but afterwards discards the value that was the result of the leftmost argument. (the one evaluated the first).

So in the end, the value that went in as right argument (The Algorithmic Data Type containing values) is returned.

In Haskell, this is known as *>

Link to this function

apply_discard_right(a, b)

Calls Applicative.apply/2, but afterwards discards the value that was the result of the rightmost argument. (the one evaluated the last).

So in the end, the value that went in as left argument (The Algorithmic Data Type containing partially-applied functions) is returned.

In Haskell, this is known as <*

Link to this function

apply_with(a, b)

See FunLand.Appliable.apply_with/2.

See FunLand.Mappable.map/2.

Link to this function

new(module_or_data_type, value)

Creates a new Algebraic Data Type that contains value. The first parameter can either be the module name of the Algebraic Data Type that you want to create, or it can be an instance of the same data type, such as [] for List, {} for Tuple, %YourModule{} for YourModule.

Link to this section Callbacks

Specs

new(a) :: applicative(a) when a: any()

A structure is Applicative if it is Appliable, as well as having the ability to create a new structure from any value, by newping it.

Being able to create new, apply and map means that we can create new structures with some values, transform them and (partially or fully) apply them to each other.

Therefore, we're able to re-use all new our old operations in a new, more complex context.

Fruit Salad Example

We've already seen that a fruit-salad bowl is Mappable and Appliable.

However, we'd like to know how we start out: When we have an apple, how do we end up with a bowl filled with an apple?

Bowl.new(my_apple) is the implementation that answers this question.

Together with apply and map, we can now take arbitrary ingredients, put them in bowls and mix and mash them together to our liking, without soiling the kitchen's countertop:

  • new: We can take an apple, and put it in a bowl: we put the apple in a new bowl to return a bowl with an apple.
  • apply: If we have a bowl with a partially-made fruit-salad, and we have a bowl with an apple, we can take the apple and the partially-made fruit salad to create a bowl with a fruit-with-apples-salad.
  • map: We can take a bowl with any fruit or salad, and do some arbitrary operation with it, such as 'blending'. In this example, we end up with the same bowl, but now filled with blended fruit-salad.

In Other Environments

  • In Haskell, Applicative.new is known by pure as well as return.
  • In Category Theory, something that is Applicative is know as its more official name Applicative Functor.