abetkin

abetkin OP

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

First 10 of 11 Posts Switch mode

kokolegorille

kokolegorille

Maybe list comprehension?

for x <- [:my, :list], do: IO.puts(x)

This can also be achieved with

[:my, :list] |> Enum.each(&IO.puts/1)
abetkin

abetkin OP

Yes, I want an alternative form for list comprehension, just like |> case do and |> if do are the alternative forms for case and if

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.

abetkin

abetkin OP

I do not propose and don’t have practical need for support of complex cases like you mention. It’s a shortcut anyway. Probably, there is still a reasonable syntax for that, like

[1, 2, 3] |> for x do
  [2, 3, 4] |> for y, x != y, do: ...
end

A simple solution would be to leave 2nd for in its original form:

[1, 2, 3] |> for x do
  for y <- [2, 3, 4], x != y, do: ...
end
kokolegorille

kokolegorille

That would defeat a lot of FP rules…

  • No loop, prefer Enum
  • No mutability
  • Lists are not like Python lists, they are linked lists
idi527

idi527

[1, 2, 3] |> for x do
  [2, 3, 4] |> for y, x != y, do: ...
end

that wouldn’t work, since after do a new statement begins.

abetkin

abetkin OP

Sorry, of course that doesn’t work

Leaving the simplest form then: list |> for x do

the richer syntax probably would be list |> for x, y <- list2 do end

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 …

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.

bdubaut

bdubaut

can’t we achieve the same thing with Enum.reduce_while/3 ?

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
AstonJ
Just a general thread to post chat/news/info relating to AI/ML stuff that may be relevant for Nx now or in the future. Got anything to sh...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
juhalehtonen
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
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
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
mudasobwa
While I am working on the Language Agnostic Code Audit SaaS, which uses MetaAST (spoiler: I am expecting it to be in a good shape for ann...
New

We're in Beta

About us Mission Statement