Copyright © (C) 2020, Roman Pushkov
Authors: Roman Pushkov (pushkovroman@me.com).
curried(X, Y, T) = lambda(X, curried(Y, T) | T)
curried(X, T) = lambda(X, curried(T) | T)
curried(T) = lambda(any(), curried(T) | T)
curried() = curried(any())
err(E) = {error, E}
func(T) = fun((...) -> T)
func() = func(any())
lambda(X, T) = fun((X) -> T)
lambda(T) = lambda(any(), T)
lambda() = lambda(any())
ok(T) = {ok, T}
result(T) = result(T, any())
result() = result(any(), any())
safe(X, T, E) = lambda(X, result(T, E))
safe(X, T) = lambda(X, result(T))
safe() = safe(any())
curry/1 | The currying function. |
do/2 | The do function. |
err/1 | The err function. |
flip/1 | The flip function. |
id/1 | The identity function. |
is_curried/1 | The is_curried function. |
ok/1 | The ok function. |
partial/2 | The partial application function. |
pipe/2 | The pipe function. |
unwrap/1 | The unwrap function. |
The currying function
Turns a multiple-arity function into a sequence of single-arity functions, so that F(X, Y, Z) becomes Fcurried(X)(Y)(Z). Note that Erlang's syntax will require extra parentheses if you wish to pass the arguments in a single statement. Example: > Multiply = f:curry(fun erlang:'*'/2), > Double = Multiply(2), > Double(10). 20 > Map = f:curry(fun lists:map/2), > Sequence = f:curry(fun lists:seq/2), > From1To = Sequence(1), > (Map(Double))(From1To(10)), [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
The do function
Similar to the pipe function, do pipes data through series of functions, however in this case the functions should return the result type: either an {ok, Value} or {error, Reason}. Terminates on an error result, preventing further computation and returning the error result itself. Example: > f:do(1, [ > (fun(X) -> f:ok(X * 1) end), > (fun(_) -> f:err(go_away) end), > (fun(X) -> f:ok(X + 1) end) > ]). {error, go_away}
err(E) -> err(E)
The err function
Wraps data into the err type. Example: > f:err(zoomer). {error, zoomer}
The flip function
Flips the next two arguments of a curried function. A non-curried function will be curried before flip is applied. Will return an unchanged function if no more arguments to be applied are left. Example: > Get = (f:curry(fun maps:get/3))(a), > GetWithDefault = (f:flip(Get))(42), > GetWithDefault(#{}). 42
id(T) -> T
The identity function
Returns the passed entity. Example: > f:id(a). a
is_curried(F) -> boolean()
The is_curried function
Checks if a function is curried. Single-arity functions are considered curried. Example: > f:is_curried(fun(_) -> hello end). true > f:is_curried(fun lists:foldl/3). false > f:is_curried(f:curry(fun lists:foldl/3)). true
ok(T) -> ok(T)
The ok function
Wraps data into the ok type. Example: > f:ok(boomer). {ok, boomer}
The partial application function
Partially applies a list of arguments to a function. Example: > Foldl = f:partial(fun lists:foldl/3), > Sum = f:partial(Foldl, [fun erlang:'+'/2, 0]), > Sum(lists:seq(1, 10)). 55
pipe(X, Fs) -> T
The pipe function
Pipes data through a list of single-arity functions. Naturally, you may use partially applied functions. Example: > f:pipe(1, [ > (f:curry(fun erlang:'+'/2))(1), > (fun(X) -> X * 2 end), > (f:partial(fun lists:seq/3, [1, 50])) > ]). [1,5,9,13,17,21,25,29,33,37,41,45,49]
unwrap(T::result(T)) -> T | no_return()
The unwrap function
Unwraps the result type. Returns the actual result or throws the stored error. Example: > f:unwrap(f:ok(1)). 1 > f:unwrap(f:err(go_away)). ** exception throw: go_away
Generated by EDoc