Created
June 18, 2019 11:15
-
-
Save clinuxrulz/90620be09cb7e367c302fe6b448a3c71 to your computer and use it in GitHub Desktop.
Cell array keyed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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