until
Module Export
[value] |> [value]Returns an iterator that yields items until a predicate returns true (inclusive)
The until function creates an iterator that yields items from the input iterator until the predicate returns true. The item that matches the predicate is included in the output (inclusive stop). If the predicate never matches, all items are yielded.
This is useful for implementing early termination patterns, such as stopping processing when an error is encountered.
Examples
Stop when condition is met
import 'iterators'
from [1, 2, 3, 4, 5] until x => x > 3Result:
[1, 2, 3, 4]Note: 4 is included because it’s the item that matched the condition.
Using with pattern matching (shorthand)
The until function supports shorthand pattern matching syntax, similar to where:
import 'iterators'
from ['text', 2, true, 3] until is numberResult:
['text', 2]Note: 2 is included because it’s the first item that matched the pattern.
Using with pattern matching (lambda)
import 'iterators'
let Error: { error: text }
let results = [ { value = 1 }, { value = 2 }, { error = 'failed' }, { value = 3 }]
from results until r => r is ErrorResult:
[{ value = 1 }, { value = 2 }, { error = 'failed' }]Early return on error pattern
Combined with last, you can implement a pattern that processes items sequentially and returns the first error encountered, or the final result if all succeed:
import 'iterators'
let Error: { error: text }
let operations = [ { value = 1 }, { value = 2 }, { error = 'something went wrong' }, { value = 4 }]
from operations until is Error lastResult:
{ error = 'something went wrong' }If no error occurs:
import 'iterators'
let Error: { error: text }
let operations = [ { value = 1 }, { value = 2 }, { value = 3 }]
from operations until is Error lastResult:
{ value = 3 }