Module f

A collection of functions and types to make your Erlang code more functional, funky and/or fancy.

Copyright © (C) 2020, Roman Pushkov

Authors: Roman Pushkov (pushkovroman@me.com).

Description

A collection of functions and types to make your Erlang code more functional, funky and/or fancy.

Data Types

curried()

curried(X, Y, T) = lambda(X, curried(Y, T) | T)

curried()

curried(X, T) = lambda(X, curried(T) | T)

curried()

curried(T) = lambda(any(), curried(T) | T)

curried()

curried() = curried(any())

err()

err(E) = {error, E}

func()

func(T) = fun((...) -> T)

func()

func() = func(any())

lambda()

lambda(X, T) = fun((X) -> T)

lambda()

lambda(T) = lambda(any(), T)

lambda()

lambda() = lambda(any())

ok()

ok(T) = {ok, T}

result()

result(T, E) = ok(T) | err(E)

result()

result(T) = result(T, any())

result()

result() = result(any(), any())

safe()

safe(X, T, E) = lambda(X, result(T, E))

safe()

safe(X, T) = lambda(X, result(T))

safe()

safe(T) = lambda(result(T))

safe()

safe() = safe(any())

Function Index

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.

Function Details

curry/1

curry(F) -> curried(T)

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]

do/2

do(X, Fs) -> result()

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/1

err(E) -> err(E)

The err function

  Wraps data into the err type.
 
  Example:
  > f:err(zoomer).
  {error, zoomer}

flip/1

flip(F::func(T) | curried(X, Y, T)) -> curried(Y, X, T)

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/1

id(T) -> T

The identity function

  Returns the passed entity.
 
  Example:
  > f:id(a).
  a

is_curried/1

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/1

ok(T) -> ok(T)

The ok function

  Wraps data into the ok type.
 
  Example:
  > f:ok(boomer).
  {ok, boomer}

partial/2

partial(F, Args::list()) -> curried(T) | T

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/2

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/1

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