Created
February 5, 2018 16:32
-
-
Save Willmo36/ab3865d8746014f86b1191d34c6b4bce to your computer and use it in GitHub Desktop.
distinctUntilChange - TypeScript extend Most.js
This file contains hidden or 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
import { Stream } from "most"; | |
import * as R from "ramda"; | |
declare module "most" { | |
interface Stream<A> { | |
distinctUntilChange<T>(seed: T): Stream<T>; | |
} | |
} | |
const pairs = <P, C>(prev: P, current: C) => ({ seed: current, value: [prev, current] }); | |
function distinctUntilChange<T>(stream: Stream<T>, seed: T) { | |
return stream | |
.loop<T, T[]>(pairs, seed) | |
.filter(p => !R.equals(p[0], p[1])) | |
.map(p => p[1]); | |
} | |
Stream.prototype.distinctUntilChange = function<T>(seed: T) { | |
return distinctUntilChange(this, seed); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment