Functions | |
| muse_cell | fn_size (muse_env *env, void *context, muse_cell args) |
| (size obj) | |
| muse_cell | fn_map (muse_env *env, void *context, muse_cell args) |
| (map fn obj). | |
| muse_cell | fn_join (muse_env *env, void *context, muse_cell args) |
| (join [reduction-fn] obj1 obj2 . | |
| muse_cell | fn_collect (muse_env *env, void *context, muse_cell args) |
| (collect obj predicate mapper [reduction-fn]) EXPERIMENTAL Intended for more general iteration over the collection objects. | |
| muse_cell | fn_reduce (muse_env *env, void *context, muse_cell args) |
| (reduce fn initial obj) | |
| muse_cell | fn_find (muse_env *env, void *context, muse_cell args) |
| (find predicate list) -> list. | |
| muse_cell | fn_andmap (muse_env *env, void *context, muse_cell args) |
| (andmap predicate list). | |
| muse_cell | fn_ormap (muse_env *env, void *context, muse_cell args) |
| (ormap predicate list). | |
| muse_cell | fn_for_each (muse_env *env, void *context, muse_cell args) |
| (for-each fn list [result]). | |
| muse_cell | fn_transpose (muse_env *env, void *context, muse_cell args) |
| (transpose -lists-). | |
(size obj)
Returns the size of the given list (i.e. its length) or vector (same as vector-length) or hashtable (same as hashtable-size).
(map fn obj).
The object can be a list, vector or hashtable and the return value will be of the corresponding type. When the object is a list, the function is expected to take a single argument and provide a mapped value that is collected into Creates a new list whose elements are determined by applying the given function to each argument of the list. For example -
(define squares (map (fn (x) (* x x)) (list 1 2 3 4 5))) (print squares)
(1 4 9 16 25)
map also works with vectors and hashtables. In those cases, it transforms the "values" of those data structures and creates an isomorphic structure with the transformed values.
(join [reduction-fn] obj1 obj2 .
..)
Joins the given objects, all of which must be of the same type. Objects can be lists, vectors or hashtables.
In the case of lists, the lists are all appended in the given order and a new list that is a concatenation of the given lists results.
In the case of vectors, they are all joined end to end to create a new vector whose length is the sum of the lengths of the individual vectors.
In the case of hashtables, a new hashtable with all the key-value pairs in all the given hashtables is created. If two hashtables contain the same key, the value stored in the later specified hashtable takes precedence over the value stored in an earlier one. The join function optionally accepts a reduction function as its first argument. When such a function is given, it is used to combined the values associated with the same key in multiple hashtables. If for a given key K the hashtables has values V1, V2, ... VN, then the result hashtable will have the value R(..R(R(V1, V2), V3)...,VN) where R is the reduction function.
(collect obj predicate mapper [reduction-fn]) EXPERIMENTAL Intended for more general iteration over the collection objects.
The obj is either a list or a vector or a hashtable.
The predicate is a function that selects items from the object. For lists, the predicate is a f(x) where x is a list item. For vectors, the predicate is a f(i . x) where i is the index and x is the value of the vector at the index. For hashtables, the predicate is a f(k . v) where k is a key and v its corresponding value in the hashtable.
The mapper is a function that transforms items into other items. For lists, the mapper is a f(x) where x is a list item. For vectors, the mapper is a f(i . x) where i is the index and x is the value at the index. The result is expected to be a similar (j . y) pair. The result vector is automatically resized to fit the result indices. For hashtables, the mapper is a f(k . v) where k is a key and v is its corresponding value in the hashtable. Similar to vectors, the mapper has to evaluate to a (k' . v') pair that will be placed into the result hashtable.
The optional reduction-fn is a f(v1 v2) that is used to combined multiple values that may be assigned to the same index or key in the cases of vectors and hashtable respectively. If the reduction function is not specified, f(v1 v2) = v2 is used as the function - i.e. replacement.
(reduce fn initial obj)
Reduces the values of the given collection obj using the given reduction function and the initial value. You can use it with lists, vectors and hashtables. The reduction occurs only over the values of the collection. Indices (in the case of vectors) and keys (in the case of hashtables) are not touched at all.
(find predicate list) -> list.
Returns a reference to the first element of the list whose head satisfies the given predicate.
For example -
(print (find (fn (x) (> x 4)) (list 1 2 3 4 5 6 7 8)))
(5 6 7 8)
(andmap predicate list).
Evaluates the predicate on each element of the list. Returns T if everything satisfied the predicate and () if even one element didn't satisfy the predicate. If an element didn't satisfy the predicate, andmap does not evaluate the predicate on subsequent elements.
(ormap predicate list).
Evaluates the predicate on each element of the list. Returns T if anything satisfied the predicate and () if nothing did. If any one element satisfied the predicate, then ormap does not evaluate the predicate on the other elements of the list.
(for-each fn list [result]).
Same as fn_map(), but doesn't collect results into a list. You can optionally give a result expression which will be used as the result of the for-each expression.
(transpose -lists-).
Treats the given lists like the rows of a matrix, transposes the lists and returns a list of the rows of the transposed matrix.
For example -
(transpose '(1 2 3) '(4 5 6)) => '((1 4) (2 5) (3 6)) (transpose '(1 4) '(2 5) '(3 6)) => '((1 2 3) (4 5 6))
Not exactly a HOF, but it can be put to good use to implement arbitrary argument mapping like this -
(define map* (fn (f . args) (map (fn (x) (apply f x)) (apply transpose args)))
You can use map* as follows -
(print (map* + (list 1 2 3 4) (list 10 20 30 40)))
(print (list (+ 1 10) (+ 2 20) (+ 3 30) (+ 4 40)))
(11 22 33 44)
1.4.7