-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add more dependency for existing combine #183
Comments
You have a number of options but the one I'd recommend would be to use It allows you to take a Stream of Streams and get a stream. An example using Ramda's liftN function: const streams$ = flyd.stream(Array(3).fill(1).map(flyd.stream));
const sum = (...args)=> args.reduce((a,b) => a + b, 0);
const result = streams$
.chain((streams)=> R.liftN(streams.length, sum)(...streams))
result.map(console.log);
// Logs: 3
streams$(streams$().concat([flyd.stream(10)]));
// Logs: 13 |
@futurist Was the above helpful? |
@nordfjord Thanks for the example and solution, it's works great for this example. Can i understand |
You can think of combine as the low level flyd operator. It's mainly used to create other operators. e.g. filter could be implemented as: const filter = fn => stream => combine((s, self)=> {
if (fn(s.val)) self(s.val);
}, [stream]);
const number$ = stream();
const above5$ = number$.pipe(filter(n => n > 5));
above5$.map(console.log);
number$(3);
// Logs nothing
number$(7)
// Logs: 7 Map is also be implemented using combine: const map = fn => s => combine((s,self)=> self(fn(s.val)), [s]); If we think of the signatures of the functions map, and chain then map is:
And chain is:
This means chain can be used when you have a function that takes a value and returns a stream. You can think of it as |
I'm writting a lib wrap-data, and need a validation in the model, it has an const flyd = require('flyd')
const wrapData = require('wrap-data')
const data = {
firstName: 'Hello',
lastName: 'World',
age: 20,
}
const model = wrapData(flyd.stream)(data)
// model, and everything inside model is a stream!
model.set('isEmpty', flyd.combine( checkEmtpy, [
model.get('age'), model.get('firstName'), model.get('lastName')
] )) Here the need is This need some dynamic |
I'm not sure how to help you with that, I might suggest another approach. const checkEmpty = ({firstName, lastName, age})=> firstName
? firstName && lastName && age > 0
: age > 0;
const model = stream({
firstName: 'Hello',
lastName: 'World',
age: 20
});
model.isEmpty = model.map(checkEmpty);
model.isEmpty() // false
model(Object.assign({}, model(), {lastName: ''}));
model.isEmpty() // true |
Hello again @futurist I have been thinking about your problem for a while, but last night a solution hit me that might fit into your frame! import { stream } from 'flyd';
// (...any[] -> any) -> Stream Stream[] -> Stream any
const dynamicCombine = (fn, streams$)=> streams$
.chain(streams => combine(fn, streams)); Now I'm not familiar with the internals of your library, but if you're able to instead of using a |
I have a problem when use combine:
I want to push new value into
arr
, and want sum the 4 arr elements instead of 3.That is, a question, how to modify existing combine dependency?
The text was updated successfully, but these errors were encountered: