-
Notifications
You must be signed in to change notification settings - Fork 111
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
Document how this would work with Arrays/Iterables/Observables #11
Comments
|
Observables !== Object.Observe |
Good point. My bad. |
You'd need a utility library to pull out object methods, probably. There are a few options: const thisify = fn => (...args) => context => fn.apply(context, args);
// Usage:
const Arr = Array.prototype;
var sum = [1,2,3,4,5,6,7,8,9]
|> thisify(Arr.filter)(x => x % 2 == 0)
|> thisify(Arr.map)(x => x ** 2)
|> thisify(Arr.reduce)((total, next) => total + next, 0) const thisify = fn => (...args) => context => fn.apply(context, args);
const extractMethods = (obj) => {
let methods = {};
for (let key in obj) {
if (typeof obj[key] === 'function'){
methods[key] = thisify(obj[key]);
}
}
}
// Usage:
const List = extractMethods([]); // or extractMethods(Array.prototype);
var sum = [1,2,3,4,5,6,7,8,9]
|> List.filter(x => x % 2 == 0)
|> List.map(x => x ** 2)
|> List.reduce((total, next) => total + next, 0) // Ideal Example:
Object.assign(Array, extractMethods(Array.prototype));
var sum = [1,2,3,4,5,6,7,8,9]
|> Array.filter(x => x % 2 == 0)
|> Array.map(x => x ** 2)
|> Array.reduce((total, next) => total + next, 0) I think eventually the JS built-ins should extended to include these methods statically on Array itself, without needing to use the extractMethods helper, but this is a good stop-gap. |
Alternatively, we could just crib the var sum = [1,2,3,4,5,6,7,8,9]
|> &.filter(x => x % 2 == 0)
|> &.map(x => x ** 2)
|> &.reduce((total, next) => total + next, 0) |
I'm not sure something like this is necessary... what's the difference between this:
and this (already supported):
oop, actually I see it now - that above code sample doesn't do what it looks like. You would actually need something like this:
Still not terrible, though. |
@dallonf I think you mean
Personally I would just use a custom library all the way to support iterables, generators, and not creating an array on each step.
|
I haven't seen any information yet on how this proposal works with Observables. It would be rad if you could use this to compose observable streams into a processing graph.
|
Most relevant is #101, which talks about using Pipeline for importable methods. |
I'm baffled that RxJS operators are not mentioned in the use cases section. That seems like a way bigger use case than any of the examples mentioned |
@felixfbecker PRs welcome to improve the examples. |
Great text, thank you! |
Could you add examples of how this syntax would look with arrays/iterables and the up coming observables spec
i.e.
The text was updated successfully, but these errors were encountered: