Skip to content
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

Closed
ichpuchtli opened this issue Dec 7, 2015 · 13 comments
Closed

Document how this would work with Arrays/Iterables/Observables #11

ichpuchtli opened this issue Dec 7, 2015 · 13 comments

Comments

@ichpuchtli
Copy link

Could you add examples of how this syntax would look with arrays/iterables and the up coming observables spec

i.e.

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)
@jonasem
Copy link

jonasem commented Dec 7, 2015

Keep in mind object observe will be removed from the spec.https://mail.mozilla.org/pipermail/es-discuss/2015-November/044684.html

@ichpuchtli
Copy link
Author

Observables !== Object.Observe

@jonasem
Copy link

jonasem commented Dec 7, 2015

Good point. My bad.

@nmn
Copy link

nmn commented Oct 11, 2017

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.

@Qard
Copy link

Qard commented Nov 8, 2017

Alternatively, we could just crib the &.property syntax from ruby. So you'd get something like:

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)

@dallonf
Copy link

dallonf commented Nov 8, 2017

I'm not sure something like this is necessary... what's the difference between this:
(Using Qard's example, adding a flatten call to demonstrate something that will make sense in a moment)

var sum = [1,2,3,4,5,6,7,8,9]
   |> &.filter(x => x % 2 == 0)
   |> &.map(x => ([x, x ** 2]))
   |> flatten
   |> &.reduce((total, next) => total + next, 0)

and this (already supported):

var sum = [1,2,3,4,5,6,7,8,9]
   .filter(x => x % 2 == 0)
   .map(x => ([x, x ** 2]))
   |> flatten // WRONG! see below
   .reduce((total, next) => total + next, 0)

oop, actually I see it now - that above code sample doesn't do what it looks like. You would actually need something like this:

var sum = [1,2,3,4,5,6,7,8,9]
   .filter(x => x % 2 == 0)
   .map(x => ([x, x ** 2]))
   |> _ => flatten(_)
   .reduce((total, next) => total + next, 0)

Still not terrible, though.

@Volune
Copy link

Volune commented Nov 9, 2017

@dallonf I think you mean

var sum = [1,2,3,4,5,6,7,8,9]
   .filter(x => x % 2 == 0)
   .map(x => ([x, x ** 2]))
   |> flatten
   |> _ => _
   .reduce((total, next) => total + next, 0)

Personally I would just use a custom library all the way to support iterables, generators, and not creating an array on each step.

import _ from 'some-lib';
var sum = [1,2,3,4,5,6,7,8,9]
   |> _.filter(x => x % 2 == 0)
   |> _.map(x => ([x, x ** 2]))
   |> _.flatten
   |> _.reduce((total, next) => total + next, 0)

@darthtrevino
Copy link

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.

const dataStream = readDataFromRemeteServerAsObservable()
  |> groupBy('category')
  |> max()

// vs what you do in RxJS currently:

const dataStream = readDataFromRemeteServerAsObservable()
  .pipe(
     groupBy('category'),
     max()
   )

@mAAdhaTTah
Copy link
Collaborator

Most relevant is #101, which talks about using Pipeline for importable methods.

@felixfbecker
Copy link
Contributor

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

@littledan
Copy link
Member

@felixfbecker PRs welcome to improve the examples.

@felixfbecker
Copy link
Contributor

@littledan #144

@littledan
Copy link
Member

Great text, thank you!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants