Fxnk.Flow (Fxnk v0.1.4) View Source

Fxnk.Flow functions are used for control flow.

Link to this section Summary

Functions

and_then/2 allows you to chain together {:ok, _} functions. Stops processing on the first {:error, _} and returns the error.

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.

Handle errors gracefully. When an error is encountered, apply a function to that error. When its not an error, do nothing.

Curried pipe/2.

pipe/2 takes an input and a list of functions and runs the functions against the input from left to right.

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.

until/3 takes an input, a predicate function and an action function, running the action function on the input until the predicate is satisfied.

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

and_then({:error, any()} | {:ok, any()}, function()) :: any()

and_then/2 allows you to chain together {:ok, _} functions. Stops processing on the first {:error, _} and returns the error.

Examples

iex> map = %{foo: "foo", bar: "bar", baz: "baz"}
iex> uppercase_okay = fn str -> {:ok, String.upcase(str) } end
iex> reverse_okay = fn str -> {:ok, String.reverse(str)} end
iex> map |> Map.fetch(:foo) |> Fxnk.Flow.and_then(uppercase_okay) |> Fxnk.Flow.and_then(reverse_okay)
{:ok, "OOF"}
iex> throw_error = fn _ -> {:error, :input_should_not_be_foo} end
iex> map |> Map.fetch(:foo) |> Fxnk.Flow.and_then(throw_error) |> Fxnk.Flow.and_then(reverse_okay)
{:error, :input_should_not_be_foo}

Specs

compose([function(), ...]) :: (any() -> any())

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(any(), [function(), ...]) :: any()

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]
Link to this function

if_else(pred, passFunc, failFunc)

View Source

Specs

if_else(function(), function(), function()) :: (any() -> any())

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
Link to this function

if_else(input, pred, passFunc, failFunc)

View Source

Specs

if_else(any(), function(), function(), function()) :: (any() -> any())

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

on_error({:error, any()} | any(), function()) :: any()

Handle errors gracefully. When an error is encountered, apply a function to that error. When its not an error, do nothing.

Example:

iex> make_error = fn message -> {:error, message} end
iex> handle_error = fn message -> Atom.to_string(message) end
iex> make_error.(:foo) |> Fxnk.Flow.on_error(handle_error)
"foo"
iex> make_success = fn message -> {:ok, message} end
iex> make_success.(:bar) |> Fxnk.Flow.on_error(handle_error)
{:ok, :bar}

Specs

pipe([function(), ...]) :: (any() -> any())

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(any(), [function(), ...]) :: any()

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

unless_is(function(), function()) :: (any() -> any())

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
Link to this function

unless_is(input, pred, func)

View Source

Specs

unless_is(any(), function(), function()) :: any()

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

until(function(), function()) :: (any() -> any())

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(any(), function(), function()) :: any()

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

when_is(function(), function()) :: (any() -> any())

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
Link to this function

when_is(input, pred, func)

View Source

Specs

when_is(any(), function(), function()) :: any()

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