cjen07
parameterized pipe in elixir: |n>
edit: negative index in |n> and mixed usage with |> are supported
example:
use ParamPipe
def foo(a, b, c) do
a*2 + b*3 + c*4
end
def bar0() do
100 |> div(5) |> div(2) # 10
end
# negative n in |n> is supported
def bar1() do
1 |0> foo(0, 0) |1> foo(0, 0) |-1> foo(0, 0) # 24
end
# mixed usage with |> is supported
def bar2() do
1
|> foo(0, 0) # 2
|1> foo(0, 0) # 6
|> div(2) # 3
|> div(3) # 1
|2> foo(0, 0) # 4
|> (fn x -> foo(0, 0, x) end).() # 16
|-1> foo(0, 0) # 64
end
code: GitHub - cjen07/param_pipe: parameterized pipe in elixir: |n> · GitHub
what do you think of this? ![]()
previous tricks: 3 meta-programming demos: prefix, memorization, julia port
Trending in Announcing
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub.
Docs are at OpenaiEx User Gu...
New
WebAuthnLiveComponent WebAuthnComponents
See this post about renaming the package.
Passwordless authentication for Phoenix LiveView app...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Castle is a package I put together to assist with building Elixir releases capable of being live-upgraded via the ‘official’ release hand...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
What IDE or editor are you using for Elixir development?
Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
It is difficult to exaggerate how much discussion has been had with respect to modifying the pipe operator. The consensus response is pretty negative to such modifications, and to be honest this is a great example why.
This is completely indecipherable. Obviously the
foofunction name doesn’t help, but the whole point of the pipe operator is that there is a consistent view on what constitutes the primary noun for each function.cjen07
why?
|n>just passes the previous result as the nth parameter to the function it pipes to.If you want to pass the content directly to
File.write!, you can usecontent |1> File.write!("path").NobbZ
Instead of
content |1> File.write!(path)I do prefercontent |> to_file(path).When I first read about a pipe that allows to pipe into an arbitrary position, I really was keen of the idea, but after a couple of years dealing with the default pipe, I started to learn to write helper functions, which make my intend much more clear, than a number that no-one ever sees on a quick read inbetween
|and>. Even worse, people are so used to|>nowadays, that they will read|x>as|>! At least if they aren’t using fonts and editors that aid to differ both.cjen07
I agree that
|>is more popular and more straightforward, and works pretty cool with helper functions. I am too lazy to write helper functions which is used just once, for all you need to do is to add a number between|and>. I am working on its mixed usage with the original pipe operator.NobbZ
I need to do even more! As
ParamPipecurrently works, I have touseit (ok, acceptable) and give upKernel.|>/2completely! The latter is a nogo to me, as the visual difference between|1>and|0>is not high enough for me.And the point I wanted to make, is not the helper function perse, but in fact, that you can often come up with much better readability by using helper functions. My version makes totally clear what happens, while with your
ParapmPiped version, I have to realise, that it is parampiped, and I have to remember what the argument with the number1does. I even have to remember that you are indexing by 0, when it is much more common to refer to positional arguments as the first, second, third, etc instead of their index…mwgkgk
I like it: elegant and concise solution to a common problem. It can coexist with regular pipes and the eye immediately catches when a special syntax is used. I believe (some way of doing) this will become standard in the future languages.
While some of the cases when you want to pipe to a non-first argument would benefit from changing the argument order (the standard-ness of data as first argument eases the cognitive load), there are some examples where you want this. E.g. GitHub - smpoulsen/focus: Lightweight Elixir lenses · GitHub (lenses) project recently introduced another set of function heads: Add function heads for view/set/over to allow pipeline-able versions · smpoulsen/focus@1da55c7 · GitHub - to combat just this. Notice how the author expresses his doubts about ambiguity of such approach. Making it easy to pipe to any argument (without using an ugly inline function) would generalize a solution to this problem.
cjen07
I just upgraded the code,
|n>mixed usage with|>is supported.benwilson512
Every line has to be re-interpreted. You have to mentally move everything around to figure out what’s going where. Reading this is a matter of doing:
Notice that by the end, we’re reading from right to left, inside to out, which is precisely the whole problem that the pipe was designed to eliminate! At least with
foo(0, 0, foo())I can keep reading positions as normal.This has been discussed more times and with more posts than I can count, and while that isn’t your fault, it does mean that I just don’t have the energy to have this conversation again. For anyone considering pursuing this further please go read the other posts on this on the forum and the elixir lang core mailing list.
cjen07
At least when I read this line, I know
foohas three arguments and I am piping in the first one. Perhaps, thisfoofunction is a good example to illustrate the problem of readability.I have not read any conversations on modifications of
|>, what I know is,1: It is impossible to define an arbitrary infix operator in elixir not by recompiling it,
2: I love the syntax of
|>, so I want to keep it the most similar way as far as I can by redefining|.3: The third argument of
Macro.pipeis always 0, except inmacro_test.exsfile.I aim not to argue on readability, just a nontrivial solution to a problem in elixir I think.
OvermindDL1
If I were to do something like this I’d have the syntax as something like:
Which is valid syntax and is syntax I’ve made but I don’t use since it is not ‘standard’ anyway, plus it is not that hard to just do
1 |> (&foo(0, &1, 0)).()or so, as ugly as it is. Maybe just a helper macro to re-shuffle args or so would be cleaner or something.Honestly, if I were to buff pipe, adding some ‘put in this place here’ would be nice, but I’d prefer something of a more monad’y composition as error handling then would become so much easier.