PSC Code Manual

util/function.lsp [src]

Function-handling functions

Functions

Function signature Description
(apply-by-twos func lst) Applies FUNC to adjacent elements of LST, accumulates and returns the results.
(attempt-apply func args) If FUNC is a function, returns the result of applying it to ARGS. Else, return nil
(compose funcs-lst) Returns a lambda that is the result of composing functions in FUNCS-LST
(function-or-lambda-p x) Returns nil unless X is a function or a lambda
(function-p x) Returns nil unless X is a function
(map-apply f map) Returns the results of applying F to each element of MAP
(map-apply! f map-sym) Updates the list at MAP-SYM in place using map-apply

(apply-by-twos func lst)

Applies FUNC to adjacent elements of LST, accumulates and returns the results.

Example:
(apply-by-twos '+ '(1 2 3)) => '(3 5)

which is equivalently shown as

(list (+ 1 2) (+ 2 3))

VARS:
(FUNC nil (FUNCTION-OR-LAMBDA-P FUNC))
(LST nil (LISTP LST))

TESTS:
(EQUAL (APPLY-BY-TWOS '+ '(1 2 3)) '(3 5))

(attempt-apply func args)

If FUNC is a function, returns the result of applying it to ARGS. Else, return nil

VARS:
(FUNC)
(ARGS (LIST nil))

(compose funcs-lst)

Returns a lambda that is the result of composing functions in FUNCS-LST

FUNCS-LST should be a list of symbols pointing to single-argument functions. The resulting
function ill take a single argument and return the result of applying all the functions in
FUNCS-LST in the reverse of the order specified.

Example:
(compose '(lowercase to-string)) returns a function that take an argument and
returns the result of applying to-string to it, followed by lowercase.

(function-or-lambda-p x)

Returns nil unless X is a function or a lambda

TESTS:
(FUNCTION-OR-LAMBDA-P +)
(FUNCTION-OR-LAMBDA-P '(LAMBDA nil 1))
(NOT (FUNCTION-OR-LAMBDA-P 123))

(function-p x)

Returns nil unless X is a function

TESTS:
(FUNCTION-P +)
(NOT (FUNCTION-P '(LAMBDA nil 1)))
(NOT (FUNCTION-P 123))

(map-apply f map)

Returns the results of applying F to each element of MAP

MAP should be a list of lists of arguments to F.

VARS:
(F nil (FUNCTION-OR-LAMBDA-P F))
(MAP nil (LISTP MAP))

TESTS:
(EQUAL (MAP-APPLY '+ '((1 2) (3 4))) '(3 7))

(map-apply! f map-sym)

Updates the list at MAP-SYM in place using map-apply

VARS:
(F nil (FUNCTION-OR-LAMBDA-P F))
(MAP-SYM SYM (LISTP (VL-SYMBOL-VALUE MAP-SYM)))