2 ms·
The pipe operator relies on the first argument being the subject of the operation. A lot of languages have the arguments in a different order, and OO languages
by hinkley 2y ago
The pipe operator relies on the first argument being the subject of the operation. A lot of languages have the arguments in a different order, and OO languages sometimes use function chaining to get a similar result.
- sparkie 2y agoYou could make use of `flip` from Haskell. flip :: (x -> y -> z) -> (y -> x -> x) flip f = \y -> \x -> f x y x |> (flip f)(y) -- f(x, y)
- Terr_ 2y agoIIRC the usual workaround in Elixir involves be small lambda that rearranges things: "World" |> then(&concat("Hello ", &1)) I imagine a shorter syntax could someday be possible, where some special placeholder expression could be used, ex: "World" |> concat("Hello ", &1) However that creates a new problem: If the implicit-first-argument form is still permitted (foo() instead of foo(&1)) then it becomes confusing which function-arity is being called. A human could easily fail to notice the absence or presence of the special placeholder on some lines, and invoke the wrong thing.
- hinkley 2y agoYeah I really hate that syntax and I can’t even explain why so I kind of blot it out, but you’re right. My dislike does improve my test coverage though, since I tend to pop out a real method instead.
- freehorse 2y agoYeah, R (tidyverse) has `.` as such a placeholder. It is useful but indeed I find the syntax off, though I find the syntax off even without it, anyway. I would rather define pipes as compositions of functions, which are pretty unambiguous in terms of what arguments they get, and then apply these to whatever i want.
- agent281 2y agoLast time I checked (2020) there were already a few rejected proposals to shorten the syntax for this. It seemed like they were pretty exasperated by them at the time.
- BoingBoomTschak 2y agoSwiss arrows ftw! https://github.com/rplevy/swiss-arrows https://github.com/rplevy/swiss-arrows https://github.com/hipeta/arrow-macros https://github.com/hipeta/arrow-macros