Lists can be mapped/transformed element-wise by a function:
let out = [1.0, 2.0, 3.0] |> sqrt
This is equivalent to:
var out: float[]
for x in [1.0, 2.0, 3.0]
out.add(sqrt(x))
Multiple mapping stages can be chained. It is advisable to keep each stage in a separate line:
["1", "2.1", "3"]
|> string.__f32__
|> sqrt
|> sin
Functions' output types don't have to be the same as the input types, as long as consecutive stages match up.
Functions can also act on the whole list. E.g. the some of squares can be computed with func sum(a: f32[]) -> f32
:
[1.0, 2.0, 3.0]
|> sqr
|> sum
Optionals can have their values be extracted, transformed and re-wrapped:
(13.0 as f32?) |> sin
🔥 If the function returns an optional, then the additional wrapping will be skipped!
"Simple" ascending sorting of basic values (can deal with i32
, f32
, string
, Path
):
range(10.0)
|> sin
|> sort
Data structures can be sorted by elements of basic values:
[vec3(1,0,0), vec3(0,1,0)]
|> sort("x")
(sort 3d vectors by their x-component)
Sort descending:
list |> sort("-")
Only pass on positive numbers:
range(10.0)
|> sin
|> filter(x => x>0)