Fxnk v0.1.1 Fxnk.Flow View Source
Fxnk.Flow
functions are used for control flow.
Link to this section Summary
Functions
Curried compose/2
.
compose/2
takes an input and a list of functions and runs the functions against the input
from right to left.
if_else/4
takes an input, a predicate, a pass function and a fail function.
Runs the pass function if the predicate returns true when passed the input, otherwise runs the fail function.
pipe/2
takes an input and a list of functions and runs the functions against the input
from left to right.
Curried unless_is/3
.
unless_is
is a logic flow function, which takes an input, a predicate function, and an action function,
allowing the action function to run unless the input returns true when ran against the predicate.
Curried until/3
.
until/3
takes an input, a predicate function and an action function,
running the action function on the input until the predicate is satisfied.
Curried when_is/3
.
when_is
is a logic flow function, which takes an input, a predicate function, and an action function,
allowing the action function to run when the input returns true when ran against the predicate.
Link to this section Functions
Specs
Curried compose/2
.
Examples
iex> reverseSort = Fxnk.Flow.compose([&Enum.reverse/1, &Enum.sort/1])
iex> reverseSort.([1,3,5,7,6,4,2])
[7, 6, 5, 4, 3, 2, 1]
Specs
compose/2
takes an input and a list of functions and runs the functions against the input
from right to left.
Examples
iex> [1,3,5,7,6,4,2] |> Fxnk.Flow.compose([&Enum.reverse/1, &Enum.sort/1])
[7, 6, 5, 4, 3, 2, 1]
Specs
Curried if_else/3
Examples
iex> multTwoIfLessThanTenOrDivideByTwo = Fxnk.Flow.if_else(fn x -> x < 10 end, fn x -> x * 2 end, fn x -> div(x, 2) end)
iex> multTwoIfLessThanTenOrDivideByTwo.(5)
10
iex> multTwoIfLessThanTenOrDivideByTwo.(20)
10
Specs
if_else/4
takes an input, a predicate, a pass function and a fail function.
Runs the pass function if the predicate returns true when passed the input, otherwise runs the fail function.
Examples
iex> Fxnk.Flow.if_else(5, fn x -> x < 10 end, fn x -> x * 2 end, fn x -> div(x, 2) end)
10
iex> Fxnk.Flow.if_else(20, fn x -> x < 10 end, fn x -> x * 2 end, fn x -> div(x, 2) end)
10
Specs
Curried pipe/2
.
Examples
iex> reverseSort = Fxnk.Flow.pipe([&Enum.sort/1, &Enum.reverse/1])
iex> reverseSort.([1,3,5,7,6,4,2])
[7, 6, 5, 4, 3, 2, 1]
Specs
pipe/2
takes an input and a list of functions and runs the functions against the input
from left to right.
Examples
iex> [1,3,5,7,6,4,2] |> Fxnk.Flow.pipe([&Enum.sort/1, &Enum.reverse/1])
[7, 6, 5, 4, 3, 2, 1]
Specs
Curried unless_is/3
.
Examples
iex> multiplyByTwoUnlessGreaterThan10 = Fxnk.Flow.unless_is(fn n -> n > 10 end, fn x -> x * 2 end)
iex> multiplyByTwoUnlessGreaterThan10.(15)
15
iex> multiplyByTwoUnlessGreaterThan10.(2)
4
Specs
unless_is
is a logic flow function, which takes an input, a predicate function, and an action function,
allowing the action function to run unless the input returns true when ran against the predicate.
Example
iex> Fxnk.Flow.unless_is(15, fn n -> n > 10 end, fn x -> x * 2 end)
15
iex> Fxnk.Flow.unless_is(2, fn n -> n > 10 end, fn x -> x * 2 end)
4
Specs
Curried until/3
.
Examples
iex> timesTwoUntilGreaterThan100 = Fxnk.Flow.until(fn x -> x > 100 end, fn n -> n * 2 end)
iex> timesTwoUntilGreaterThan100.(1)
128
Specs
until/3
takes an input, a predicate function and an action function,
running the action function on the input until the predicate is satisfied.
Examples
iex> Fxnk.Flow.until(1, fn x -> x > 100 end, fn n -> n * 2 end)
128
Specs
Curried when_is/3
.
Examples
iex> timesTwoWhenGreaterThan10 = Fxnk.Flow.when_is(fn x -> x > 10 end, fn n -> n * 2 end)
iex> timesTwoWhenGreaterThan10.(15)
30
iex> timesTwoWhenGreaterThan10.(5)
5
Specs
when_is
is a logic flow function, which takes an input, a predicate function, and an action function,
allowing the action function to run when the input returns true when ran against the predicate.
Examples
iex> Fxnk.Flow.when_is(15, fn x -> x > 10 end, fn n -> n * 2 end)
30
iex> Fxnk.Flow.when_is(5, fn x -> x > 10 end, fn n -> n * 2 end)
5