Realm v0.1.0 Realm.Apply.Algebra View Source

Link to this section Summary

Functions

Reverse arguments and sequencing of convey/2. Conceptually this makes operations happen in a different order than convey/2, with the left-side arguments (functions) being run on all right-side arguments, in that order. We're altering the sequencing of function applications.

Sequence actions, replacing the last argument with the first argument's values This is essentially a sequence of actions forgetting the second argument

Alias for convey/2. Why "hose"?

Extends Functor.map/2 to apply arguments to a binary function

Extends lift to apply arguments to a ternary function

Extends lift to apply arguments to a quaternary function

Extends over to apply arguments to a binary function

Extends over to apply arguments to a ternary function

Extends over to apply arguments to a ternary function

Sequence actions, replacing the first/previous values with the last argument This is essentially a sequence of actions forgetting the first argument

Link to this section Functions

Reverse arguments and sequencing of convey/2. Conceptually this makes operations happen in a different order than convey/2, with the left-side arguments (functions) being run on all right-side arguments, in that order. We're altering the sequencing of function applications.

Examples

iex> import Realm.Apply.Algebra
...> ap([fn x -> x + 1 end, fn y -> y * 10 end], [1, 2, 3])
[2, 3, 4, 10, 20, 30]
# For comparison
iex> Apply.convey([1, 2, 3], [fn x -> x + 1 end, fn y -> y * 10 end])
[2, 10, 3, 20, 4, 30]
iex> [100, 200]
...> |> Realm.Functor.map(curry(fn)(x, y, z) -> x * y / z end)
...> |> ap([5, 2])
...> |> ap([100, 50])
[5.0, 10.0, 2.0, 4.0, 10.0, 20.0, 4.0, 8.0]
# ↓                          ↓
# 100 * 5 / 100          200 * 5 / 50

Sequence actions, replacing the last argument with the first argument's values This is essentially a sequence of actions forgetting the second argument

Examples

iex> import Realm.Apply.Algebra
...> [1, 2, 3]
...> |> following([3, 4, 5])
...> |> following([5, 6, 7])
[
  1, 1, 1, 1, 1, 1, 1, 1, 1,
  2, 2, 2, 2, 2, 2, 2, 2, 2,
  3, 3, 3, 3, 3, 3, 3, 3, 3
]
iex> import Realm.Apply.Algebra
...> {1, 2, 3} |> following({4, 5, 6}) |> following({7, 8, 9})
{12, 15, 3}

Alias for convey/2. Why "hose"?

  • Pipes (|>) are application with arguments flipped
  • ap/2 is like function application "in a context"
  • The opposite of ap is a contextual pipe
  • hoses are a kind of flexible pipe Q.E.D.

Examples

iex> [1, 2, 3]
...> |> hose([fn x -> x + 1 end, fn y -> y * 10 end])
[2, 10, 3, 20, 4, 30]

Extends Functor.map/2 to apply arguments to a binary function

Examples

iex> import Realm.Apply.Algebra
...> lift([1, 2], [3, 4], &+/2)
[4, 5, 5, 6]
iex> import Realm.Apply.Algebra
...> [1, 2]
...> |> lift([3, 4], &*/2)
[3, 6, 4, 8]

Extends lift to apply arguments to a ternary function

Examples

iex> import Realm.Apply.Algebra
...> lift([1, 2], [3, 4], [5, 6], fn(a, b, c) -> a * b - c end)
[-2, -3, 1, 0, -1, -2, 3, 2]

Extends lift to apply arguments to a quaternary function

Examples

iex> import Realm.Apply.Algebra
...> lift([1, 2], [3, 4], [5, 6], [7, 8], fn(a, b, c, d) -> a * b - c + d end)
[5, 6, 4, 5, 8, 9, 7, 8, 6, 7, 5, 6, 10, 11, 9, 10]

Extends over to apply arguments to a binary function

Examples

iex> over(&+/2, [1, 2], [3, 4])
[4, 5, 5, 6]
iex> (&*/2)
...> |> over([1, 2], [3, 4])
[3, 4, 6, 8]

Extends over to apply arguments to a ternary function

Examples

iex> import Realm.Apply.Algebra
...> fn(a, b, c) -> a * b - c end
...> |> over([1, 2], [3, 4], [5, 6])
[-2, -3, -1, -2, 1, 0, 3, 2]

Extends over to apply arguments to a ternary function

Examples

iex> import Realm.Apply.Algebra
...> fn(a, b, c) -> a * b - c end
...> |> over([1, 2], [3, 4], [5, 6])
[-2, -3, -1, -2, 1, 0, 3, 2]

Sequence actions, replacing the first/previous values with the last argument This is essentially a sequence of actions forgetting the first argument

Examples

iex> import Realm.Apply.Algebra
...> [1, 2, 3]
...> |> then([4, 5, 6])
...> |> then([7, 8, 9])
[
  7, 8, 9,
  7, 8, 9,
  7, 8, 9,
  7, 8, 9,
  7, 8, 9,
  7, 8, 9,
  7, 8, 9,
  7, 8, 9,
  7, 8, 9
]
iex> import Realm.Apply.Algebra
...> {1, 2, 3} |> then({4, 5, 6}) |> then({7, 8, 9})
{12, 15, 9}