function (ex_stdlib v0.2.0)

View Source

Function utilities.

This module provides utilities for working with functions, including function introspection, composition, currying, and other functional programming patterns.

Examples:

   AddTwo = function:curry(fun erlang:'+'/2, [2]),
   5 = AddTwo(3),
  
   AddThenDouble = function:compose([fun(X) -> X * 2 end, fun(X) -> X + 1 end]),
   8 = AddThenDouble(3).  % (3 + 1) * 2 = 8

Summary

Functions

Applies a function to a list of arguments.

Applies a function from a module to a list of arguments.

Applies a function to up to 4 arguments directly.

Returns a function that returns the boolean complement of the given function.

Composes a list of functions into a single function.

Returns a function that always returns the given constant value.

Curries a function by partially applying some arguments.

Checks if a function is exported by a module.

The identity function - returns its argument unchanged.

Returns information about the given function.

Returns specific information about the given function.

Creates a memoized version of the given function.

Returns a function that negates the result of the given function.

Partially applies a function with the given arguments.

Pipes a value through a list of functions.

Creates a throttled version of the given function.

Types

fun_info/0

-type fun_info() ::
          arity | env | index | name | module | new_index | new_uniq | pid | reductions | type | uniq.

Functions

apply(Fun, Args)

-spec apply(fun(), [term()]) -> term().

Applies a function to a list of arguments.

This is equivalent to erlang:apply/2 but with a more convenient name.

apply(Module, Function, Args)

-spec apply(module(), atom(), [term()]) -> term().

Applies a function from a module to a list of arguments.

This is equivalent to erlang:apply/3 but with a more convenient name.

apply(Fun, Arg1, Arg2, Arg3)

-spec apply(fun(), term(), term(), term()) -> term().

Applies a function to up to 4 arguments directly.

More efficient than building an argument list for simple cases.

complement(Fun)

-spec complement(fun()) -> fun().

Returns a function that returns the boolean complement of the given function.

Alias for negate/1 for better readability in some contexts.

compose(Rest)

-spec compose([fun()]) -> fun().

Composes a list of functions into a single function.

Functions are applied right-to-left (mathematical composition). The last function in the list is applied first.

constant(Value)

-spec constant(term()) -> fun(() -> term()).

Returns a function that always returns the given constant value.

Useful for creating constant functions in functional patterns.

curry(Fun, Args)

-spec curry(fun(), [term()]) -> fun().

Curries a function by partially applying some arguments.

Returns a new function that expects the remaining arguments.

exported(Module, Function, Arity)

-spec exported(module(), atom(), arity()) -> boolean().

Checks if a function is exported by a module.

Returns true if the function is exported, false otherwise.

identity(X)

-spec identity(term()) -> term().

The identity function - returns its argument unchanged.

Useful as a default function or in functional composition.

info(Fun)

-spec info(fun()) -> [{fun_info(), term()}].

Returns information about the given function.

Returns a list of {Key, Value} tuples with function information.

info(Fun, Item)

-spec info(fun(), fun_info()) -> term() | undefined.

Returns specific information about the given function.

Returns the value associated with the given key, or undefined if not found.

memoize(Fun)

-spec memoize(fun()) -> fun().

Creates a memoized version of the given function.

The memoized function caches results based on input arguments. Uses the process dictionary for caching - not suitable for long-lived processes.

negate(Fun)

-spec negate(fun()) -> fun().

Returns a function that negates the result of the given function.

The given function should return a boolean value.

partial(Fun, Args)

-spec partial(fun(), [term()]) -> fun().

Partially applies a function with the given arguments.

Similar to curry/2 but always returns a function regardless of arity.

pipe(Value, Rest)

-spec pipe(term(), [fun()]) -> term().

Pipes a value through a list of functions.

Functions are applied left-to-right (pipeline style).

throttle(Fun, IntervalMs)

-spec throttle(fun(), non_neg_integer()) -> fun().

Creates a throttled version of the given function.

The throttled function can only be called once per interval (in milliseconds).