Live data from Hacker News

The JavaScript Pipeline Operator

yanis.blog

51–60 of 63 posts

Re: The JavaScript Pipeline Operator

#51
post #32
post #4

ES6 was needed, but I think js should hit the breaks for new language features for a while.

I disagree. ES6 made JS much more palatable but it's still behind most modern languages in terms of number of features and in velocity of new features getting introduced. Javascript has actually been extremely slow to get new features compared to other languages. Since ES6 (3 years ago), the only major, mainstream feature that has been added to JS has been async functions, which are awesome, but in that time Python (…

> I disagree. ES6 made JS much more palatable but it's still behind most modern languages in terms of number of features and in velocity of new features getting introduced. Javascript has actually been extremely slow to get new features compared to other languages.

JS has been adding features at a tremendous rate. Several major features land every year. Adding something like async generators is a huge change that affects large parts of the language.

If the Go, Ruby, or Python guys want to add a feature, they just code up a proposal and add it if the maintainers like it. JS has 4 major implementations and a dozen or so other reasonably popular implementations. Adding a feature in a way that works with all of them (without breaking 25 years worth of applications) is hard.

Reason, Elm, and Purescript are not good comparisons. The features they add (especially the type system) aren't likely to ever be JS features (if they are even possible in the language).

Re: The JavaScript Pipeline Operator

#52
post #51
post #32

Earlier quoted context omitted.

I disagree. ES6 made JS much more palatable but it's still behind most modern languages in terms of number of features and in velocity of new features getting introduced. Javascript has actually been extremely slow to get new features compared to other languages. Since ES6 (3 years ago), the only major, mainstream feature that has been added to JS has been async functions, which are awesome, but in that time Python (…

> I disagree. ES6 made JS much more palatable but it's still behind most modern languages in terms of number of features and in velocity of new features getting introduced. Javascript has actually been extremely slow to get new features compared to other languages. JS has been adding features at a tremendous rate. Several major features land every year. Adding something like async generators is a huge change that aff…

You say "Several major features land every year.", but you don't back that up.

In 2018 the major features we got object rest/spread and asynchronous interation. Rest/spread is a big feature that I forgot to mention, but async iteration is only useful in a few situations, I'd call it a minor feature.

In 2017 we got async/await, and shared memory/atomics. Only a few fringe code bases will use shared/memory/atomics, so I'd call async/await the only major feature.

In 2016 we got 0 major features. The only things in the entire spec (an entire year of language progress) was Array.includes and an exponent operator (). Both of those are very clearly "minor" features.

So in the last 3 years, I'd say we've had 2 or 3 major, mainstream features, that's very far from "several major features every year". Especially 2016 where there were 0 major features.

Re: The JavaScript Pipeline Operator

#53
post #26

You could just use ramda https://ramdajs.com/docs/#pipe Not quite as seamless and it would be nice to have |> but ultimatles introduces a lot of complexity. The only good reason I could see is if there is a performance benefit a JavaScript JIT could take advantage of by having the |> operator?

Honestly, I don't think so. More likely this will be just a syntactical sugar.

Performance benefits likely depend on which side of the partial application / currying coin the operator ends up landing on. Some of the partial application proposals would potentially reduce the needs for currying in the language, which would reduce the needs for a lot of variable closures in some libraries.

The biggest potential benefit is to typing (in Typescript or Flow), which potentially can provide potentially much better or at least much simpler typing and type inferencing with an operator versus many of the alternatives (such as variadic pipe() functions).

Re: The JavaScript Pipeline Operator

#54

You don’t need lodash or a new operator for the example provided: departments .flatMap(d => d.employees) .map(e => e.salary) .reduce((p, v) => Math.max(p, v), 0) That said, the pipe operator is nice because it allows similar chaining patterns on subjects that are not arrays, as shown in the proposal’s README: https://github.com/tc39/proposal-pipeline-operator/blob/mast...

That's all well and good ( increasing readability ) but the problem remains that each step has to finish before the next step can begin... sometimes the entire dataset won't fit into memory/machine/whatever... More useful, IMHO, would be a way to EASILY compose a true pipeline: const _pipe = (a, b) => (arg) => b(a(arg)), pipe = (...ops) => ops.reduce(_pipe) ...but have the behavior work like unix pipes ( a stream ),…

Have you seen https://github.com/labs42io/itiriri? It does lazy queries on iterables, like IEnumerable from C#.

    import { query } from 'itiriri';

    function* fibonacci() {
       let [a, b] = [0, 1];

      while (true) {
        yield a;
        [a, b] = [b, a + b];
      }
    }

    // Finding first 3 Fibonacci numbers that contain 42
    const result = query(fibonacci())
      .filter(x => x.toString().indexOf('42') !== -1)
      .take(3);

    for (const e of result) {
      console.log(e);
    }

    // outputs: 514229, 267914296, 7778742049

