sodapopcan

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 :expressionless:

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

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

slouchpie

You can also use styler | Hex to fix single pipes.

zachdaniel

zachdaniel

Creator of Ash

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

sodapopcan

Turns out the real answer is “just use recode” which I totally forgot about. Was a good learning experience regardless :slight_smile:

zachdaniel

zachdaniel

Creator of Ash

Based on your proclivity for source code rewriting, you may also be interested in the igniter project :slight_smile:

It’s similar to recode in some ways, but designed for building generators, installers, and upgraders. GitHub - ash-project/igniter

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement