ListFunctions2(A, B)ΒΆ
list.spad line 301 [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)appliesfnto each element of listuand 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 functionfnon the elements of listuand the result of previous applications.identis returned if theuis 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 functionfnto reduce more and more of listu.identis returned if theuis 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)].