Created
February 3, 2016 12:45
-
-
Save briancavalier/94a08e1fd273091aaf06 to your computer and use it in GitHub Desktop.
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 { periodic, sample, filter, multicast } from '../most'; | |
| // stream of values we want to filter | |
| // values :: Stream String | |
| const values = periodic(100, 'hi'); | |
| // filter condition. For fun, this one just periodically flip-flops | |
| // between true and false, but could be any stream of booleans | |
| // allow :: Stream boolean | |
| const allow = periodic(1000).scan(x => !x, false); | |
| const sentinel = {}; | |
| // onlyWhen :: Stream boolean -> Stream a -> Stream a | |
| const onlyWhen = (filterStream, valueStream) => { | |
| // For now, it's unfortunate that we have to resort to multicast. | |
| // We can def improve sample() to make this more succinct | |
| const values = multicast(valueStream); | |
| return filter(x => x !== sentinel, sample((x, allow) => allow ? x : sentinel, values, values, filterStream)); | |
| }; | |
| onlyWhen(allow, values).observe(x => console.log(x)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment