tur/saffron/prelude
stdlib/saffron/prelude.tur
defn
vec-map-loop__
(vec-map-loop__ [v :(Vec any) f :(fn [any] any) out :(Vec any) i :int n :int] :int)
the Saffron dialect's prelude.
Since: saffron-lang-plan S6
defn
vec-map
(vec-map [v :(Vec any) f :(fn [any] any)] :(Vec any))
apply f to each element of a vector, into a fresh vector.
Parameters
| v | (Vec any), e.g. a `[...]` literal | |
| f | a one-argument function over `any` |
Returns
A fresh (Vec any) of the same length. The caller owns it.
Example
(vec-map [1 2 3] (fn [x] (* x 2))) ; => 2 4 6
Since: saffron-lang-plan S6
defn
vec-filter-loop__
(vec-filter-loop__ [v :(Vec any) pred :(fn [any] any) out :(Vec any) i :int n :int] :int)
internal: the recursive half of vec-filter.
defn
vec-filter
(vec-filter [v :(Vec any) pred :(fn [any] any)] :(Vec any))
keep the elements of a vector for which pred is truthy.
Parameters
| v | (Vec any) | |
| pred | a one-argument predicate over `any`. D4 truthiness applies, so | |
| only nil and `false` drop an element -- an empty string and a zero | ||
| are both kept. |
Returns
A fresh (Vec any) holding the kept elements, in order. The caller owns it.
Example
(vec-filter [1 2 3 4] (fn [x] (> x 2))) ; => 3 4
Since: saffron-lang-plan S6
defn
vec-fold-loop__
(vec-fold-loop__ [v :(Vec any) f :(fn [any any] any) acc :any i :int n :int] :any)
internal: the recursive half of vec-fold.
defn
vec-fold
(vec-fold [v :(Vec any) init :any f :(fn [any any] any)] :any)
left-fold a vector with an accumulator.
Parameters
| v | (Vec any) | |
| init | the starting accumulator | |
| f | (fn [acc x] acc') |
Returns
The final accumulator.
Example
(vec-fold [1 2 3] 0 (fn [a x] (+ a x))) ; => 6
Since: saffron-lang-plan S6