Re: The JavaScript Pipeline Operator

#55
post #3

rxjs is already available and allows to perform chained transformations by using wide variety of methods.

Good point. I've heard about it but never could quite understand if it's useful enough. Can you share some public repos where it's being used?

I've been converting our eCommerce site to using it. It's OK. Way too complicated for the average developer, it's full of jargon that is not usually present in JS/web development. The use cases they have on their site do not reflect how it is actually used, I spent a lot of time wondering how to translate their examples of clicks and timers into my problems of API calls and route changes.

The more I think about how I use it, the more I realize I'm using it to clean up the leaky abstractions from our backend. If our backend was better, promises would meet my needs fully - get the data, stick it in the views. But no. I have to do all these bullshit transformations and call multiple APIs because apparently Java Spring apps are EXTREMELY DIFFICULT to develop...

Re: The JavaScript Pipeline Operator

#56
post #54

Earlier quoted context omitted.

That's all well and good ( increasing readability ) but the problem remains that each step has to finish before the next step can begin... sometimes the entire dataset won't fit into memory/machine/whatever... More useful, IMHO, would be a way to EASILY compose a true pipeline: const _pipe = (a, b) => (arg) => b(a(arg)), pipe = (...ops) => ops.reduce(_pipe) ...but have the behavior work like unix pipes ( a stream ),…

Have you seen https://github.com/labs42io/itiriri ? It does lazy queries on iterables, like IEnumerable from C#. import { query } from 'itiriri'; function* fibonacci() { let [a, b] = [0, 1]; while (true) { yield a; [a, b] = [b, a + b]; } } // Finding first 3 Fibonacci numbers that contain 42 const result = query(fibonacci()) .filter(x => x.toString().indexOf('42') !== -1) .take(3); for (const e of result) { console.l…

I wrote a lib that does this too. It's been a while, but using generators tended to be way slower than just using arrays, except in the most obvious cases (array of 1000000 elements, only take 5, no sorting involved, etc). Maybe that's changed. It's been a while since I've checked.

Re: The JavaScript Pipeline Operator

#57
post #8

>The difference is in how we read it. With pipeline operator data flows from left to the right, and thus making it much more comprehensible without the need to introduce extra variables. Why you need operator? Can't it be done with a function? pipe(64,Math.sqrt)

[deleted]

Re: The JavaScript Pipeline Operator

#58
post #52
post #51

Earlier quoted context omitted.

> I disagree. ES6 made JS much more palatable but it's still behind most modern languages in terms of number of features and in velocity of new features getting introduced. Javascript has actually been extremely slow to get new features compared to other languages. JS has been adding features at a tremendous rate. Several major features land every year. Adding something like async generators is a huge change that aff…

You say "Several major features land every year.", but you don't back that up. In 2018 the major features we got object rest/spread and asynchronous interation. Rest/spread is a big feature that I forgot to mention, but async iteration is only useful in a few situations, I'd call it a minor feature. In 2017 we got async/await, and shared memory/atomics. Only a few fringe code bases will use shared/memory/atomics, so…

In 2016, browsers were still working on implementing es2015 (I'm still waiting for proper tail calls on non-Safari engines).

Atomics is a huge feature (especially in the amount of work required). getOwnPropertyDescriptors is also a big addition.

Async iteration is a very big deal that can potentially affect things like reading files in node. Object spread seems big, but is actually far more simple as it is a syntactic special case of Object.assign(). In contrast, the far reaching repercussions of adding async iteration and the implementation are both large things. Given the level of optimization for JS regex, adding a bunch of new features there is also a big job. Promise finally is also a big update (though combining map and flatmap in promises automatically is the biggest issue with them).

This year, there's integers, working imports, and big class upgrades in the works. The list of major proposals is rapidly shrinking.

Is there another major language adding that many big features in the past three years? Keep in mind that most stage 3 proposals already have at least one implementation already done.

Re: The JavaScript Pipeline Operator

#59

My only real criticism, is it would be nice if it supported generators/promises as an alternative to for-await syntax.

IxJS [1] already has pipeable functions for working with AsyncIterable (the underlying interface for the proposed for-await-of syntax).

[1] https://github.com/ReactiveX/IxJS

Re: The JavaScript Pipeline Operator

#60
post #8

>The difference is in how we read it. With pipeline operator data flows from left to the right, and thus making it much more comprehensible without the need to introduce extra variables. Why you need operator? Can't it be done with a function? pipe(64,Math.sqrt)

And if you have 10 of them in a row then you have: pipe(pipe(pipe(pipe(pipe(pipe(pipe(pipe(pipe(pipe(64,sqrt),add),divide),subtract,....) I don't know about you, but I wouldn't want to debug that.

Javascript functions can have variable number of arguments.

    function pipe(...args) { }

    pipe(64,sqrt,add,divide,subtract)
Post reply on HN