Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Can't wait for this. Pipes are awesome in Elixir and bringing them to JS/TS will be great.

To me this is both concise and readable:

    const weather = `https://api.weather.gov/gridpoints/TOP/31,80/forecast`
        |> await fetch(%)
        |> await %.json()
        |> %.properties.periods[0]


While pipes are great in Elixir I think it’s important to look at the total cost to adding any new syntax in the context of the language that is considering them.

In the JS ecosystem, idiomatic nested function calls are read from the inside out. And in most cases that is highly readable because a single layer of nesting is all you need.

This proposal flips that on its head, so you have to retrain yourself to read code in a new way based on the presence of a |>. That adds significant cognitive overhead, especially in code that isn’t well written or that is already complex.

Your example looks nice, but also just as nice is the .then() syntax you could have used.

Now we’ll see code bases with both styles. You’ll have some team members who love pipes so much they’ll never call a function the old way doing x |> console.log and the rest of the team doing console.log(x).

Sometimes, limitations are a good thing.


This is not elixir pipes. Elixir pipes are sound because they are piping to a function. Piping to a statement is not the same thing and it will become more clear in actual usage just how bad these are. Remember % in js is also modulo, and "test" % "ing" returns NaN. NaN is infectious since basically anything that operates on NaN returns NaN. "test" % "ing" + "ana" returns NaNana . You can quickly see how these pipes (as implemented) are so bad they might as well be language sabotage.


That is concise, readable, and does not have any room at all for error handling. If this was Rust, it could at least be turned into something which returned the right Err for what was happening.

Without something like that, trying to add error handling to the things which may blow up would instantly turn it into gibberish. Every single function here can fail (the HTTP request could fail, the data returned could be unparseable as JSON, or not have the right format). We should be trying to make our languages encourage us to write code which can handle those errors naturally, rather than encouraging us to write fragile code.


That ignores the “let it crash” motto of the BEAM. Don’t bloat the code with error handling when the supervisory tree can retry crashed processes.


But it allows a fully functional style, and so you can do railway oriented programming[0], where every function has a success and failure route. The top level code looks the same as that without error handling.

[0] https://fsharpforfunandprofit.com/rop/


    Uncaught TypeError: Cannot read properties of undefined (reading 'periods')


   :3,$s/\./?./g
Solves all your problems without having to think.


periods could be null. So you would need to optional chain the array access also.


Came here to say this. I am learning Elixir on and off, and I think pipes are amazing.


It’s pretty great in R too




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: