sodapopcan
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
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachdaniel
Is the idea here to unpipe everything next/down from the current zipper? I think your code would turn this
into this:
Is that what you’re aiming for?
sodapopcan
Yes, any single pipes, though that particular case I did not account for! It does indeed work here though it adds new lines (which is a separate general issue I’m dealing with).
These were my test cases so far (not super thorough):
zachdaniel
Ahhh, interesting. Only pipes where there isn’t a 2 or more pipes. Neat idea
In that case your traverse + prev/next strategy looks like the best way to go about this IMO. It’s a benefit of Zipper that you actually don’t need an accumulator to do this kind of thing. It would only add complexity IMO.
zachdaniel
The new line change likely is just coming from the fact that the code is formatted when you stringify it? Probably nothing to be done there?
sodapopcan
Yes, I just started on a new team that inherited a codebase where it’s rampant. I’m not too fussed about “no single pipes” depending on how they are used, but they are everywhere to point it’s making code exhausting to read. There is even stuff like this:
In any event, we’re getting all of our bikeshedding out of the way upfront
And it’s also nice to have a task to run in CI.
That’s good to know! Zipper certainly allowed me to write code inline with how I was thinking about the problem. It’s more of an academic interest if it’s even possible with basic walking as it was hurting my brain trying to figure it out. I’m happy to move on from it, though.
The problem is is that it’s doing it for any function calls that have more than one arg, even if the line is within the limit. I believe it has to do with how Sourceror adds
:__block__around stuff to help maintain the original format. I’m pretty sure I just need to be more explicit in how I return the transformed node. I believe the answer might be in here somewhere.Thanks for your response, Zach!
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.sodapopcan
Ok, I may be celebrating early but simply setting empty
metasolved it.sodapopcan
I’ll keep an eye out for this, thanks!!
sodapopcan
Turns out the real answer is “just use recode” which I totally forgot about. Was a good learning experience regardless
zachdaniel
Based on your proclivity for source code rewriting, you may also be interested in the igniter project
It’s similar to recode in some ways, but designed for building generators, installers, and upgraders. GitHub - ash-project/igniter: A code generation and project patching framework. · GitHub