sodapopcan
Improve this `unpipe` function?
I go through bouts of playing around with re-writing source-code using Sourceror. It generally goes: I learn a whole bunch, start getting comfortable with it, stop doing it for many months and forget everything ![]()
I’m back at it and just finished creating a function that will inline single pipes. IE:
socket
|> assign(:foo, "foo")
becomes:
assign(socket, :foo, "foo")
I was looking to get some feedback on my solution.
I initially thought I could get away with simply pre- or postwalking with some clever pattern-matching, but that proved to be difficult for me. I ended up using a zipper which made life a LOT easier getting me to a solution quite quickly. There are still a few issues with line-length but I’m otherwise quite happy with the clarity of it.
Still, I’m wondering:
- Is this possible using walking with an accumulator?
- Do you have a solution that’s different/better than mine?
- Do you have any other feedback?
TIA
def unpipe(ast) do
Sourceror.Zipper.zip(ast)
|> Sourceror.Zipper.traverse(fn
%{node: {:|>, _, _} = node} = zipper ->
prev = Sourceror.Zipper.prev(zipper)
next = Sourceror.Zipper.next(zipper)
with false <- match?(%{node: {:|>, _, _}}, prev),
false <- match?(%{node: {:|>, _, _}}, next),
{:|>, _, [var, {func, meta, args}]} <- node do
Sourceror.Zipper.replace(zipper, {func, meta, [var | args]})
else
_ ->
zipper
end
zipper ->
zipper
end)
|> Sourceror.Zipper.topmost_root()
end
Most Liked
sodapopcan
Ok, I may be celebrating early but simply setting empty meta solved it.
{:|>, _, [var, {func, _, args}]} <- node do
Sourceror.Zipper.replace(zipper, {func, [], [var | args]})
slouchpie
zachdaniel
Ah, right. I think what you can do is check if the node previous to the top node is {:__block__, meta, [just_one_thing]} and if so, replace that node instead? Might mess w/ your traversal though.
Last Post!
sodapopcan
That’s good to know! I’ve been thinking of polishing and focusing up that package for a while now, even just for learnings, but have been spending my time on other ventures. I made it at a time where I was prototyping a new Phoenix app weekly and I don’t do that anymore. I will certainly look to igniter for inspiration, though. I remember a convo where you and Ben were talking about using closures in module attributes which was the big piece of refactoring I wanted to do, even just to see it work. I’m currently using a GenServer to get around not being able to figure that out for myself but anyway, that is a bit off topic ![]()
Popular in Questions
Other popular topics
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









