cgarciae

cgarciae

Pipe Branching Syntax Proposal

Hi,

I want to propose an extension to the language that enables it to express branched computations using the pipe |> operator. Here is a basic example of how it could be used:

def add(a, b), do: a + b
def mul(a, b), do: a * b

...

assert [4, 7, 20] ==
  2
  |> [
    add(2),
    add(5),
    mul(10)
  ]

As you see the essence would be to be able to pipe into a list with operations, and the piped value is distributed among the operations in the list, and a list is returned. The previous would be equivalent to:

assert [4, 7, 20] ==
  [
    2 |> add(2),
    2 |> add(5),
    2 |> mul(10)
  ]

I am bringing this idea from a python library I made for TensorFlow where I implement a small DSL to achieve this. You can view an example here: GitHub - cgarciae/tensorbuilder: TensorBuilder is a TensorFlow library enables you to easily create complex deep neural networks by leveraging the phi DSL to help define their structure. · GitHub

Most Liked

Eiji

Eiji

@OvermindDL1: Good macro, but it’s a little bigger code, anyway good job! :slight_smile:
Today, I have more time so I decided to at least try to answer for as much unanswered questions as possible (no matter if old or not).
btw. It could be helpful if I could sort by 2 or more columns … For example firstly sort by replies count and then sort by category.

OvermindDL1

OvermindDL1

Yep, but it gives a more natural piping syntax. Could even change it up more too, like by another operator or something, or just parenthesis ‘sub-piping’ or so. :slight_smile:

Cool. :slight_smile:

The discourse API is pretty simple (simple JSON endpoints) so no capability sadly. We need a new Discourse, written in Elixir, that uses GraphQL instead of plain JSON. :wink:

OvermindDL1

OvermindDL1

Could do a wrapper around the pipe too:

defmodule EnhancedPipe do
  defmacro enhanced_pipe(ast) do
    Macro.postwalk(ast, fn
      {:|>, meta, [val_ast, fun_list]} when is_list(fun_list) ->
        v = Macro.var(:v_enhanced_pipe, __MODULE__)
        fun_ast =
          quote do
            case do
              unquote(v) -> unquote(do_list_pipe(v, fun_list))
            end
          end
        {:|>, meta, [val_ast, fun_ast]}
      ast -> ast
    end)
  end
  defp do_list_pipe(val_ast, []), do: []
  defp do_list_pipe(val_ast, [applicitive_ast | rest_asts]) do
    [ quote do
        unquote(val_ast) |> unquote(applicitive_ast)
      end
    | do_list_pipe(val_ast, rest_asts)
    ]
  end
end

Used like:

iex> import EnhancedPipe
EnhancedPipe
iex> enhanced_pipe(
...> 2
...> |>[
...>   add(2),
...>   add(5),
...>   mul(10),
...>   ]
...> )
[4, 7, 20]
iex> enhanced_pipe(
...> 2
...> |>[
...>   add(2),
...>   add(5),
...>   mul(10),
...>   ]
...> |> Enum.map(&mul(&1, 2))
...> )
[8, 14, 40]

Or could just outright replace the pipe too with a macro…

Where Next?

Popular in Discussions Top

lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Crowdhailer
I’ve been hearing much about the new formatter and it’s something I have been keen to try. I find examples buy far the most illuminating...
248 19148 150
New
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New
jesse
Hi everyone, I hesitated to post this here because I don’t want you to think I’m spamming, but I’ve been working on a Platform-as-a-Serv...
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I'm a nov...
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