sky v0.2.0 Sky
Collection of higher-oder functions not present in the standard library.
Summary
Functions
Given two one-argument functions, a subject f and a predicate p, return a function that takes an argument x
Get the arity of the given function
Creates a function that receives an argument and always returns the original given value
Curries the given function, starting with an optional list of given params
Given a single-argument function f, return a new function that
receives either a tuple in the form {:ok, value}
which would execute
f.(value)
, or any other term, which would be returned without change
Given a predicate p, which receives a single argument and returns a
boolean value, return a function which is equivalent to not p.(x)
Given a one-argument function that raises an exception, return a function that instead returns any of the following tuples
Given two one-argument functions, a subject f and a predicate p, return a function that takes an argument x
Given a two argument functions, swap the order in wich the arguments are received
Given a function f with arity n, return a function that receives a single tuple of n elements and applies them to the original f
Functions
Given two one-argument functions, a subject f and a predicate p, return a function that takes an argument x.
However, in contrast to Sky.reject_if/2
, if the predicate is not satisfied,
the parameter x is returned. Otherwise the return value is f.(x)
.
Example
iex> trim = (&tl/1)
iex> non_empty? = fn list -> length(list) > 0 end
iex> safetrim = Sky.apply_if(trim, non_empty?)
iex> safetrim.([1,2])
[2]
iex> safetrim.([])
[]
Get the arity of the given function.
Examples
iex> Sky.arity(&Enum.map/2)
2
iex> Sky.arity(fn(_, _, _) -> nil end)
3
Creates a function that receives an argument and always returns the original given value.
Example
iex> one = Sky.constant(1)
iex> one.(2)
1
iex> one.(nil)
1
Curries the given function, starting with an optional list of given params.
Examples
The order of the arguments is respected.
iex> Sky.curry(fn(a, b) -> a - b end).(5).(4)
1
You can also pass it a list of predefined parameters.
iex> Sky.curry(fn(a, b, c) -> a + b + c end, [1, 2]).(3)
6
Given a single-argument function f, return a new function that
receives either a tuple in the form {:ok, value}
which would execute
f.(value)
, or any other term, which would be returned without change.
In case of success, the return value of the returned function is always
in the form {:ok, value}
even if the result of f was in the same form.
This means that any nesting is eliminated.
Example
iex> inc = Sky.lift_ok(fn n -> n + 1 end)
iex> inc.({:ok, 1})
{:ok, 2}
iex> inc.({:error, :bad_input})
{:error, :bad_input}
Note that the value is always a two-element tuple {:ok, v}
.
iex> inc = Sky.lift_ok(fn n -> n + 1 end)
iex> inc.(inc.({:ok, 1}))
{:ok, 3}
Given a predicate p, which receives a single argument and returns a
boolean value, return a function which is equivalent to not p.(x)
Example
iex> negative? = fn x -> x < 0 end
iex> no_negative? = Sky.negate(negative?)
iex> no_negative?.(1)
true
Given a one-argument function that raises an exception, return a function that instead returns any of the following tuples:
- {:ok, value} when no exception is raised
- {:error, exception} when an exception is raised.
Examples
iex> safe_float_div = Sky.noraise(fn ({a, b}) -> a / b end)
iex> safe_float_div.({1, 0})
{:error, %ArithmeticError{message: "bad argument in arithmetic expression"}}
iex> safe_float_div.({1, 2})
{:ok, 0.5}
Given two one-argument functions, a subject f and a predicate p, return a function that takes an argument x.
Such function returns {:ok, f.(x)}
only if p.(x)
is truthy, :error otherwise.
Example
iex> f = fn x -> x - 1 end
iex> p = fn x -> x > 0 end
iex> Sky.reject_if(f, p).(1)
{:ok, 0}
iex> Sky.reject_if(f, p).(0)
:error
Given a two argument functions, swap the order in wich the arguments are received.
Examples
iex> Sky.swap(&rem/2).(7, 5)
5
iex> Sky.swap(fn(a, b) -> a - b end).(2, 1)
-1
Given a function f with arity n, return a function that receives a single tuple of n elements and applies them to the original f.
Example
iex> Sky.tupleize(fn(a, b) -> a + b end).({1, 2})
3
It might seem less useful for functions of one argument, but you might gain composability with other functions from the module.