Chapter 12
The Built-In Functions
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.
compose function1 #rest more-functions
⇒ function
An instance of <function>
.
Instances of <function>
.
An instance of <function>
.
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.
complement predicate
⇒ function
An instance of <function>
.
An instance of <function>
.
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.
disjoin predicate1 #rest more-predicates
⇒ function
An instance of <function>
.
Functions.
An instance of <function>
.
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.
conjoin predicate1 #rest more-predicates
⇒ function
An instance of <function>
.
Instances of <function>
.
An instance of <function>
.
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.
curry function #rest curried-args
⇒ new-function
An instance of <function>
.
Instances of <object>
.
An instance of <function>
.
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.
rcurry function #rest curried-args
⇒ new-function
An instance of <function>
.
Instances of <object>
.
An instance of <function>
.
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.
always object
⇒ function
An instance of <object>
.
An instance of <function>
.
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!"