computation() |> &(Map.put(my_map, key, &1)).()
That said, with this less-power, you do kind of end up forced to design your functions in a way to where piping makes sense, and IMO it leads to cleaner and more consistent APIs. computation() |> &(Map.put(my_map, key, &1)).()
That said, with this less-power, you do kind of end up forced to design your functions in a way to where piping makes sense, and IMO it leads to cleaner and more consistent APIs.In Ocaml, you often end up doing things like:
List.create 0 3
|> List.map ~f:(fun x -> x+1)
|> List.fold ~init:0 ~f:(fun acc x -> x+acc)
|> Stdio.print_endline "%d"
I'm ambivalent about adding them to JS however. It's a nice feature but I don't think it works well with the rest of the syntax. computation() |> &(Map.put(my_map, key, &1))
is terrible style when Map.put(my_map, key, computation())
works just as well and is more readable. It is pretty rare to have a pipeline that needs to insert the value elsewhere than the first position. And please, do not write single element pipelines, I see them far too often from Elixir beginners.I think that the way it's done is a net-positive in designing cleaner APIs, but there are times when I've already done a pipeline, and storing the output is just the last step. This last step is just frustratingly, not always possible. I don't think one should do something like the above, it's just what you must resort to if you _did_ want to do it.
data =
this
|> is
|> a
|> pipeline
save_to_file(file, data)
Instead of trying to put the call to save_to_file into the pipeline by wrapping it in a closure.It's similar to await/async, you eventually start designing code that better suits that interface rather than pigeonholing it with complex syntax.
computation() |> then(&Map.put(my_map, key, &1))
It's the big reason the `then/2` function was created from my understanding.An aside, the implementation is kind of amusing. It almost seems unnecessary for this to be a macro but maybe the compiler can optimize this a bit more? I would expect TCO to simplify of my "simple" implementation of
def then(value, fun) do
fun.(value)
end
https://github.com/elixir-lang/elixir/blob/a64d42f5d3cb6c327...