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

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
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
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
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
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
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

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement