maybe.jamo (built in)

maybe: a value that might not be there, and pairs and triples for giving back more than one thing. use "maybe.jamo"

A Maybe<T> holds one value or nothing. A function that might not find anything gives back a Maybe, and the code that called it checks has() before get(), or uses or_else for a fallback:

var hit = find_first(enemies, func(e: Enemy): bool => e.health > 0)
if hit.has()
    hit.get().health -= 10
end

pair(a, b) and triple(a, b, c) bundle values, so a function can give back two or three things at once.

structMaybe

struct Maybe<T>
    items: [T]
end

funcsome

func some<T>(value: T): Maybe<T>

A Maybe holding a value

funcnone

func none<T>(): Maybe<T>

A Maybe holding nothing. Its type has to be given, like none<int>()

funcMaybe.has

func Maybe<T>.has(): bool

funcMaybe.get

func Maybe<T>.get(): T

The value. Stops with a clear error when there isn't one, so check has() first, or use or_else

funcMaybe.or_else

func Maybe<T>.or_else(fallback: T): T

The value, or fallback when there isn't one

funcfind_first

func find_first<T>(items: [T], keep: func(T): bool): Maybe<T>

The first item where keep(item) is true, or nothing

structPair

struct Pair<A, B>
    first: A
    second: B
end

structTriple

struct Triple<A, B, C>
    first: A
    second: B
    third: C
end

funcpair

func pair<A, B>(first: A, second: B): Pair<A, B>

functriple

func triple<A, B, C>(first: A, second: B, third: C): Triple<A, B, C>