Skip to content

Zipping

Samarium's zip operator >< allow for easy zipping, that is, iterating through multiple iterables at once:

names: ["Alice", "Bob", "Charlie"];
ages: [/\\\\, /\\\/, ////];

... i ->? names >< ages {
    "$0 is $1 years old" --- i!;
}
Alice is 16 years old
Bob is 17 years old
Charlie is 15 years old

Enumerating iterables can be simulated by using an empty slice object:

x: ["Alpha", "Beta", "Gamma"];

... i, v ->? <<>> >< x {
    "x<<$0>> :: $1" --- [i, v]!;
}
x<<0>> :: Alpha
x<<1>> :: Beta
x<<2>> :: Gamma

<=string.ordinal;

winners: ["Jake", "Clarisse", "Matt"];

... p, w ->? <</..>> >< winners {
    "$0 place: $1" --- [ordinal(p), w];
}
1st place: Jake
2nd place: Clarisse
3rd place: Matt