abetkin

abetkin

For syntax for pipes - proposal

Proposed syntax

[:my, :list] |> for x do
  IO.puts(x)
end

Seems natural since the following forms exist:

my_var |> case do ... end
my_cond |> if do ... end

Most Liked

peerreynders

peerreynders

Pretty it ain’t.

defmodule X do # X as in dangerously eXperimental

  defmacro pipe_for(enumerable, to, rest) do
    quote do
      unquote(
        {:for, [],
          [ {:<-, [], [to, enumerable]} | wrap_block(rest,[]) ]
        }
      )
    end
  end

  defp wrap_block([_] = block_list, gen_filters),
    do: :lists.reverse([block_list | gen_filters])
  defp wrap_block([gen_filter | rest], gen_filters),
    do: wrap_block(rest, [gen_filter | gen_filters])

end

defmodule Demo do
  import X

  def demo1() do
    [:my, :list]
    |> pipe_for(x, [do: (IO.puts x)])
  end

  def demo2() do
    other = [1,2]
    [:a, :b, :c]
    |> pipe_for(i, [j <- other, do: ({i,j})])
  end

  defp multiple_of_3?(n), do: rem(n,3) == 0

  def demo3() do
    0..15
    |> pipe_for(n, [multiple_of_3?(n), do: (n * n)])
  end

  def demo4(n) do
    1..n
    |> pipe_for(a, [
          b <- 1..n,
          c <- 1..n,
          a + b + c <= n,
          a*a + b*b == c*c,
          do: (
            {a, b, c}
          )])
  end

  def demo5() do
    other = [2,3,4]
    [1,2,3]
    |> pipe_for(x, [do: (
         other
         |> pipe_for(y, [x != y, do: (y)])
       )])
  end

end

IO.puts("-- demo1: ")
Demo.demo1()
IO.puts("-- demo 2: #{inspect Demo.demo2()}")
IO.puts("-- demo 3: #{inspect Demo.demo3()}")
IO.puts("-- demo 4: #{inspect Demo.demo4(48)}")
IO.puts("-- demo 5: #{inspect Demo.demo5()}")
 $ elixir demo.exs
-- demo1: 
my
list
-- demo 2: [a: 1, a: 2, b: 1, b: 2, c: 1, c: 2]
-- demo 3: [0, 9, 36, 81, 144, 225]
-- demo 4: [{3, 4, 5}, {4, 3, 5}, {5, 12, 13}, {6, 8, 10}, {8, 6, 10}, {8, 15, 17}, {9, 12, 15}, {12, 5, 13}, {12, 9, 15}, {12, 16, 20}, {15, 8, 17}, {16, 12, 20}]
-- demo 5: [[2, 3, 4], [3, 4], [2, 4]]
$ 

Don’t look at me, Elixir let me do it …

idi527

idi527

It’s probably a bit more difficult to implement than case. How would you express

for x <- [1, 2, 3], y <- [2, 3, 4], x != y do
  # ...
end

with a pipe?

And as @kokolegorille has already pointed out, simple for cases can be expressed with one of Enum.each, Enum.map, or Enum.reduce depending on the result that you want.

joaoevangelista

joaoevangelista

I just think we are putting pipes everywhere, where they aren’t needed, there is no need to put it on expressions, or even on one function without composition.

for x <- bar is much more convenient as other languages apply the same syntax.
maybe |> case do seems not lexical at all, case what?
myvar |> Enum.each(fn x -> x |> case do ... end end) omg.

Soon we are in the same boat as Scala, having N ways to write the same thing.

Just don’t overuse the poor pipe.

Where Next?

Popular in Discussions Top

Donovan
Hello everyone, I’m so glad to have discovered this awesome community. Thanks for creating it! This is my second post, and apologies for...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
mmport80
I have put far too much effort into Dialyzer over the last year or so - and basically - I doubt it’s worth the effort. It’s not as easy ...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
ejpcmac
I have discovered Nix last month and I am currently on my way to migrating to it—both on macOS at home and the full NixOS distrubution at...
New
eteeselink
Hi all, In the last days, two things happened: A blog post titled “They might never tell you it’s broken” made the rounds. It’s about ...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

We're in Beta

About us Mission Statement