Sequences
Create a sequence from a collection:
Despite the round parentheses in the output, sequences are not lists.
Create a sequence from an empty colleection:
Returning nil (falsy) instead of an empty sequence (truthy) is
useful in conditions.
Get the first element, all but the first element, and the rest of a sequence:
Applied to an empty sequence, both first and next return nil
(truthy), whereas rest returns an empty sequence (falsy):
Add a new element to the front of a square:
Sort and reverse a collection:
Partition a collection into smaller junks:
Weave two collections together:
Insert a separator in between elements:
Those functions turn a collection into a sequence first. Such collections are said to be seqable, i.e. they can be turned into a sequence.
Filter a collection using a predicate:
Check whether or not a predicate holds true for at least one element:
Transform the elements of a collection:
Compose functions:
Executing ((comp f g) x) is equivalent to (f (g x)).
Process a collection in a loop:
Combine the elements of a collection to a single value:
Provide an initial accumulator value (instead of using the first element):
The {:even 0 :odd 0} map (second argument) becomes the initial
acc.
Exercises
Compose Function
Write a function compose that expects a vector functions as its
argument. The function shall return another function expecting a
single argument that first applies the last given function, then
applies its result to the second-last given function, etc.
Hint: Use last and butlast to separate the last element from the
rest of a collection. If no functions are passed, just return the
identity function.
Test: ((compose [#(- % 1) #(* % 2) #(+ % 1)]) 5) shall return 11.
Sequence Count
Write a function seq-count that accepts a vector, a list, a set, or
a map, and returns the number of elements in the given collection.
Hint: Convert the collection to a sequence first and use next to
traverse the sequence.
Test: (seq-count []) shall return 0, (seq-count [1 2 3]) shall
return 3, and (seq-count [nil nil]) shall return 2.
Parse Numbers
Given a collection of numbers, booleans, strings, and elements of
other types, write a function as-nums that returns a sequence of
numbers as follows:
- Numbers shall be retained as they are,
- booleans shall be converted to
1(true) or0(false), - and strings shall be parsed to floating point numbers.
Strings that cannot be parsed as a number or elements of any other type can be ignored and won’t end up in the returned sequence.
Hint: Use Float/parseFloat to parse the strings. Handle a possible
NumberFormatException.
Test: (as-nums [3 2.5 true true false "1.25" "hey" :ho nil 7]) shall
return (3 2.5 1 1 0 1.25 7).
Longest Songs
Given a vector of songs:
Write a function longest that accepts a positive numeric parameter
n, a collection of songs, and returns the n longest songs
formatted as a string.
Hint: Write a helper function parse-duration that converts the
duration into a number of seconds. Use regular expressions with
re-matcher and
re-find for that
purpose. Use sort-by and reverse to sort the result in descending
order, take to extract the first n elements, and interpose to
format the output.
Test: (longest 3 songs) shall return "1) The Ivory Gate of Dreams | 2) Still Remains | 3) The Ghosts of Home".
Pointy Arrow Refactoring
Rrefactor the longest function from the last exercise using the
pointy arrow operator ->>
Hint: The ->> operator takes the return value from a function, and
hands it down to the next function as its last parameter. The range
function creates a sequence starting from the first parameter
(inclusive) up to the second parameter (exclusive). Use interleave
and partition to combine two sequences.
Test: Same as in last exercise.