For Development HEAD DRAFTSearch (procedure/syntax/module):

11.65 srfi.235 - Combinators (SRFI)

Module: srfi.235

This srfi provides combinator utilities. Gauche provides some frequently-used combinators built-in. This module augments it.

The following procedures are built-in; see Combinators, for the details.

constantly  complement  flip  swap  boolean

Combinator utilities

Procedures that takes procedure(s) and returns a procedure.

Function: on-left proc

[SRFI-235]{srfi.235} Returns a two argument procedure, which ignores its right argument and calls proc with the left argument.

((on-left car) '(a b) '(c d)) ⇒ a
Function: on-right proc

[SRFI-235]{srfi.235} Returns a two argument procedure, which ignores its left argument and calls proc with the right argument.

((on-right car) '(a b) '(c d)) ⇒ c
Function: conjoin pred …

[SRFI-235]{srfi.235} Returns a procedure taking any number of arguments and that behaves as follows: For each argument, calls pred … left to right; if any of pred returns #f, stop and immediately returns #f. If all predicates returns a true value for all arguments, returns the last result. If no preds are given, or no arguments are given, returns #t.

((conjoin char? digit->integer) #\1 #\2 #\3) ⇒ 3
((conjoin char? digit->integer) #\1 #\a #\3) ⇒ #f
Function: disjoin pred …

[SRFI-235]{srfi.235} Returns a procedure taking any number of arguments and that behaves as follows: For each argument, calls pred … left to right; if any of pred returns a true value, stop and immediately returns that value. If all predicates returns #f for all arguments, returns #f. If no preds are given, or no arguments are given, returns #f.

((disjoin integer? char?) 'a 'b 'c) ⇒ #f
((disjoin integer? char?) #\a 'b 'c) ⇒ #t
Function: each-of proc …

[SRFI-235]{srfi.235} Returns a procedure that works, when applied to arguments, applies each proc on the arguments in turn. The result of procs are discarded.

(define (f x y) (print (+ x y)))
(define (g x y) (print (* x y)))

((each-of f g) 2 7)
  ⇒ #<undef>; prints 9 and 14
Function: all-of pred

[SRFI-235]{srfi.235} Returns a procedure that takes one argument, a list. The returned procedure tries to apply pred to each element of list in turn. If pred returns #f, immediately returns #f. Otherwise, returns the last result of pred. If the returned procedure is applied to an empty list, #t is returned.

The following equivalence holds:

((all-of pred) list) ≡ (every pred list)
Function: any-of pred

[SRFI-235]{srfi.235} Returns a procedure that takes one argument, a list. The returned procedure applies pred to each element of list in turn. If pred returns a true value, immediately returns it. Otherwise, returns #f. If the returned procedure is applied to an empty list, #f is returned.

The following equivalence holds:

((any-of pred) list) ≡ (any pred list)
Function: on reducer mapper

[SRFI-235]{srfi.235} Returns a procedure such that (apply (on reducer mapper) args) works as if (apply reducer (map mapper args)).

(define square-sum (on + square))

(square-sum 1 2 3 4 5) ⇒ 55
Function: left-section proc arg …

[SRFI-235]{srfi.235} Returns a procedure such that, when applied to more args, calls proc with arg … and the given args. In other words, returns a specialized procedure with the left argumen(s) are given as arg ….

In Gauche, this is the same as (pa$ arg …) (see Combinators).

((left-section list 'a 'b 'c) 'd 'e 'f)
 ⇒ (a b c d e f)
Function: right-section proc arg …

[SRFI-235]{srfi.235} Returns a procedure such that, when applied to more args, calls proc with the newly given args concatenated with (reverse arg …).

((right-section list 'a 'b 'c) 'd 'e 'f)
 ⇒ (d e f c b a)

The reason that arg … is reversed is because of this invariance:

(right-section proc arg arg1 ...)
 ≡ (right-section (right-section proc arg) arg1 ...)
Function: apply-chain proc proc2 …

[SRFI-235]{srfi.235} Returns a procedure such that it applies the last procedure to the given arguments, then applies the previous procedure on the returned value(s), and so on. Returns the result(s) of proc.

((apply-chain car cdr) '(a b c d)) ⇒ b
((apply-chain list div-and-mod) 10 3) ⇒ (3 1)
Function: arguments-drop proc n
Function: arguments-drop-right proc n
Function: arguments-take proc n
Function: arguments-take-right proc n

[SRFI-235]{srfi.235} Returns a procedure such that proc is applied on the argument list after taking/dropping n arguments. That is, (arguments-drop proc n) is (^ args (apply proc (drop args n))), and so on.

((arguments-drop list 2) 'a 'b 'c 'd 'e)       ⇒ (c d e)
((arguments-drop-right list 2) 'a 'b 'c 'd 'e) ⇒ (a b c)
((arguments-take list 2) 'a 'b 'c 'd 'e)       ⇒ (a b)
((arguments-take-right list 2) 'a 'b 'c 'd 'e) ⇒ (d e)
Function: group-by key-proc :optional equal

[SRFI-235]{srfi.235} Returns a procedure that takes one list argument. It applies key-proc to each element of the list to get keys, and then elements with the same key are grouped into a fresh list, and returns list of such groups. The keys are compared with equal, which defaults to equal?. Within each group, elements that appears left in the original list also appears on the left.

((group-by key-proc eq-proc) lis) works like (group-collection lis :key key-proc :test eq-proc), except that group-collection can take any collection (see Selection and searching in collection).

((group-by (cut string-ref <> 0) char-ci=?)
 '("apricot" "Banana" "Apple" "blueberry"))
 ⇒ (("apricot" "Apple") ("Banana" "blueberry"))

Syntax-like procedures

Function: begin-procedure thunk …

[SRFI-235]{srfi.235} Calls thunk … from left to right. Discats their results except the last one(s), which will be the result(s) of this procedure.

If no thunks are given, returns #<undef>.

Function: if-procedure value then-thunk else-thunk

[SRFI-235]{srfi.235} If value is true, calls then-thunk and returns its result(s). Otherwise, calls else-thunk and returns its result(s).

Function: when-procedure value thunk …
Function: unless-procedure value thunk …

[SRFI-235]{srfi.235} If value evaluates to true/false, calls each thunk in turn and returns an undefined value, respectively. Otherwise, returns imediately.

Function: value-procedure value then-proc else-thunk

[SRFI-235]{srfi.235} If value evaluates to true, apply then-proc on it and returns its result(s). Otherwise, call else-thunk and returns its result(s).

Function: case-procedure value thunk-alist :optional else-thunk

[SRFI-235]{srfi.235} The thunk-alist argument is a list of pairs of a value and a thunk. First, value is evaluated, then the result is searched from the cars of thunk-alist, using eqv?. If there’s a matching entry, its thunk is called and its result(s) is/are returned. If there’s no matching entry, else-thunk is invoked if present and its result(s) is/are returned. If there’s neither else-thunk, an undefined value is returned.

Function: and-procedure thunk …

[SRFI-235]{srfi.235} Calls thunk … left to right, until it yields #f, and returns #f. If all thunks yields true value, returns the result of the last thunk. If no thunks are given, returns #t.

Function: eager-and-procedure thunk …

[SRFI-235]{srfi.235} Calls all thunks, then checks their results. If any of the result is #f, returns #f. Otherwise, returns the last result of thunk. If no thunks are given, returns #t.

Function: or-procedure thunk …

[SRFI-235]{srfi.235} Calls thunk … left to right, until it yields a true value, and returns that value. If all thunks yields #f, returns #f. If no thunks are given, returns #t.

Function: eager-or-procedure thunk …

[SRFI-235]{srfi.235} Calls all thunks, then checks their results from left to right, and returns the first true value. If all the results are #f, or no thunks are given, returns #f.

Function: funcall-procedure thunk

[SRFI-235]{srfi.235} Just call thunk and returns its result(s). This is useful to pass to other procedures, e.g. (map funcall-procedure list-of-procedures).

Function: loop-procedure thunk

[SRFI-235]{srfi.235} Calls thunk repeatedly forever. Nonlocal escape is the only way to break out.

Function: while-procedure thunk
Function: until-procedure thunk

[SRFI-235]{srfi.235} Calls thunk repeatedly while/until it returns a true value, respectively.

Auxiliary utilities

Function: always obj …

[SRFI-235]{srfi.235} Ignores arguments and always returns #t. Same as (constantly #t).

Function: never obj …

[SRFI-235]{srfi.235} Ignores arguments and always returns #f. Same as (constantly #f).

Note: boolean is provided in the core (see Booleans).



For Development HEAD DRAFTSearch (procedure/syntax/module):
DRAFT