Previous section: General Arithmetic Functions

Dylan reference manual -- Functional Operations

12. Other Operations

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  function1 #rest more-functions   =>  function	[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 method square (x :: <number>) x * x end;
square
? define method square-all (coords :: <sequence>) 
      map (square, coords);
  end;
square-all
? define method sum (numbers :: <sequence>)
      reduce1 (\+, numbers);
  end;
sum
? define constant distance = compose(sqrt, sum, square-all);
distance
? distance ( #(3, 4, 5) );
7.0710678118654755
complement   predicate   =>  function	[Function]
complement 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?).
? map (female?, list (michelle, arnold, roseanne));
#(#t, #f, #t)
? map (complement (female?), list (michelle, arnold, roseanne));
#(#f, #t, #f)
disjoin   predicate1 #rest more-predicates   =>  function	[Function]
disjoin 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.

conjoin  predicate1 #rest more-predicates   =>  function	[Function]
conjoin 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.

curry   function #rest curried-args   =>  new-function	[Function]
curry returns a function that applies function to curried-args plus its own arguments, in that order. For example curry (\=, "x") is a predicate that tests for equality with the string "x"; curry (\+, 1) is an incrementing function; curry (\>, 6) is a predicate that returns true for values less than 6; curry (concatenate, "set-") is a function that concatenates the string "set-" to any additional sequences it is passed.
? map (curry (\+, 1), #(3, 4, 5))
#(4, 5, 6)
rcurry  function #rest curried-args   =>  new-function	[Function]
rcurry ("right" curry) operates just like curry, except it allows the rightmost 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 yuppify = rcurry (concatenate, ", ayup");
yuppify
? yuppify ("I'm from New Hampsha");
"I'm from New Hampsha, ayup"
always   object   =>  function	[Function]
always returns a function that can be called with any number of arguments. The function ignores its arguments and always returns object.
? always (1) ("x", "y", "z")
1
? always (#t) (#f, #f)
#t

Next section: Function application