using TidierIteration;
x = [1:4;]
modify!(x, x->x^2)
x4-element Vector{Int64}:
1
4
9
16
Functions to modify, filter and discard elements of a collection.
modify applies a function f to each element of x.
It also work on dictionaries, keeping the keys intact:
Dict{Int64, Int64} with 4 entries:
4 => 16
2 => 4
3 => 9
1 => 1
We can also modify only when a function p is true:
We can discard some elements of x when a function p is false:
This is the same as base Julia filter(p, x). It also work on dictionaries:
If we want to apply p to the keys of a dictionary, use
Dict{Int64, Int64} with 2 entries:
3 => 9
1 => 1
There is also the negation of keep: discard. It’s definition is trivial: discard(x, p) = keep(x, !p).
When we want to throw away “length zero elements”, use compact: