Lists and Transformations
In this chapter you will learn:
- How to create and deconstruct lists
- The core list operations:
map,filter,foldl,foldr - Other useful list functions from the prelude
- How to combine list operations into pipelines
Creating Lists
Section titled “Creating Lists”Lists are written with square brackets and commas:
numbers: [1, 2, 3, 4, 5]strings: ["hello", "world"]empty: []nested: [[1, 2], [3, 4]]Basic List Operations
Section titled “Basic List Operations”head and tail
Section titled “head and tail”head returns the first element; tail returns everything after it:
eu -e '[10, 20, 30] head'10eu -e '[10, 20, 30] tail'- 20- 30Use head-or to provide a default for empty lists:
eu -e '[] head-or(0)'0first and second
Section titled “first and second”first is an alias for head. second returns the second element:
eu -e '[:a, :b, :c] second'bcons prepends an element to a list:
eu -e 'cons(0, [1, 2, 3])'- 0- 1- 2- 3Test whether a list is empty:
eu -e '[] nil?'trueCount the elements:
eu -e '[10, 20, 30] count'3Transforming Lists
Section titled “Transforming Lists”Apply a function to every element:
eu -e '[1, 2, 3] map(inc)'- 2- 3- 4eu -e '[1, 2, 3] map(* 10)'- 10- 20- 30filter
Section titled “filter”Keep only elements satisfying a predicate:
eu -e '[1, 2, 3, 4, 5, 6] filter(> 3)'- 4- 5- 6remove
Section titled “remove”The opposite of filter – remove elements satisfying the predicate:
eu -e '[1, 2, 3, 4, 5] remove(> 3)'- 1- 2- 3Folding
Section titled “Folding”Folds reduce a list to a single value by applying a binary function across all elements.
Left fold: foldl(op, init, list) applies op from the left:
eu -e 'foldl(+, 0, [1, 2, 3, 4, 5])'15Right fold: foldr(op, init, list) applies op from the right:
eu -e 'foldr(++, [], [[1, 2], [3, 4], [5]])'- 1- 2- 3- 4- 5Slicing
Section titled “Slicing”take and drop
Section titled “take and drop”eu -e '[1, 2, 3, 4, 5] take(3)'- 1- 2- 3eu -e '[1, 2, 3, 4, 5] drop(3)'- 4- 5take-while and drop-while
Section titled “take-while and drop-while”eu -e '[1, 2, 3, 4, 5] take-while(< 4)'- 1- 2- 3Combining Lists
Section titled “Combining Lists”append and ++
Section titled “append and ++”eu -e '[1, 2] ++ [3, 4]'- 1- 2- 3- 4concat
Section titled “concat”Flatten a list of lists:
eu -e 'concat([[1, 2], [3], [4, 5]])'- 1- 2- 3- 4- 5mapcat
Section titled “mapcat”Map then flatten (also known as flatMap or concatMap):
eu -e '["ab", "cd"] mapcat(str.letters)'- a- b- c- dChecking Lists
Section titled “Checking Lists”all-true? and any-true?
Section titled “all-true? and any-true?”eu -e '[true, true, false] all-true?'falseeu -e '[true, true, false] any-true?'trueall and any
Section titled “all and any”Test with a predicate:
eu -e '[2, 4, 6] all(> 0)'trueeu -e '[1, 2, 3] any(zero?)'falseReordering
Section titled “Reordering”reverse
Section titled “reverse”eu -e '[:a, :b, :c] reverse'- c- b- azip-with
Section titled “zip-with”Combine two lists element by element:
eu -e 'zip-with(+, [1, 2, 3], [10, 20, 30])'- 11- 22- 33zip-with and pair to create blocks
Section titled “zip-with and pair to create blocks”eu -e 'zip-with(pair, [:x, :y, :z], [1, 2, 3]) block'x: 1y: 2z: 3Infinite Lists
Section titled “Infinite Lists”Eucalypt supports lazy evaluation, so you can work with infinite lists:
eu -e 'repeat(:x) take(4)'- x- x- x- xUse take to extract a finite portion.
Sorting
Section titled “Sorting”Sort with a comparison function:
eu -e '[5, 3, 1, 4, 2] qsort(<)'- 1- 2- 3- 4- 5sort-nums
Section titled “sort-nums”A convenience for sorting numbers in ascending order:
eu -e '[30, 10, 20] sort-nums'- 10- 20- 30Putting It Together
Section titled “Putting It Together”Here is a more complete example combining multiple list operations:
data: [ { name: "Alice" score: 85 }, { name: "Bob" score: 92 }, { name: "Charlie" score: 78 }, { name: "Diana" score: 95 }]
top-scorers: data filter(.score >= 90) map(.name)data:- name: Alice score: 85- name: Bob score: 92- name: Charlie score: 78- name: Diana score: 95top-scorers:- Bob- DianaKey Concepts
Section titled “Key Concepts”- Lists are created with
[...]and can be heterogeneous map,filter, andfoldl/foldrare the core transformation functionstake,drop,reverse,append(++), andconcatreshape listsall,any,all-true?, andany-true?test list conditions- Lazy evaluation allows working with infinite lists via
repeat qsortsorts with a custom comparator;sort-numssorts numbers