Previous chapter: Characters

Dylan manual: Functional Operations

## 17. 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	[Method]
```
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. Thus, cadr could be expressed (compose car cdr).

With three or more arguments, compose composes pairs of argument functions, until a single composite function is obtained. (Because functional composition is associative, the order in which this is performed is unimportant.)

```? (define-method sum ((numbers <sequence>))
(reduce1 + numbers))
sum
? (define-method square ((x <number>)) (* x x))
square
? (define-method square-all ((coords <sequence>))
(map square coords)
square-all
? (define distance (compose sqrt sum square-all))
distance
? (distance '(3 4 5))
7.0710678118654755
```
```complement   predicate   ==>  function	[Method]
```
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	[Method]
```
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 or expression of calls to the predicates.

```conjoin   predicate1 #rest more-predicates   ==>  function	[Method]
```
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 and expression of calls to the predicates.

```curry   function #rest curried-args   ==>  new-function	[Method]
```
curry returns a function that applies function to curried-args plus its own arguments, in that order. For example (curry id? 'x) is a predicate that tests for identity with the symbol 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	[Method]
```
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 yuppify (rcurry concatenate ", ayup"))
yuppify
? (yuppify "I'm from New Hampsha")
"I'm from New Hampsha, ayup"
```
```always   object   ==>  function	[Method]
```
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
```