Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cell array keyed
export function cellArrayKeyed2<K,A,B>(keyEq: (a:K,b:K)=>boolean, keyExtractor: (a:A)=>K, transform: (a:A) => B, cas: sodium.Cell<A[]>): sodium.Cell<B[]> {
return sodium.Operational
.updates(cas)
.accumLazy(
cas.sampleLazy().map(as => as.map(a => T2.of(keyExtractor(a), transform(a)))),
(nextAs: A[], lastResult: T2<K,B>[]) => {
let nextResult: T2<K,B>[] = [];
for (let i = 0; i < nextAs.length; ++i) {
let a = nextAs[i];
let key = keyExtractor(a);
let existingOp: Option<T2<K,B>> = Option.none();
for (let j = 0; j < lastResult.length; ++j) {
let r = lastResult[j];
if (keyEq(r._1, key)) {
existingOp = Option.some(r);
break;
}
}
if (existingOp.isSome) {
let existing = existingOp.fromSome();
nextResult.push(existing);
} else {
nextResult.push(T2.of(key, transform(a)));
}
}
return nextResult;
}
)
.map(x => x.map(x2 => x2._2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.