Stream: helpdesk (published)

Topic: Avoid extra begin blocks in expression


view this post on Zulip DrChainsaw (Jul 07 2022 at 17:34):

Is there a reasonable way to avoid the begin block wrapping the nested let block here:

function buildexpr(x::Tuple)
    subexprs = map(x) do xi
        buildexpr(xi)
    end
    quote
        let
            $(subexprs...)
            foo($x)
        end
    end
end

buildexpr(x) = :($x)

julia> buildexpr((1,2,(3,4)))
quote
    #= REPL[289]:6 =#
    let
        #= REPL[289]:7 =#
        1
        2
        begin
            #= REPL[289]:6 =#
            let
                #= REPL[289]:7 =#
                3
                4
                #= REPL[289]:8 =#
                foo((3, 4))
            end
        end
        #= REPL[289]:8 =#
        foo((1, 2, (3, 4)))
    end
end

I guess they don't do anything bad, but I would like to make the generated expression a bit more readable as there might be many levels.

view this post on Zulip Michael Abbott (Jul 07 2022 at 18:43):

julia> function buildexpr(x::Tuple)
           subexprs = map(x) do xi
               buildexpr(xi)
           end
           :(let
               $(subexprs...)
               foo($x)
           end)
       end
buildexpr (generic function with 2 methods)

julia> buildexpr((1,2,(3,4)))
:(let
      #= REPL[185]:6 =#
      1
      2
      let
          #= REPL[185]:6 =#
          3
          4
          #= REPL[185]:7 =#
          foo((3, 4))
      end
      #= REPL[185]:7 =#
      foo((1, 2, (3, 4)))
  end)

view this post on Zulip DrChainsaw (Jul 07 2022 at 19:18):

Wow, here I have been thinking for the longest time that qoute blocks are just a way to do :() with fewer parentheses. The manual seems to just kinda introduce them like they are equivalent.

view this post on Zulip Sukera (Jul 07 2022 at 19:26):

:() is more like a quasiquote with interpolation

view this post on Zulip Sukera (Jul 07 2022 at 19:26):

without interpolation, it's a "return the literal expression written here" thing

view this post on Zulip Michael Fiano (Jul 07 2022 at 19:28):

Yeah, that's how I mapped it to my Lisp brain as well.

view this post on Zulip Michael Fiano (Jul 07 2022 at 19:29):

I use that pattern all the time for combining multiple let blocks into one, though not in Julia...yet :smile:

view this post on Zulip jar (Jul 07 2022 at 19:29):

@Sukera Why do you have to distinguish between those two cases?

view this post on Zulip Sukera (Jul 07 2022 at 19:32):

because the interpolate part gets interpolated into the returned expression, instead of being an interpolate block in the expression

view this post on Zulip DrChainsaw (Jul 07 2022 at 19:32):

@Sukera This was how I thought about both qoute and :(). My revised intuition is that they are equivalent, its just that a quote adds a begin block when lowered (or something) which happened to show up here.

view this post on Zulip Michael Fiano (Jul 07 2022 at 19:32):

Basically, quoting is not the same as quasiquoting

view this post on Zulip Sukera (Jul 07 2022 at 19:32):

as in, the returned expression has no idea it got interpolated into


Last updated: Nov 06 2024 at 04:40 UTC