data-collectors
Version:
A collection of composable reduction operations for arbitrary streams of values
11 lines (9 loc) • 389 B
text/typescript
import { Collector } from "../collector";
export function first<T> () : Collector<T, { value: T }, T> {
return Collector.of(
() => ( { value: null } ),
( state, item ) => state.value === null ? state.value = item : void 0,
( stateA, stateB ) => ( { value: stateA.value != null ? stateA.value : stateB.value } ),
state => state.value
);
}