Log in
Seblog.nl

Seblog.nl

Match on the end of a pipe

This is a private copy of my post on the ElixirForum

So I have looked for this topic and found similar ones but not actually this one. Forgive me if I missed it.

While I really love Elixirs pipelines, I see myself writing this pattern from time to time:

def some_function(start) do
  result =
    start
    |> Enum.map(&do_something/1)
    |> Enum.filter(&but_without_these/1)

  {:ok, result}
end

For this pattern, people seem to have come up with operators that do things with second or last arguments (I found one of those discussions). I'm not really interested in that, I want to make another point.

In the above pattern, I start reading about a result, then I see start, and then the actions that lead to start. Your eyes notice the pipeline quite fast, you see what happens, but especially when the action after the pipeline is more complicated, you start asking yourself: wait, what did they call the result of this pipeline? To find out, you have to go all the way up to read the variable name.

Put in another way: this way of writing messes with the ordering in which things are happening. First, the start is evaluated, then the pipeline is run, and only after that the match is completed which gives result a value.

I think it would be nice to be able to write it like this:

def some_function(start) do
  start
  |> Enum.map(&do_something/1)
  |> Enum.filter(&but_without_these/1)
  >>> result

  {:ok, result}
end

This removes some indentation and reads nicely in order of what happens when. It might even stop the questions for |2>? You can just start a new pipeline after the first one with this variable where-ever you want.

Oh, and it's a pattern match, so it's quite powerful, as you know.

def some_function(start) do
  start
  |> Enum.map(&do_something/1)
  |> Enum.filter(&but_without_these/1)
  >>> [first | _]
  Enum.zip(start, first)
  |> Enum.reduce(&more_transforms/1)
  >>> result

  {:ok, result}
end

Note: I used >>> here because it's one of the available custom operators, but I think => would be prettier (but probably taken) or <| (but that's not available). Here's a very naive macro to make it work:

defmacro left >>> right do
  {:=, [], [right, left]}
end

Again, if this has been proposed too many times, please pardon the intrusion :)

Hoe groot is de kans

Ik keek net uit het raam en zag een geel busje langsrijden. Gewoon, een geel busje. Ik dacht aan hoe vet het zou zijn als daar een voertuig reed waarmee ik een diepe verbinding voelde. Ik zou dan denken: ha, kijk, dat voertuig, hoe groot is de kans dat ik uit mijn raam kijk en precies dat voertuig zie?

Ik had ook dit gele busje speciaal kunnen vinden, dacht ik toen. Ja, hoe groot is de kans dat ik precies uit het raam kijk als er een geel busje voorbij rijdt? Het punt is: er is altijd wel iets bijzonders dat voorbij rijdt. Hoe groot is de kans dat je uit het raam kijkt en een betekenisvolle verbinding kan leggen met datgene wat langsrijdt? Die nadert honderd procent.

Net toen ik tot die conclusie kwam, zag ik een gele takelwagen langsrijden. Nu ben je gewoon met me aan het fucken, wereld, dacht ik.

Dat je na duizenden kilometers reizen toch weer precies die ene millimeter vindt waar je sleutel perfect in past, waarna je ‘thuis’ bent.

Straks komt de zon op en vlieg ik met hem mee. Mijn valentijnsdag duurt 32 uur, waarvan 11 alleen in een vliegtuig. ‘s Ochtends in een land waar mensen chocola geven, ‘s avonds in een land waar mensen anoniem kaartjes sturen.

Een jongen komt binnen in het hostel. Het meisje naast me vraagt aan de hostel-eigenaresse in het Japans of het een Chinees is. Nee, het is een Koreaan. Waarop het meisje in het Koreaans tegen de jongen begint. Hostel-eigenaresse onder de indruk, ik ook. Haar Koreaans blijkt beter dan haar Engels, mijn Japans beter dan dat van hem, de hostel-eigenaresse zegt dat ze soms ook wat Koreaans snapt. ‘Solten wir dann etwas Deutsch reden?’, vraag ik voor de grap, want dat spreekt de hostel-eigenaresse ook. Alle talen door elkaar.

Je weet dat je nerd bent als je in Akihabara bijna staat te huilen bij een Pikachu-spelletje dat je wilde toen je 12 was.

Meer laden