ExDash v0.3.1 Exdash.Function

Summary

Functions

Executes fun after it's invoked times or more times

Creates a function that invokes fun while it’s called less than times

Invoke the fun a set amount of times, returning a list of the result of each invocation

Creates a function that accepts arguments of fun and either invokes fun returning its result, if at least arity number of arguments have been provided, or returns a function that accepts the remaining fun arguments, and so on

Creates a function that is restricted to invoking fun once. Repeat calls to the function return the value of the first invocation

Creates a function that invokes fun with partials prepended to the arguments it receives

Creates a function that provides fun to the wrapper function as its first argument

Functions

after_nth(times, fun)

Specs

after_nth(integer, (... -> any)) :: (... -> any)

Executes fun after it's invoked times or more times.

Examples

iex> {:ok, pid} = Agent.start_link(fn -> 0 end)
...> fun = Exdash.Function.after_nth(2, fn ->
...>    Agent.update(pid, fn n -> n + 1 end)
...> end)
...> Enum.each(1..3, fn _ -> fun.() end)
...> Agent.get(pid, &(&1))
2
before_nth(times, fun)

Specs

before_nth(integer, (... -> any)) :: (... -> any)

Creates a function that invokes fun while it’s called less than times.

Examples

iex> {:ok, pid} = Agent.start_link(fn -> 0 end)
...> fun = Exdash.Function.before_nth(2, fn ->
...>    Agent.update(pid, fn n -> n + 1 end)
...> end)
...> Enum.each(1..3, fn _ -> fun.() end)
...> Agent.get(pid, &(&1))
1
call_times(times, fun)

Specs

call_times(integer, (... -> any)) :: list

Invoke the fun a set amount of times, returning a list of the result of each invocation.

Examples

iex> Exdash.Function.call_times(3, &(&1))
[1, 2, 3]

iex> Exdash.Function.call_times(0, &(&1))
[]
curry(func)

Specs

curry((... -> any)) :: (any -> (any -> any))

Creates a function that accepts arguments of fun and either invokes fun returning its result, if at least arity number of arguments have been provided, or returns a function that accepts the remaining fun arguments, and so on.

Examples

iex> add = Exdash.Function.curry(&Kernel.+/2)
...> add_one = add.(1)
...> add_one.(2)
3
curry(func, arity, args)
once(fun)

Specs

once((... -> any)) :: (... -> any)

Creates a function that is restricted to invoking fun once. Repeat calls to the function return the value of the first invocation.

Examples

iex> {:ok, pid} = Agent.start_link(fn -> 0 end)
...> fun = Exdash.Function.once(fn ->
...>   Agent.get_and_update(pid, fn n -> {n, n + 1} end)
...> end)
...> {fun.(), fun.(), Agent.get(pid, &(&1))}
{0, 0, 1}
partial(fun, args)

Specs

partial((... -> any), [...]) :: (any -> any)

Creates a function that invokes fun with partials prepended to the arguments it receives.

## Examples iex> add_one = Exdash.Function.partial(&Kernel.+/2, [1]) ...> add_one.(2) 3

iex> greet = fn greeting, name ->
  ...>  "#{greeting}, #{name}"
  ...> end
  ...> pgreet = Exdash.Function.partial(greet, [Exdash.Placeholder, "Ad"])
  ...> pgreet.("Hi")
  "Hi, Ad"
wrap(fun, wrapper)

Specs

wrap((... -> any), (... -> any)) :: (... -> any)

Creates a function that provides fun to the wrapper function as its first argument.

Examples

iex> escape = fn text -> "/#{text}/" end
...> p = Exdash.Function.wrap(escape, fn (fun, value) ->
...>   "<p>#{fun.(value)}</p>"
...> end)
...> p.("some text")
"<p>/some text/</p>"