Functors

This chapter describes how to create, combine and use functors from the MAD environment. Functors are objects that behave like functions with callable semantic, and also like readonly arrays with indexable semantic, where the index is translated as a unique argument into the function call. They are mainly used by the object model to distinguish them from functions which are interpreted as deferred expressions and evaluated automatically on reading, and by the Survey and Track tracking codes to handle (user-defined) actions.

Constructors

This module provides mostly constructors to create functors from functions, functors and any objects with callable semantic, and combine them all together.

functor(f)

Return a functor that encapsulates the function (or any callable object) f. Calling the returned functor is like calling f itself with the same arguments.

compose(f, g)

Return a functor that encapsulates the composition of f and g. Calling the returned functor is like calling \((f \circ g)(\dots)\). The operator f ^ g is a shortcut for compose if f is a functor.

chain(f, g)

Return a functor that encapsulates the calls chain of f and g. Calling the returned functor is like calling \(f(\dots) ; g(\dots)\). The operator f .. g is a shortcut for chain if f is a functor.

achain(f, g)

Return a functor that encapsulates the AND-ed calls chain of f and g. Calling the returned functor is like calling \(f(\dots) \land g(\dots)\).

ochain(f, g)

Return a functor that encapsulates the OR-ed calls chain of f and g. Calling the returned functor is like calling \(f(\dots) \lor g(\dots)\).

bind1st(f, a)

Return a functor that encapsulates f and binds a as its first argument. Calling the returned functor is like calling \(f(a,\dots)\).

bind2nd(f, b)

Return a functor that encapsulates f and binds b as its second argument. Calling the returned functor is like calling \(f(a,b,\dots)\) where a may or may not be provided.

bind3rd(f, c)

Return a functor that encapsulates f and binds c as its third argument. Calling the returned functor is like calling \(f(a,b,c,\dots)\) where a and b may or may not be provided.

bind2st(f, a, b)

Return a functor that encapsulates f and binds a and b as its two first arguments. Calling the returned functor is like calling \(f(a,b,\dots)\).

bind3st(f, a, b, c)

Return a functor that encapsulates f and binds a, b and c as its three first arguments. Calling the returned functor is like calling \(f(a,b,c,\dots)\).

bottom()

Return a functor that encapsulates the identity function ident to define the bottom symbol of functors. Bottom is also available in the operator strings table opstr as "_|_".

Functions

is_functor(a)

Return true if a is a functor, false otherwise. This function is only available from the module MAD.typeid.