GithubHelp home page GithubHelp logo

Comments (4)

cscherrer avatar cscherrer commented on May 21, 2024 1

Wow, this is great! I've seen you reference active patterns before, but hadn't really seen the use case.
A couple of things about this are still surprising:

  • I don't yet see why b in my earlier attempt didn't match all of :($a -> begin $(bs...) end)
  • It's interesting that LamExpr(:a,:b) doesn't work outside @match

So I have plenty more to learn. But this issue is solved, so I'll close it now. Thank you!

from mlstyle.jl.

thautwarm avatar thautwarm commented on May 21, 2024

Hi,
that's caused for :(a -> a + 1) will create such an AST:

:(a->begin
          #= REPL[1]:1 =#
          a + 1
      end)

Also, :(a -> begin a + 1 end) will create the same AST as well...
So we can match it with:

julia> @match :(a -> a+1) begin
           :($a -> begin $lnn; $b end) => "ok"
           x           => "nope"
       end
"ok"

from mlstyle.jl.

thautwarm avatar thautwarm commented on May 21, 2024

There's a solution to end the ugly look of :($a -> begin $lnn; $b end):

@active LamExpr(x) begin
    @match x begin
        :($a -> begin $(bs...) end) => 
          let exprs = filter(x -> !(x isa LineNumberNode), bs)
            if length(exprs) == 1
              (a, exprs[1])
            else
              (a, Expr(:block, bs...))
              end
        end
         _  => nothing
    end
end

@match :(a -> b) begin
         LamExpr(a, b) => (a, b)
end

=> (:a, :b)

@match :(a -> begin b ; 1 end) begin
   LamExpr(a, b) => (a, b)
end

=> (:a, quote
    #= REPL[5]:1 =#
    #= REPL[5]:1 =#
    b
    #= REPL[5]:1 =#
    1
end)



from mlstyle.jl.

thautwarm avatar thautwarm commented on May 21, 2024

@cscherrer

I don't yet see why b in my earlier attempt didn't match all of :($a -> begin $(bs...) end)

Okay, this is explained in Slack channel, but for the sake of other users' prospective search, I'll explained it again here...

It's caused by the Julia parser,

julia> dump(:(a -> a))
Expr
  head: Symbol ->    
  args: Array{Any}((2,))
    1: Symbol a
    2: Expr
      head: Symbol block   # <----------------- LOOK HERE OUT!!!
      args: Array{Any}((2,))
        1: LineNumberNode
          line: Int64 1
          file: Symbol REPL[1]
        2: Symbol a

Also, if you remove the LineNumberNode of :(a -> a), you can then match it with :($a -> $b)...

julia> using MLStyle

julia> rmlines =  begin
           e :: Expr           -> Expr(e.head, filter(x -> x !== nothing, map(rmlines, e.args))...)
             :: LineNumberNode -> nothing
           a                   -> a
       end
#3 (generic function with 1 method)

julia> @match :(a -> a) |> rmlines begin
          :($a -> $b) => (a, b)
       end
(:a, :a)

MLStyle AST matching has no hacking, it just takes advantage of the (almost) consistent mapping from Julia syntaxes to their ASTs...

It's interesting that LamExpr(:a,:b) doesn't work outside @match

That's a bit implementation dependent.
If you want, you can read the following explanation:

In fact an immutable struct called LamExpr is made if nothing called LamExpr exists in current scope... The value denoted by LamExpr will be registered in the pattern compilation table, which will decide how to compile Julia ASTs into pattern matching codes...

Thank you again for your feedbacks. You have encouraged our development a lot!

from mlstyle.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.