ListFunctions2(A, B)ΒΆ
list.spad line 292 [edit on github]
ListFunctions2 implements utility functions that operate on two kinds of lists, each with a possibly different type of element.
- map: (A -> B, List A) -> List B
map(fn, u)
appliesfn
to each element of listu
and returns a new list with the results. For examplemap(square, [1, 2, 3]) = [1, 4, 9]
.
- reduce: ((A, B) -> B, List A, B) -> B
reduce(fn, u, ident)
successively uses the binary functionfn
on the elements of listu
and the result of previous applications.ident
is returned if theu
is empty. Note the order of application in the following examples:reduce(fn, [1, 2, 3], 0) = fn(3, fn(2, fn(1, 0)))
andreduce(*, [2, 3], 1) = 3 * (2 * 1)
.
- scan: ((A, B) -> B, List A, B) -> List B
scan(fn, u, ident)
successively uses the binary functionfn
to reduce more and more of listu
.ident
is returned if theu
is empty. The result is a list of the reductions at each step. See reduce for more information. Examples:scan(fn, [1, 2], 0) = [fn(2, fn(1, 0)), fn(1, 0)]
andscan(*, [2, 3], 1) = [2 * 1, 3 * (2 * 1)]
.