sebsel

sebsel

Match on the end of a pipe (Matchpipe?)

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 :slight_smile:

Most Liked

OvermindDL1

OvermindDL1

There are all kinds of useful libraries and patterns, but if you want to stick with stock Elixir then I would do (and do) this:

def some_function(start) do
  start
  |> Enum.map(&do_something/1)
  |> Enum.filter(&but_without_these/1)
  |> case do result ->
    {:ok, result}
  end
end

I use that |> case do result -> ... end pattern a lot. I know that not everyone likes it but I find it significantly more readable then misplaced variable definitions. Now if only we had proper monad’y do blocks without a library. ^.^;

19
Post #2
OvermindDL1

OvermindDL1

For non-kernel things I use the Exceptional library most often, so with it then it would be:

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

But the case way is useful for ‘generic’ handling of the value without needing to make a new function (which you should always do, but I generally don’t if it’s only a line or two in size and just use the case instead). :slight_smile:

EDIT: For note, to_tagged_status/1 is a lot more than just an &{:ok, &1}.() wrapper, it returns either an :ok or :error tuple depending on the value. If the value is an exception then it wraps it’s exception message in the :error tuple. If it’s already an error or ok tuple then it passes it through unchanged.

iex(devserver2@127.0.0.1)9> 42 |> to_tagged_status()
{:ok, 42}
iex(devserver2@127.0.0.1)10> nil |> to_tagged_status()
{:ok, nil}
iex(devserver2@127.0.0.1)11> {:ok, :blah} |> to_tagged_status()
{:ok, :blah}
iex(devserver2@127.0.0.1)12> Enum.OutOfBoundsError.exception("error message") |> to_tagged_status()
{:error, "error message"}
iex(devserver2@127.0.0.1)13> {:error, :blorp} |> to_tagged_status()
{:error, :blorp}

I quite like the style of returning ok/error tuples, raw ok values, or exceptions. It is quite useful I’ve found. :slight_smile:

michalmuskala

michalmuskala

Some, including me, do consider it rather unreadable :wink:. I’d much rather see:

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

{:ok, result}

or even

{:ok,
 start
 |> Enum.map(&do_something/1)
 |> Enum.filter(&but_without_these/1)}

Where Next?

Popular in Discussions Top

mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
owaisqayum
I have a sample string sentence = "Hello, world ... 123 *** ^%&amp;*())^% %%:&gt;" From this string, I want to only keep the integers, ...
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
100phlecs
Are there any downsides, like perf issues, to putting all functional components in CoreComponents as long as you prefix it with the conte...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
kostonstyle
Hi all How can I compare haskell with elixir, included tools, webservices, ect. Thanks
New
AstonJ
It’s been a while since we’ve had a thread like this, so what better way to kick off the year with :003: What does being an Elixir user ...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement