Chapter 12

The Built-In Functions


Functional Operations

The following operations are used to create new functions from other functions or objects. Often the Dylan compiler will have special knowledge of these operations to allow for efficient in-line compilation.

compose [Function]


Returns the composition of one or more functions.

Signature:

compose function1 #rest more-functions function

Arguments:
function1

An instance of <function>.

more-functions

Instances of <function>.

Values:
function

An instance of <function>.

Description:

When called with just a single argument, compose returns that argument.

When called with two arguments, compose returns a function that applies the second function to its arguments and then applies the first function to the (single) result value.

With three or more arguments, compose composes pairs of argument functions, until a single composite function is obtained. (It doesn't matter if the pairings are done from the left or from the right, as long as the order of application is preserved.)

define constant number-of-methods =
  compose(size, generic-function-methods)
define constant root-position = compose(position, root-view) 

complement [Function]


Returns a function that expresses the complement of a predicate.

Signature:

complement predicate function

Arguments:
predicate

An instance of <function>.

Values:
function

An instance of <function>.

Description:

Returns a function that applies predicate to its arguments. If the predicate returns #f, the complement returns #t; otherwise, the complement returns #f. For example, odd? could be defined as complement(even?).

choose(complement(zero?), #(1, 3, 0, 4, 0, 0, 3))
  ⇒  #(1, 3, 4, 3) 

disjoin [Function]


Returns a function that expresses the disjunction of one or more predicates.

Signature:

disjoin predicate1 #rest more-predicates function

Arguments:
predicate1

An instance of <function>.

more-predicates

Functions.

Values:
function

An instance of <function>.

Description:

Returns a single function, termed the disjunction of its argument functions. The disjunction accepts any number of arguments and operates by applying the predicates, in order, to the arguments. If any of the predicates returns true, the remaining predicates (if any) are not applied, and the true result is returned. Otherwise, all the predicates will be applied, and #f returned.

A disjunction is similar to an | expression of calls to the predicates.

define constant nonzero? = disjoin(positive?, negative?);
nonzero?(4)
  #t 

conjoin [Function]


Returns a function that expresses the conjunction of one or more predicates.

Signature:

conjoin predicate1 #rest more-predicates function

Arguments:
predicate1

An instance of <function>.

more-predicates

Instances of <function>.

Values:
function

An instance of <function>.

Description:

Returns a single function, termed the conjunction of its argument functions. The conjunction accepts any number of arguments and operates by applying the predicates, in order, to the arguments. If any of the predicates returns #f, the remaining predicates (if any) are not applied and #f is immediately returned. Otherwise, all the predicates will be applied, and the result of the last application is returned.

A conjunction is similar to an & expression of calls to the predicates.

choose(conjoin(positive?, integral?), #(-1, -3, 5, -3.7, 3.5, 7))
  ⇒ #(5, 7) 

curry [Function]


Returns a function based on an existing function and a number of default initial arguments.

Signature:

curry function #rest curried-args new-function

Arguments:
function

An instance of <function>.

curried-args

Instances of <object>.

Values:
new-function

An instance of <function>.

Description:

Returns a function that applies function to curried-args plus its own arguments, in that order. For example curry (\>, 6) is a predicate that returns true for values less than 6; curry (\=, "x") is a predicate that tests for equality with the string "x"; curry (\+, 1) is an incrementing function; curry (concatenate, "set-") is a function that concatenates the string "set-" to any additional sequences it is passed.

define constant all-odd? = curry(every?, odd?)
all-odd?(list(1, 3, 5))
 #t
define constant less-than-10? = curry(\>, 10)
less-than-10?(4)
 #t 

rcurry [Function]


Returns a function based on an existing function and a number of default final arguments.

Signature:

rcurry function #rest curried-args new-function

Arguments:
function

An instance of <function>.

curried-args

Instances of <object>.

Values:
new-function

An instance of <function>.

Description:

Returns a function that applies function to curried-args plus its own arguments, with the curried-args occurring last.

rcurry ("right" curry) operates just like curry, except it allows the rightmlost arguments of function to be specified in advance, rather than the leftmost arguments. For example, rcurry (\>, 6) is a predicate that returns true for values greater than 6.

define constant number? = rcurry(instance?, <number>)
number?(4) 
 #t
number?("string")
 #f
define constant greater-than-10? = rcurry(\>, 10)
greater-than-10?(4)
 #f 

always [Function]


Returns a function that always returns a particular object.

Signature:

always object function

Arguments:
object

An instance of <object>.

Values:
function

An instance of <function>.

Description:

Returns a function that can be called with any number of arguments. The function ignores its arguments and always returns object.

define constant menu = always("spam!")
menu("today") 
 "spam!"
menu("tomorrow")  
 "spam!"
menu(4, 5, 6)
 "spam!"