Comments (4)
Does this not work?
function operator1(source) {
return source.pipe(map(x => x * 3), filter(x => x % 2 == 0));
}
function operator2(source) {
return map(x => x + 100)(source);
}
from([1,2,3]).pipe(operator1, operator2);
from ixjs.
@trxcllnt yes that does work.
I suppose I could either
- just use your operator1 style, and still use pipe for a single operator
- have the abstract operator function return either op() or [opA(),opB()] and use lodash flatten(castArray(operator())) the array before calling pipe(...operators).
I don't find your operator2 style intuitive. But if I was to adopt that, I would expect
pipe(map(x => x + 100))(source)
to work, but it has to be
pipe(source,map(x => x + 100))
If you're not going to have chaining operators on the source object, then being able to use standalone pipe() recursively within chaining pipe() would make a smooth experience.
For now, this achieves what I need :
const { from, pipe } = require('ix/asynciterable');
const { map,filter } = require('ix/asynciterable/operators');
const _ = require('lodash');
function joinOps(...ops) {
return _.flatten(ops);
}
(async function() {
try {
function operator1() {
return [
map(x => x * 3),
filter(x => x % 2 != 0)
];
}
function operator2() {
return map(x => x + 100);
}
function logger(data) {
console.log(data);
return data;
}
try{
let operators = [operator1,operator2];
operators = _(operators).map(op => op()).flatten().value();
let result = await from([1,2,3]).pipe(...operators).forEach(logger);
console.log('done');
}
catch(err) {
console.error('pipeline failed with error:', err);
}
console.log('finished');
} catch(e) {
console.log(e);
} finally {
process.exit();
}
})();
from ixjs.
The style I described is how all the Ix operators are implemented; functions which return functions that take a source iterable and return a result iterable. pipe()
is a specialization of left-fold over a list of functions with the signature (source: TSource) => TResult
.
It sounds like you want a curried version of pipe, which is valid but straightforward to write yourself:
// could also be:
// const compose = (...ops) => (source) => source.pipe(...ops);
const compose = (...ops) => (source) => ops.reduce((source, op) => op(source), source);
function operator1() {
return compose(map(x => x * 3), filter(x => x % 2 == 0));
}
function operator2() {
return map(x => x + 100);
}
from([1,2,3]).pipe(operator1(), operator2());
from ixjs.
Thanks for that, I haven't had enough functional experience to write that quickly.
Yes, your compose() is what I was expecting from the standalone pipe().
This can't be a special need - could you add something like compose to the library?
from ixjs.
Related Issues (20)
- Adding a helper module for bringing in everything HOT 3
- What happened to the function signature on reduce (and scan)? HOT 4
- takeUntil should complete iterator immediately
- docs(readme): `Array#extras` link is defunct HOT 2
- Spurious 'unhandledRejection' events HOT 1
- Weird memory behaviour with merge HOT 1
- async timeout operator does not pass through its input HOT 2
- Merge with iter-tools? HOT 3
- Typescript >= 4.3.2 HOT 1
- [Suggestion]: Update `@types/node`
- vite environment ix package load error
- Usage with Skypack CDN? HOT 2
- ES modules do not work HOT 2
- Build errors with Typescript 4.7.4 HOT 4
- Take while inclusive
- Operator flat concurrency issue with async iterables HOT 7
- from with abort signal HOT 1
- merge function can cause unhandled rejected promise condition
- async iterable merge function that ends upon the first "done" HOT 1
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 ixjs.