Skip to content
eucalypt
v0.15.0

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

Lists are written with square brackets and commas:

numbers: [1, 2, 3, 4, 5]
strings: ["hello", "world"]
empty: []
nested: [[1, 2], [3, 4]]

head returns the first element; tail returns everything after it:

Terminal window
eu -e '[10, 20, 30] head'
10
Terminal window
eu -e '[10, 20, 30] tail'
- 20
- 30

Use head-or to provide a default for empty lists:

Terminal window
eu -e '[] head-or(0)'
0

first is an alias for head. second returns the second element:

Terminal window
eu -e '[:a, :b, :c] second'
b

cons prepends an element to a list:

Terminal window
eu -e 'cons(0, [1, 2, 3])'
- 0
- 1
- 2
- 3

Test whether a list is empty:

Terminal window
eu -e '[] nil?'
true

Count the elements:

Terminal window
eu -e '[10, 20, 30] count'
3

Apply a function to every element:

Terminal window
eu -e '[1, 2, 3] map(inc)'
- 2
- 3
- 4
Terminal window
eu -e '[1, 2, 3] map(* 10)'
- 10
- 20
- 30

Keep only elements satisfying a predicate:

Terminal window
eu -e '[1, 2, 3, 4, 5, 6] filter(> 3)'
- 4
- 5
- 6

The opposite of filter – remove elements satisfying the predicate:

Terminal window
eu -e '[1, 2, 3, 4, 5] remove(> 3)'
- 1
- 2
- 3

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:

Terminal window
eu -e 'foldl(+, 0, [1, 2, 3, 4, 5])'
15

Right fold: foldr(op, init, list) applies op from the right:

Terminal window
eu -e 'foldr(++, [], [[1, 2], [3, 4], [5]])'
- 1
- 2
- 3
- 4
- 5
Terminal window
eu -e '[1, 2, 3, 4, 5] take(3)'
- 1
- 2
- 3
Terminal window
eu -e '[1, 2, 3, 4, 5] drop(3)'
- 4
- 5
Terminal window
eu -e '[1, 2, 3, 4, 5] take-while(< 4)'
- 1
- 2
- 3
Terminal window
eu -e '[1, 2] ++ [3, 4]'
- 1
- 2
- 3
- 4

Flatten a list of lists:

Terminal window
eu -e 'concat([[1, 2], [3], [4, 5]])'
- 1
- 2
- 3
- 4
- 5

Map then flatten (also known as flatMap or concatMap):

Terminal window
eu -e '["ab", "cd"] mapcat(str.letters)'
- a
- b
- c
- d
Terminal window
eu -e '[true, true, false] all-true?'
false
Terminal window
eu -e '[true, true, false] any-true?'
true

Test with a predicate:

Terminal window
eu -e '[2, 4, 6] all(> 0)'
true
Terminal window
eu -e '[1, 2, 3] any(zero?)'
false
Terminal window
eu -e '[:a, :b, :c] reverse'
- c
- b
- a

Combine two lists element by element:

Terminal window
eu -e 'zip-with(+, [1, 2, 3], [10, 20, 30])'
- 11
- 22
- 33
Terminal window
eu -e 'zip-with(pair, [:x, :y, :z], [1, 2, 3]) block'
x: 1
y: 2
z: 3

Eucalypt supports lazy evaluation, so you can work with infinite lists:

Terminal window
eu -e 'repeat(:x) take(4)'
- x
- x
- x
- x

Use take to extract a finite portion.

Sort with a comparison function:

Terminal window
eu -e '[5, 3, 1, 4, 2] qsort(<)'
- 1
- 2
- 3
- 4
- 5

A convenience for sorting numbers in ascending order:

Terminal window
eu -e '[30, 10, 20] sort-nums'
- 10
- 20
- 30

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: 95
top-scorers:
- Bob
- Diana
  • Lists are created with [...] and can be heterogeneous
  • map, filter, and foldl/foldr are the core transformation functions
  • take, drop, reverse, append (++), and concat reshape lists
  • all, any, all-true?, and any-true? test list conditions
  • Lazy evaluation allows working with infinite lists via repeat
  • qsort sorts with a custom comparator; sort-nums sorts numbers