Witchcraft v1.0.0-beta Witchcraft.Chain View Source
Chain function applications on contained data that may have some additional effect
As a diagram:
%Container<data> --- (data -> %Container<updated_data>) ---> %Container<updated_data>
Examples
iex> chain([1, 2, 3], fn x -> [x, x] end)
[1, 1, 2, 2, 3, 3]
alias Algae.Maybe.{Nothing, Just}
%Just{just: 42} >>> fn x -> %Just{just: x + 1} end
#=> %Just{just: 43}
%Just{just: 42}
>>> fn x -> if x > 50, do: %Just{just: x + 1}, else: %Nothing{} end
>>> fn y -> y * 100 end
#=> %Nothing{}
Type Class
An instance of Witchcraft.Chain
must also implement Witchcraft.Apply
,
and define Witchcraft.Chain.chain/2
.
Functor [map/2]
↓
Apply [ap/2]
↓
Chain [chain/2]
Link to this section Summary
Functions
Operator alias for reverse_chain/2
Operator alias for chain/2
An alias for chain/2
do
notation sugar
Sequentially compose actions, piping values through successive function chains
Compose link functions to create a new link function
Join together one nested level of a data structure that contains itself
Compose link functions to create a new link function
An alias for reverse_chain/2
chain/2
but with the arguments flipped
Link to this section Types
Link to this section Functions
Operator alias for reverse_chain/2
Extends the <~
/ <<~
heirarchy with one more level of power / abstraction
Examples
iex> to_monad = fn x -> (fn _ -> x end) end
...> bound = to_monad.(&(&1 + 10)) <<< to_monad.(&(&1 * 10))
...> bound.(10)
20
In Haskell, this is the famous =<<
operator, but Elixir doesn’t allow that
infix operator.
Operator alias for chain/2
Extends the ~>
/ ~>>
heirarchy with one more level of power / abstraction
Examples
iex> to_monad = fn x -> (fn _ -> x end) end
...> bound = to_monad.(&(&1 * 10)) >>> to_monad.(&(&1 + 10))
...> bound.(10)
20
In Haskell, this is the famous >>=
operator, but Elixir doesn’t allow that
infix operator.
An alias for chain/2
.
Provided as a convenience for those coming from other languages.
do
notation sugar
Sequences chainable actions. Note that each line must be of the same type.
For a version with return
, please see Witchcraft.Monad.do/2
Examples
iex> chain do
...> [1]
...> end
[1]
iex> chain do
...> [1, 2, 3]
...> [4, 5, 6]
...> [7, 8, 9]
...> end
[
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> chain do
...> a <- [1, 2, 3]
...> b <- [4, 5, 6]
...> [a * b]
...> end
[
4, 8, 12,
5, 10, 15,
6, 12, 18
]
Normal functions are fine within the do
as well, as long as each line
ends up being the same chainable type
iex> import Witchcraft.{Functor, Applicative}
...> chain do
...> map([1, 2, 3], fn x -> x + 1 end)
...> of([], 42)
...> [7, 8, 9] ~> fn x -> x * 10 end
...> end
[
70, 80, 90,
70, 80, 90,
70, 80, 90
]
Or with a custom type
alias Algae.Maybe.{Nothing, Just}
chain do
%Just{just: 4}
%Just{just: 5}
%Just{just: 6}
end
#=> %Just{just: 6}
chain do
%Just{just: 4}
%Nothing{}
%Just{just: 6}
end
#=> %Nothing{}
let
bindings
let
s allow you to hold static values inside a do-block, much like normal assignment
iex> chain do
...> let a = 4
...> [a]
...> end
[4]
This is somewhat limited, though, as values drawn from a chianable structure
with <-
are not in scope when desugared. For example, this is not possible
due to the recursive binding on x
:
chain do
x <- [1, 2, 3]
y <- [4, 5, 6]
let will_fail = x + 1
[y * will_fail]
end
Desugaring
Sequencing
The most basic form
chain do
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
end
is equivalent to
[1, 2, 3]
|> then([4, 5, 6])
|> then([7, 8, 9])
<-
(“drawn from”)
Drawing values from within a chainable structure is similar feels similar to assignmet, but it is pulling each value separately in a chain link function.
For instance
chain do
a <- [1, 2, 3]
b <- [4, 5, 6]
[a * b]
end
desugars to this
[1, 2, 3] >>> fn a ->
[4, 5, 6] >>> fn b ->
[a + b]
end
end
but is often much cleaner to read in do-notation, as it cleans up all of the nested functions (especially when the chain is very long).
Sequentially compose actions, piping values through successive function chains.
The applied linking function must be unary and return data in the same type of container as the input. The chain function essentially “unwraps” a contained value, applies a linking function that returns the initial (wrapped) type, and collects them into a flat(ter) structure.
chain/2
is sometimes called “flat map”, since it can also
be expressed as data |> map(link_fun) |> flatten()
.
As a diagram:
%Container<data> --- (data -> %Container<updated_data>) ---> %Container<updated_data>
Examples
iex> chain([1, 2, 3], fn x -> [x, x] end)
[1, 1, 2, 2, 3, 3]
iex> [1, 2, 3]
...> |> chain(fn x -> [x, x] end)
...> |> chain(fn y -> [y, 2 * y, 3 * y] end)
[1, 2, 3, 1, 2, 3, 2, 4, 6, 2, 4, 6, 3, 6, 9, 3, 6, 9]
iex> chain([1, 2, 3], fn x ->
...> chain([x + 1], fn y ->
...> chain([y + 2, y + 10], fn z ->
...> [x, y, z]
...> end)
...> end)
...> end)
[1, 2, 4, 1, 2, 12, 2, 3, 5, 2, 3, 13, 3, 4, 6, 3, 4, 14]
compose_link(Witchcraft.Chain.link, Witchcraft.Chain.link) :: Witchcraft.Chain.link
Compose link functions to create a new link function.
Note that this runs the same direction as <|>
(“the math way”).
This is pipe_compose_link/2
with arguments flipped.
Examples
iex> links =
...> fn x -> [x, x] end
...> |> compose_link(fn y -> [y * 10] end)
...> |> compose_link(fn z -> [z + 42] end)
...>
...> [1, 2, 3] >>> links
[430, 430, 440, 440, 450, 450]
Join together one nested level of a data structure that contains itself
Examples
iex> join([[1, 2, 3]])
[1, 2, 3]
iex> join([[1, 2, 3], [4, 5, 6]])
[1, 2, 3, 4, 5, 6]
iex> join([[[1, 2, 3], [4, 5, 6]]])
[[1, 2, 3], [4, 5, 6]]
alias Algae.Maybe.{Nothing, Just}
%Just{
just: %Just{
just: 42
}
} |> join()
#=> %Just{just: 42}
join %Just{just: %Nothing{}}
#=> %Nothing{}
join %Just{just: %Just{just: %Nothing{}}}
#=> %Just{just: %Nothing{}}
%Nothing{} |> join() |> join() |> join() # ...and so on, forever
#=> %Nothing{}
Joining tuples is a bit counterintuitive, as it requires a very specific format:
iex> join { # Outer 2-tuple
...> {1, 2}, # Inner 2-tuple
...> {
...> {3, 4}, # Doubly inner 2-tuple
...> {5, 6, 7}
...> }
...> }
{{4, 6}, {5, 6, 7}}
iex> join {
...> {"a", "b"},
...> {
...> {"!", "?"},
...> {:ok, 123}
...> }
...> }
{{"a!", "b?"}, {:ok, 123}}
pipe_compose_link(Witchcraft.Chain.link, Witchcraft.Chain.link) :: Witchcraft.Chain.link
Compose link functions to create a new link function.
This is compose_link/2
with arguments flipped.
Examples
iex> links =
...> fn x -> [x, x] end
...> |> pipe_compose_link(fn y -> [y * 10] end)
...> |> pipe_compose_link(fn z -> [z + 42] end)
...>
...> [1, 2, 3] >>> links
[52, 52, 62, 62, 72, 72]
reverse_bind(Witchcraft.Chain.t, Witchcraft.Chain.link) :: Witchcraft.Chain.t
An alias for reverse_chain/2
.
Provided as a convenience for those coming from other languages.
reverse_chain(Witchcraft.Chain.link, Witchcraft.Chain.t) :: Witchcraft.Chain.t
chain/2
but with the arguments flipped
Examples
iex> reverse_chain(fn x -> [x, x] end, [1, 2, 3])
[1, 1, 2, 2, 3, 3]