Arrays & Higher-Order Functions

Arrays in Djazair are dynamic, resizable lists that can hold values of any type. They come packed with rich functional programming methods like map, filter, reduce, find, every, and some.

Creating & Modifying Arrays

djazair
# Array initialization
let list = [10, 20, 30]

# Adding and removing elements
list.append(40)         # [10, 20, 30, 40]
list.insert(1, 15)      # [10, 15, 20, 30, 40]
let last = list.pop()   # last is 40, list is [10, 15, 20, 30]
list.remove(15)         # removes value 15 => [10, 20, 30]

# Element update & arithmetic
list[0] += 5            # list[0] is now 15

Functional Transformations (HOFs)

Djazair provides powerful higher-order functions that take callbacks or arrow functions:

djazair
let numbers = [1, 2, 3, 4, 5, 6]

# 1. map — transform each element
let doubled = numbers.map(fn(x) => x * 2)
# => [2, 4, 6, 8, 10, 12]

# 2. filter — select matching elements
let evens = numbers.filter(fn(x) => x % 2 == 0)
# => [2, 4, 6]

# 3. reduce — aggregate array into a single accumulator
let sum = numbers.reduce(fn(acc, x) => acc + x, 0)
# => 21

# 4. Method Chaining
let chainedResult = numbers
    .filter(fn(x) => x > 2)
    .map(fn(x) => x * 10)
    .reduce(fn(acc, x) => acc + x, 0)

print("Chained result: ${chainedResult}") # => 180

Search & Predicate Methods

djazair
let users = [
    {"name": "Alice", "age": 22},
    {"name": "Bob", "age": 17},
    {"name": "Charlie", "age": 30}
]

# find — returns the first item matching predicate
let adult = users.find(fn(u) => u["age"] >= 18)
print("First adult: ${adult["name"]}") # => Alice

# every — checks if all items satisfy condition
let allAdults = users.every(fn(u) => u["age"] >= 18)
print("All adults? ${allAdults}") # => False

# some — checks if any item satisfies condition
let hasMinor = users.some(fn(u) => u["age"] < 18)
print("Has minor? ${hasMinor}") # => True

Statistical & Utility Methods

djazair
let scores = [10, 5, 20, 15, 10]

print("sum: ${scores.sum()}")       # => 60
print("max: ${scores.max()}")       # => 20
print("min: ${scores.min()}")       # => 5
print("unique: ${scores.unique()}") # => [10, 5, 20, 15]

# In-place sorting and non-destructive sorted()
let letters = ["c", "a", "b"]
print(letters.sorted())             # => ["a", "b", "c"]