Skip to content

listutils

Bill Hails edited this page Oct 27, 2024 · 3 revisions

The listutils.fn namespace in the fn/ directory contains basic functions for dealing with lists. It can be included by link "listutils.fn" as list; in the let section of your source file.

At the time of writing it includes the following functions.

Name Type Description
member(item, lst) #a -> list(#a) -> bool true if the first argument is a member of the second argument list.
exclude(list, lst) list(#t) -> list(#t) -> list(#t) returns the second argument list minus any elements on the first argument list.
map(function, lst) (#a -> #b) -> list(#a) -> list(#b) Applies the first argument function to each element of the second argument list and returns a list of the results.
length(lst) list(#s) -> number Returns the length of the argument list.
foldl(fn, acc, lst) (#a -> #b -> #b) -> #b -> list(#a) -> #b Example: foldl(fn (a, b) { a + b }, 0, [1, 2, 3]) will add up all of the elements of the list.
foldr(fn, acc, lst) (#a -> #b -> #b) -> #b -> list(#a) -> #b Same as foldl but starts at the end of the list and works backwards.
foldl1(fn, lst) (#a -> #a -> #a) -> list(#a) -> #a Like foldl but uses the first item on the list as the initial value.
foldr1(fn, lst) (#a -> #a -> #a) -> list(#a) -> #a Like foldl1 but from the right like foldr.
reverse(lst) list(#a) -> list(#a) Returns the reverse of its argument list.
scanl(fn, acc, lst) (#a -> #b -> #b) -> #a-> list(#a) -> list(list(#b)) like foldl but returns a list of the intermediate values.
filter(fn, lst) (#a -> bool) list(#a) -> list(#a) applies its first argument function to each item on its second argument list and returns a list of those items for which the function returned true.
concat(list(lst)) list(list(#a)) -> list(#a) Combines its argument list of lists into a single list.
join(lst, list(lst)) list(#a) -> list(list(#a)) -> list(#a) Like concat but interpolates its first argument between each element of its second.
any(fn, lst) (#a -> bool) -> list(#a) -> bool Returns true if its argument function returns true for any elements of the list.
none(fn, lst) (#a -> bool) -> list(#a) -> bool Returns true only if its argument function returns false for every element of the list.
all(fn, lst) (#a -> bool) -> list(#a) -> bool Returns true only if its argument function returns true for every element of the list.
repeat(n, item) number -> #a -> list(#a) Example repeat(5, 'x'); // "xxxxx"
nth(n, lst) number -> list(#a) -> #a Returns the nth element of the list.
sum(lst) list(number) -> number adds up all of the elements of the list
product(lst) list(number) -> number multiplies all of the elements of the list.
zip(lst, lst) list(#a) -> list(#b) -> list(#(#a, #b)) Pairs each element of its first argument list with the corresponding element of the second argument list, returns a list of those pairs (tuples).
zipwith(fn, lst, lst) (#a -> #b -> #c) -> list(#a) -> list(#b) -> list(#c) Like zip but takes a third argument function to do the combining.
last(lst) list(#a) -> #a Returns the last element of the argument list.
empty(lst) list(#a) -> bool Returns true if the argument list is empty.
take(n, lst) number -> list(#a) -> list(#a) Returns the first n items from the list.
drop(n, lst) number -> list(#a) -> list(#a) Returns all but the first n elements of the list.
minimum(lst) list(#a) -> #a Returns the smallest element on the list.
maximum(lst) list(#a) -> #a Returns the largest element on the list.
range(n, n) number -> number -> list(number) Returns a list of numbers from low to high, inclusive.
dedup(lst) list(#a) -> list(#a) returns the unique elements of the argument list.
sortBy(fn, lst) (#a -> #a -> cmp) -> list(#a) -> list(#a) Takes a comparison function and uses it to sort the list.
sort(lst) list(#a) -> list(#a) Sorts the list using the standard <=> operator.

Next: Anb Utils.

Clone this wiki locally