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
@OvermindDL1: Good macro, but it’s a little bigger code, anyway good job! ![]()
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
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. ![]()
Cool. ![]()
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. ![]()
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…
Popular in Discussions
Other popular topics
Chat & Discussions>Discussions
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









