Comments (7)
You have a number of options but the one I'd recommend would be to use chain
.
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
from flyd.
@futurist Was the above helpful?
from flyd.
@nordfjord Thanks for the example and solution, it's works great for this example. Can i understand chain
can be a replacement for combine
? what's the usage scenario of differents for chain
and combine
normally?
from flyd.
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:
map :: (a -> b) -> Stream a -> Stream b
And chain is:
chain :: (a -> Stream b) -> Stream a -> Stream b
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 flatMap
from flyd.
I'm writting a lib wrap-data, and need a validation in the model, it has an isEmpty
field and need to combine
other streams dynamically, like below:
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 name
is optional, if firstName
is empty then checkEmpty
will ignore all name
field. But if firstName
not empty then lastName
can not empty.
This need some dynamic combine
or chain
, add more stream or subtract some stream from the combine list.
from flyd.
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
from flyd.
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 List<Stream>
to host the "fields" of the model, and move to Stream<List<Stream>>
then this would solve your problem.
from flyd.
Related Issues (20)
- Merge stream delay HOT 6
- add package.module
- Drag 'n Drop example is broken HOT 2
- Compare previous state with current state HOT 14
- Feature Request - Supply metadata for a combined stream HOT 2
- Help to build control flow func HOT 3
- Do you think this Promise -> Stream conversion is a right alternative way? HOT 6
- Wrong chain + setImmediate behavior HOT 1
- Benchmark against Most.js HOT 3
- Can flyd streams be made lazy ? HOT 9
- HELP: Trouble reading api documentation HOT 1
- Enhancement: Add Readonly-Streams (at least in Typings) HOT 3
- What's the motivation for ending a stream? HOT 3
- question on serialize stream
- from Node stream HOT 2
- Question: Required (code) complexity for higher-order streams HOT 10
- Support multiple operators for Pipe HOT 4
- [Help Needed] CombineLatest in the style of RxJs HOT 17
- Switch Latest without Ramda Dependency HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flyd.