11

Parallel Processing

Concurrent operations with Osa domain

Osa Domain

Osa (Odù pattern for transformation) provides parallel operations on collections. It automatically distributes work across available CPU cores.

Aggregate Operations

Method Description Example
sum(list) Sum all numbers Osa.sum([1,2,3]) ? 6
product(list) Multiply all Osa.product([2,3,4]) ? 24
min(list) Find minimum Osa.min([3,1,2]) ? 1
max(list) Find maximum Osa.max([3,1,2]) ? 3
count(list) Count truthy Osa.count([1,0,1]) ? 2
ayanmo numbers = [10, 25, 5, 30, 15, 20];

Irosu.fo("Sum: " + Osa.sum(numbers));      // 105
Irosu.fo("Min: " + Osa.min(numbers));      // 5
Irosu.fo("Max: " + Osa.max(numbers));      // 30
Irosu.fo("Product: " + Osa.product(numbers)); // 11250000

Sorting

ayanmo unsorted = [5, 2, 8, 1, 9, 3];
ayanmo sorted = Osa.sort(unsorted);
Irosu.fo(sorted);  // [1, 2, 3, 5, 8, 9]

// Sorting doesn't modify original
Irosu.fo(unsorted);  // [5, 2, 8, 1, 9, 3]

Parallel Map

Apply a function to every element in parallel:

ayanmo numbers = [1, 2, 3, 4, 5];

// Square each number
ayanmo squares = Osa.map(numbers, |n| n * n);
Irosu.fo(squares);  // [1, 4, 9, 16, 25]

// Double each number
ayanmo doubled = Osa.map(numbers, |n| n * 2);
Irosu.fo(doubled);  // [2, 4, 6, 8, 10]

Parallel Filter

Keep only elements matching a condition:

ayanmo numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Keep even numbers
ayanmo evens = Osa.filter(numbers, |n| n % 2 == 0);
Irosu.fo(evens);  // [2, 4, 6, 8, 10]

// Keep numbers greater than 5
ayanmo big = Osa.filter(numbers, |n| n > 5);
Irosu.fo(big);  // [6, 7, 8, 9, 10]

Reduce

Combine all elements into one value:

ayanmo numbers = [1, 2, 3, 4, 5];

// Sum using reduce
ayanmo total = Osa.reduce(numbers, 0, |acc, n| acc + n);
Irosu.fo(total);  // 15

// Find max using reduce
ayanmo maximum = Osa.reduce(numbers, 0, |max, n| {
    ti (n > max) { padap? n; }
    padap? max;
});
Irosu.fo(maximum);  // 5

Chaining Operations

ayanmo data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Filter ? Map ? Reduce
ayanmo evens = Osa.filter(data, |n| n % 2 == 0);
ayanmo squared = Osa.map(evens, |n| n * n);
ayanmo total = Osa.sum(squared);

Irosu.fo("Sum of squares of evens: " + total);  // 220

Practical Example: Data Processing

// Process large dataset
ayanmo prices = [10.5, 20.0, 15.75, 8.25, 30.0, 12.5];

// Apply 10% tax
ayanmo with_tax = Osa.map(prices, |p| p * 1.1);

// Filter expensive items
ayanmo expensive = Osa.filter(with_tax, |p| p > 15);

// Calculate total
ayanmo total = Osa.sum(expensive);

Irosu.fo("Total for expensive items: $" + total);