7

Lists & Arrays

Working with collections using Ogunda / List

Creating Lists

List Creation
// Empty list
ayanmo empty = [];

// List with values
ayanmo numbers = [1, 2, 3, 4, 5];
ayanmo names = ["Adé", "Ọba", "Ifá"];
ayanmo mixed = [42, "hello", otito, nil];

// Nested lists (2D arrays)
ayanmo matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
// Empty list
let empty = [];

// List with values
let numbers = [1, 2, 3, 4, 5];
let names = ["Adé", "Ọba", "Ifá"];
let mixed = [42, "hello", true, nil];

// Nested lists (2D arrays)
let matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

Ogunda / List Methods

Method Description Example (Yoruba) Example (English)
len(list) Get length Ogunda.len([1,2,3]) → 3 List.len([1,2,3]) → 3
first(list) First element Ogunda.first([1,2,3]) → 1 List.first([1,2,3]) → 1
last(list) Last element Ogunda.last([1,2,3]) → 3 List.last([1,2,3]) → 3
push(list, val) Add to end Ogunda.push([1,2], 3) List.push([1,2], 3)
pop(list) Remove last Ogunda.pop([1,2,3]) List.pop([1,2,3])
contains(list, val) Check membership Ogunda.contains([1,2], 2) → otito List.contains([1,2], 2) → true

Iterating Over Lists

List Iteration
ayanmo colors = ["red", "green", "blue"];

// For-each loop
fun color ninu colors {
    Irosu.fo("Color: " + color);
}

// With index (manual)
ayanmo i = 0;
nigba (i < Ogunda.len(colors)) {
    ayanmo color = Ogunda.get(colors, i);
    Irosu.fo(i + ": " + color);
    ayanmo i = i + 1;
}
let colors = ["red", "green", "blue"];

// For-each loop
for color in colors {
    Fmt.println("Color: " + color);
}

// With index (manual)
let i = 0;
while (i < List.len(colors)) {
    let color = List.get(colors, i);
    Fmt.println(i + ": " + color);
    let i = i + 1;
}

Building Lists

Building Lists
// Build a list of squares
ayanmo squares = [];
ayanmo n = 1;

nigba (n <= 5) {
    ayanmo square = n * n;
    ayanmo squares = Ogunda.push(squares, square);
    ayanmo n = n + 1;
}

Irosu.fo(squares);  // [1, 4, 9, 16, 25]
// Build a list of squares
let squares = [];
let n = 1;

while (n <= 5) {
    let square = n * n;
    let squares = List.push(squares, square);
    let n = n + 1;
}

Fmt.println(squares);  // [1, 4, 9, 16, 25]

List Operations with Osa / Async

The Osa (or Async) domain provides parallel operations on lists:

Aggregate Operations
ayanmo numbers = [5, 2, 8, 1, 9, 3];

// Aggregate operations
Irosu.fo(Osa.sum(numbers));     // 28
Irosu.fo(Osa.min(numbers));     // 1
Irosu.fo(Osa.max(numbers));     // 9
Irosu.fo(Osa.product(numbers)); // 2160

// Sorting
Irosu.fo(Osa.sort(numbers));    // [1, 2, 3, 5, 8, 9]
let numbers = [5, 2, 8, 1, 9, 3];

// Aggregate operations
Fmt.println(Async.sum(numbers));     // 28
Fmt.println(Async.min(numbers));     // 1
Fmt.println(Async.max(numbers));     // 9
Fmt.println(Async.product(numbers)); // 2160

// Sorting
Fmt.println(Async.sort(numbers));    // [1, 2, 3, 5, 8, 9]

Practical Example: Find Average

Average Function
ise average(numbers) {
    ayanmo total = Osa.sum(numbers);
    ayanmo count = Ogunda.len(numbers);
    padapọ total / count;
}

ayanmo scores = [85, 90, 78, 92, 88];
Irosu.fo("Average: " + average(scores));  // 86.6
fn average(numbers) {
    let total = Async.sum(numbers);
    let count = List.len(numbers);
    return total / count;
}

let scores = [85, 90, 78, 92, 88];
Fmt.println("Average: " + average(scores));  // 86.6

What You Learned