Arrays
Instance methods on arrays. All methods support chaining.
Methods
| Method | Returns | Description |
|---|---|---|
len() | int | Element count |
push(val) | Append element | |
pop() | int | Remove and return last |
first() | int | First element |
last() | int | Last element |
min() | int | Minimum value |
max() | int | Maximum value |
sum() | int | Sum of all elements |
slice(start, end) | array | Sub-array |
sort() | array | Sorted copy |
reverse() | array | Reversed copy |
unique() | array | Deduplicated copy |
concat(other) | array | Concatenate arrays |
join(sep) | string | Join with separator |
index_of(val) | int | Find element (-1 if missing) |
remove(idx) | Remove at index | |
insert(idx, val) | Insert at index | |
map(fn) | array | Transform elements |
filter(fn) | array | Filter elements |
reduce(fn, init) | int | Fold elements |
each(fn) | Call fn for each element | |
any(fn) | int | Any element matches (1/0) |
every(fn) | int | All elements match (1/0) |
sort_by(cmp_fn) | Sort with comparator |
Method Chaining
wyn
// Chain any combination of array methods
var result = [3, 1, 4, 1, 5, 9].sort().reverse()
println(result[0].to_string()) // 9
var count = [5, 3, 1, 4, 2].filter(fn(x: int) -> bool { return x > 2 }).sort().len()
println(count.to_string()) // 3
var words = "hello world foo".split(" ").sort().join(", ")
println(words) // foo, hello, worldFunctional
wyn
var doubled = nums.map(fn(x: int) -> int { return x * 2 })
var big = nums.filter(fn(x: int) -> bool { return x > 3 })
var total = nums.reduce(fn(acc: int, x: int) -> int { return acc + x }, 0)Comprehensions
wyn
var squares = [x * x for x in 0..10]
var evens = [x for x in 0..20 if x % 2 == 0]See Also
- Strings —
.join()arrays into strings - Log — structured logging
- HashMap — key-value alternative to arrays
- Control Flow —
for-inloops over arrays