Comments (4)
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.
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.
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.
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 calledLamExpr
exists in current scope... The value denoted byLamExpr
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)
- Unable to document `@data`
- How do you match on Pair? HOT 1
- Pattern "equality is strict for primitive types"
- `Vector{Int}` value fails to match `Vector{Any}` pattern
- Document `pattern_unmacrocall` usage
- Use doctests
- Expand docs for `pattern_uncall`
- Expand docs for `pattern_unref`
- Expand docs for `is_enum` and `enum_matcher`
- Explain active patterns
- Give a useful example of `let` patterns HOT 1
- Document GADTs HOT 1
- Show multiple `@case` expressions in the `@switch` docs
- Better error message for calling `@match` on non-record struct instance
- Automatic `@as_record` pattern matching HOT 1
- `@trymatch` to return `Some`
- Work with EnumX and other namespaced properties HOT 2
- Fix pattern matching docs section order
- `@match :(if true; end) :(if true; end) => 1` fails HOT 1
- `@match` gives the incorrect results when types are passed in as values HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mlstyle.jl.