Live data from Hacker News

Working pipe operator today in pure JavaScript

github.com

41–50 of 109 posts

Re: Working pipe operator today in pure JavaScript

#41
post #20
post #8

Further proof that JavaScript accidentally became the new C++. “Aren’t you surprised that this syntax works?” is not praise for a language design.

Agreed!!!! Serious q: but how does this sentiment change with LLMs? They can pickup new syntax pretty fast, then use fewer tokens...

I’ve heard it said before on HN that this is not true in general because more tokens in familiar patterns helps the model understand what it’s doing (vs. very terse and novel syntax).

Otherwise LLMs would excel at writing APL and similar languages, but seems like that’s not the case.

Re: Working pipe operator today in pure JavaScript

#42

Neat, but I think that functions already do what we need. For one thing, the example isn't the most compelling, because you can: const greeting = 'hello'.toUpperCase() + '!!!'; or const greeting = 'HELLO!!!'; That said, there is already: function thrush(initial, ...funcs) { return funcs.reduce( (current, func) => func(current), initial); } const greeting = thrush('hello', s => s.toUpperCase(), s => s + '!!!');

Are any of the cases compelling? Thinking of the actual proposal. It creates some new magic with |> and % just for syntactic sugar.

I am all for clean syntax but I feel like JS has already reached a nice middle ground between expressiveness (especially w/ map/reduce/filter) and readability. I'd personally rather not have another syntax that everyone will have to learn unless we're already moving to a new language.

Re: Working pipe operator today in pure JavaScript

#44
I love the idea! The creativity of (ab)using JavaScript type coersion is really neat. I did something similar using proxies to create a chainable API.

https://dev.to/sethcalebweeks/fluent-api-for-piping-standalo...

  const shuffle = (arr) => arr.sort(() => Math.random() - 0.5);
  const zipWith = (a, b, fn) => a.slice(0, Math.min(a.length, b.length)).map((x, i) => fn(x, b[i]));
  const log = (arr) => {
    console.log(arr);
    return arr;
  };

  const chain = chainWith({shuffle, zipWith, log});

  chain([1, 2, 3, 4, 5, 6, 7, 8, 9])
    .map((i) => i + 10)
    .log() // [ 11, 12, 13, 14, 15, 16, 17, 18, 19 ]
    .shuffle()
    .log() // e.g. [ 16, 15, 11, 19, 12, 13, 18, 14, 17 ]
    .zipWith(["a", "b", "c", "d", "e"], (a, b) => a + b)
    .log() // e.g. [ '16a', '15b', '11c', '19d', '12e' ]
    [0]; // e.g. '16a'

Re: Working pipe operator today in pure JavaScript

#45

I love the idea! The creativity of (ab)using JavaScript type coersion is really neat. I did something similar using proxies to create a chainable API. https://dev.to/sethcalebweeks/fluent-api-for-piping-standalo... const shuffle = (arr) => arr.sort(() => Math.random() - 0.5); const zipWith = (a, b, fn) => a.slice(0, Math.min(a.length, b.length)).map((x, i) => fn(x, b[i])); const log = (arr) => { console.log(arr); ret…

[deleted]

Re: Working pipe operator today in pure JavaScript

#46
post #42

Earlier quoted context omitted.

Are any of the cases compelling? Thinking of the actual proposal. It creates some new magic with |> and % just for syntactic sugar.

I am all for clean syntax but I feel like JS has already reached a nice middle ground between expressiveness (especially w/ map/reduce/filter) and readability. I'd personally rather not have another syntax that everyone will have to learn unless we're already moving to a new language.

I agree but to steelman it, what about custom functions? I think just doing it naively is perfectly fine. Or if you want use some pipe utility. Or wrap the array, string, etc. with your own custom methods.

Re: Working pipe operator today in pure JavaScript

#49

Neat, but I think that functions already do what we need. For one thing, the example isn't the most compelling, because you can: const greeting = 'hello'.toUpperCase() + '!!!'; or const greeting = 'HELLO!!!'; That said, there is already: function thrush(initial, ...funcs) { return funcs.reduce( (current, func) => func(current), initial); } const greeting = thrush('hello', s => s.toUpperCase(), s => s + '!!!');

Whatever that thrush thing is feels 10x more gross than the pipe

Re: Working pipe operator today in pure JavaScript

#50
post #33

Pipes are great in environments where "everything is a string" (bash, etc), but do we really need them in javascript? I have yet to see a compelling example.

Pipes are great where you want to chain several operations together. Piping is very common in statically typed functional langauges, where there are lots of different types in play. Sequences are a common example. So this: xs.map(x => x * 2).filter(x => x > 4).sorted().take(5) In pipes this might look like: xs |> map(x => x * 2) |> filter(x => x > 4) |> sorted() |> take(5) In functional languages (of the ML variety),…

[deleted]
Post reply on HN