SultanC
  1. Home
  2. Examples
  3. Collections

A slice (`[] T`) is a view of a sequence that functions can take and return

collections/slices.sn
// A slice (`[] T`) is a view of a sequence that functions can take and return.
import std.io.println;
import std.text.number;

def sum(values: [] int): int {
    let total: mutable int = 0;
    let i: mutable uint = 0;
    while (i < (length(values) as uint)) {
        total := total + values[i];
        i := i + 1;
    }
    return total;
}

def main(): int {
    let values: [] int = [10, 20, 12];
    println(int_to_text(sum(values)));
    return 0;
}

Run it

With the interpreter, without compiling:

./build/sultanc run examples/en/collections/slices.sn

Or compile it to an executable:

./build/sultanc examples/en/collections/slices.sn -o build/example && ./build/example

Source on GitHub   All examples