Skip to content

Instantly share code, notes, and snippets.

@briancavalier
Created February 3, 2016 12:45
Show Gist options
  • Select an option

  • Save briancavalier/94a08e1fd273091aaf06 to your computer and use it in GitHub Desktop.

Select an option

Save briancavalier/94a08e1fd273091aaf06 to your computer and use it in GitHub Desktop.
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