function (ex_stdlib v0.2.0)
View SourceFunction 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
Functions
Applies a function to a list of arguments.
This is equivalent to erlang:apply/2 but with a more convenient name.
Applies a function from a module to a list of arguments.
This is equivalent to erlang:apply/3 but with a more convenient name.
Applies a function to up to 4 arguments directly.
More efficient than building an argument list for simple cases.
-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.
-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.
Returns a function that always returns the given constant value.
Useful for creating constant functions in functional patterns.
-spec curry(fun(), [term()]) -> fun().
Curries a function by partially applying some arguments.
Returns a new function that expects the remaining arguments.
Checks if a function is exported by a module.
Returns true if the function is exported, false otherwise.
The identity function - returns its argument unchanged.
Useful as a default function or in functional composition.
Returns information about the given function.
Returns a list of {Key, Value} tuples with function information.
Returns specific information about the given function.
Returns the value associated with the given key, or undefined if not found.
-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.
-spec negate(fun()) -> fun().
Returns a function that negates the result of the given function.
The given function should return a boolean value.
-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.
Pipes a value through a list of functions.
Functions are applied left-to-right (pipeline style).
-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).