## 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.

composefunction1#restmore-functions=>function[Function]

When called with just a single argument,composereturns that argument.When called with two arguments,

composereturns 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,

composecomposes 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

complementpredicate=>function[Function]

complementreturns a function that appliespredicateto its arguments. If thepredicatereturns#f, the complement returns#t; otherwise, the complement returns#f. For example,odd?could be defined ascomplement (even?).?map (female?, list (michelle, arnold, roseanne));#(#t, #f, #t) ?map (complement (female?), list (michelle, arnold, roseanne));#(#f, #t, #f)

disjoinpredicate1#restmore-predicates=>function[Function]

disjoinreturns 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#freturned.A disjunction is similar to an

|expression of calls to the predicates.

conjoinpredicate1#restmore-predicates=>function[Function]

conjoinreturns 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#fis 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.

curryfunction#restcurried-args=>new-function[Function]

curryreturns a function that appliesfunctiontocurried-argsplus its own arguments, in that order. For examplecurry (\=, "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)

rcurryfunction#restcurried-args=>new-function[Function]

rcurry("right" curry) operates just likecurry, except it allows the rightmost arguments offunctionto 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"

alwaysobject=>function[Function]

Next section: Function applicationalwaysreturns a function that can be called with any number of arguments. The function ignores its arguments and always returnsobject.?always (1) ("x", "y", "z")1 ?always (#t) (#f, #f)#t