lib/stream.py)
Pull-based synchronous “streams”: lazy chains terminated by stream.fold or
stream.subscribe. There are no Promises/Futures/async
in SPL—everything runs inside the interpreter on the caller’s stack.
Load extensionless (reqFileExtension = False):
use stream;.
SPLStream wrapping a callable that, when invoked
(producer()), returns a fresh iterable of items.
Each terminating call (fold / subscribe)
obtains a brand-new iterable from every upstream lazy stage.
stream.from_range(start, end, step) emits integers inclusively using a while-loop
generator (step > 0: while cur <= end;
negative step mirrors with while cur >= end).
Inputs are truncated with int(float(x)). Step zero raises.
stream.from_list(array) freezes a shallow copy (tuple(list)).
null behaves like an empty list.
map, filter, take) build a new upstream producer that drains the predecessor each time iteration starts.
take uses itertools.islice(upstream_iter, n) with max(0, int(float(n))).
function.ref(name) values. Predicates reuse interpreter truthiness
(Interpreter._spl_logical_bool must equal 1 to keep an item during
filter).
stream.map(stream, func) invokes func(value) via
function.name machinery for each element.
stream.filter(stream, pred) emits only values where pred returns logically true.
stream.fold(stream, initial, reducer) synchronously consumes the iterable and applies
accumulator = reducer(accumulator, item) for each emitted value, returning the final accumulator
(same thread; not a deferred Promise).
stream.subscribe(stream, callback) discards outputs but runs the sink callback per item then returns null.
use stream;
list.createList([1, -2, 3], Nums);
functionDefine.pos(v):
return.number(test.isGreater(v, 0));
end;
functionDefine.dbl(v):
return.number(math.multiply(v, 2));
end;
functionDefine.accumSum(acc, x):
return.number(math.add(acc, x));
end;
s.setVar(stream.from_list(Nums));
s.setVar(stream.filter(s, function.ref(pos)));
s.setVar(stream.map(s, function.ref(dbl)));
t.setVar(stream.take(s, 5));
Tot.setVar(stream.fold(t, 0, function.ref(accumSum)));
print.number(Tot);
Implementation reference: someProgrammingLanguage/lib/stream.py.