Fxnk.Functions (Fxnk v0.1.4) View Source
Fxnk.Functions
are functions for computation or helpers.
Link to this section Summary
Functions
always/1
returns the value passed to it always.
always/2
returns the second value passed to it always.
Curried converge/3
converge/3
takes an initial argument, a function and a list of functions. It applies the argument to each of the list of functions
and then applies the results of those functions as the argument to the end function.
curry/1
takes a function and returns a function.
Returns the empty version of whatever is passed in.
A function that always returns false.
Takes a function, returns a function that takes the same args as the initial function, but flips the order of the arguments.
Same as flip/1
, but takes the arguments at the same time as the function.
identity/1
returns what was passed to it.
juxt/2
takes an initial argument and list of functions and applies the functions to the argument.
Task based juxt/2
. Useful for multiple async functions. While juxt/2
will run in sequence, juxt_async will run in parallel.
For instance, if you've got two functions sleepTwoSeconds
and sleepThreeSeconds
, juxt/2
will respond in five seconds. juxt_async/2
will respond in 3.
Task based Enum.map
. Allows you to concurrently fire off many async functions at the same time, rather than waiting for each to resolve before
starting the next one. Returns when the slowest returns.
Returns the result
of an {:ok, result}
response from a function.
Allows you to partially apply a function. Useful with ok/2
Returns an anonymous function that takes a single argument.
tap/1
takes a function and returns a function that takes a value. Applies the value to the function and then returns the value.
tap/2
takes a value and a function, applies the value to the function and returns the value.
Function that always returns true.
Link to this section Functions
Specs
always/1
returns the value passed to it always.
Examples
iex> fourtyTwo = Fxnk.Functions.always(42)
iex> fourtyTwo.("hello")
42
Specs
always/2
returns the second value passed to it always.
Examples
iex> Fxnk.Functions.always("hello", 42)
42
Specs
Curried converge/3
Example
iex> reverseUpcaseConcat = Fxnk.Functions.converge(&Fxnk.String.concat/2, [&String.reverse/1, &String.upcase/1]) iex> reverseUpcaseConcat.("hello") "ollehHELLO"
Specs
converge/3
takes an initial argument, a function and a list of functions. It applies the argument to each of the list of functions
and then applies the results of those functions as the argument to the end function.
The end function must have the same arity as the length of the list of functions.
Example
iex> Fxnk.Functions.converge("hello", &Fxnk.String.concat/2, [&String.reverse/1, &String.upcase/1])
"ollehHELLO"
Specs
curry/1
takes a function and returns a function.
Examples
iex> add = Fxnk.Functions.curry(fn (a, b) -> a + b end)
iex> add.(6).(7)
13
iex> addOne = Fxnk.Functions.curry(add.(1))
iex> addOne.(1336)
1337
Specs
Returns the empty version of whatever is passed in.
Example
iex> Fxnk.Functions.empty("hello!")
""
iex> Fxnk.Functions.empty(%{foo: "bar"})
%{}
iex> Fxnk.Functions.empty([1,2,3,4])
[]
iex> Fxnk.Functions.empty({:ok, "x"})
{}
Specs
falsy() :: false
A function that always returns false.
Specs
Takes a function, returns a function that takes the same args as the initial function, but flips the order of the arguments.
Example
iex> flippedConcatString = Fxnk.Functions.flip(&Fxnk.String.concat/2)
iex> Fxnk.String.concat("hello", "world")
"helloworld"
iex> flippedConcatString.("hello", "world")
"worldhello"
Specs
Same as flip/1
, but takes the arguments at the same time as the function.
Example
iex> Fxnk.Functions.flip("hello", "world", &Fxnk.String.concat/2)
"worldhello"
Specs
identity/1
returns what was passed to it.
Example
iex> Fxnk.Functions.identity(42)
42
Specs
juxt/1
takes list of functions and returns a curried juxt.
Example
iex> minmax = Fxnk.Functions.juxt([&Fxnk.Math.min/1, &Fxnk.Math.max/1])
iex> minmax.([1,3,5,7])
[1, 7]
Specs
juxt/2
takes an initial argument and list of functions and applies the functions to the argument.
Example
iex> Fxnk.Functions.juxt(%{foo: "foo", bar: "bar", baz: "baz"}, [Fxnk.Map.prop(:foo), Fxnk.Map.prop(:bar)])
["foo", "bar"]
Specs
juxt_async(any(), [function(), ...], non_neg_integer()) :: [any(), ...]
Task based juxt/2
. Useful for multiple async functions. While juxt/2
will run in sequence, juxt_async will run in parallel.
For instance, if you've got two functions sleepTwoSeconds
and sleepThreeSeconds
, juxt/2
will respond in five seconds. juxt_async/2
will respond in 3.
Example
iex> addTwo = fn x ->
...> :timer.sleep(1000)
...> x + 2
...> end
iex> addThree = fn x ->
...> :timer.sleep(2000)
...> x + 3
...> end
iex> Fxnk.Functions.juxt_async(4, [addTwo, addThree])
[6, 7]
Specs
map_async([any(), ...], function(), non_neg_integer()) :: [any(), ...]
Task based Enum.map
. Allows you to concurrently fire off many async functions at the same time, rather than waiting for each to resolve before
starting the next one. Returns when the slowest returns.
Example
iex> addTwo = fn x ->
...> :timer.sleep(1000)
...> x + 2
...> end
iex> Fxnk.Functions.map_async([1,2,3,4,5], [addTwo, addThree])
[3,4,5,6,7]
Specs
Returns the result
of an {:ok, result}
response from a function.
Example
iex> addTwo = fn x -> {:ok, x + 2} end
iex> Fxnk.Functions.ok(4, addTwo)
6
Specs
Allows you to partially apply a function. Useful with ok/2
Returns an anonymous function that takes a single argument.
Examples
iex> %{hello: "world"} |> Fxnk.Functions.ok(Fxnk.Functions.partial(Map, :fetch, [:hello]))
"world"
Specs
tap/1
takes a function and returns a function that takes a value. Applies the value to the function and then returns the value.
Example
iex> function = Fxnk.Functions.tap(&Fxnk.Math.inc/1)
iex> function.(42)
42
Specs
tap/2
takes a value and a function, applies the value to the function and returns the value.
Example
iex> Fxnk.Functions.tap(42, &Fxnk.Math.inc/1)
42
Specs
truthy() :: true
Function that always returns true.
Example
iex> Fxnk.Functions.truthy()
true