@@ -29,10 +29,11 @@ julia> @varname x[:, 1][1+1]
29
29
x[:,1][2]
30
30
```
31
31
"""
32
- struct VarName{sym, T<: Lens }
32
+ struct VarName{sym,T<: Lens }
33
33
indexing:: T
34
34
35
- VarName {sym} (indexing= IdentityLens ()) where {sym} = new {sym,typeof(indexing)} (indexing)
35
+ VarName {sym} (indexing = IdentityLens ()) where {sym} =
36
+ new {sym,typeof(indexing)} (indexing)
36
37
end
37
38
38
39
# A bit of backwards compatibility.
@@ -56,15 +57,16 @@ julia> VarName(@varname(x[1][2:3]))
56
57
x
57
58
```
58
59
"""
59
- VarName (vn:: VarName , indexing:: Lens = IdentityLens ()) = VarName {getsym(vn)} (indexing)
60
+ VarName (vn:: VarName , indexing:: Lens = IdentityLens ()) = VarName {getsym(vn)} (indexing)
60
61
61
62
function VarName (vn:: VarName , indexing:: Tuple )
62
63
return VarName {getsym(vn)} (tupleindex2lens (indexing))
63
64
end
64
65
65
66
tupleindex2lens (indexing:: Tuple{} ) = IdentityLens ()
66
67
tupleindex2lens (indexing:: Tuple{<:Tuple} ) = IndexLens (first (indexing))
67
- tupleindex2lens (indexing:: Tuple ) = IndexLens (first (indexing)) ∘ tupleindex2lens (indexing[2 : end ])
68
+ tupleindex2lens (indexing:: Tuple ) =
69
+ IndexLens (first (indexing)) ∘ tupleindex2lens (indexing[2 : end ])
68
70
69
71
"""
70
72
getsym(vn::VarName)
@@ -81,7 +83,7 @@ julia> getsym(@varname(y))
81
83
:y
82
84
```
83
85
"""
84
- getsym (vn:: VarName{sym} ) where sym = sym
86
+ getsym (vn:: VarName{sym} ) where { sym} = sym
85
87
86
88
87
89
"""
@@ -121,14 +123,14 @@ end
121
123
122
124
123
125
Base. hash (vn:: VarName , h:: UInt ) = hash ((getsym (vn), getindexing (vn)), h)
124
- Base.:(== )(x:: VarName , y:: VarName ) = getsym (x) == getsym (y) && getindexing (x) == getindexing (y)
126
+ Base.:(== )(x:: VarName , y:: VarName ) =
127
+ getsym (x) == getsym (y) && getindexing (x) == getindexing (y)
125
128
126
- # Composition rules similar to the standard one for lenses, but we need a special
127
- # one for the "empty" `VarName{..., Tuple{}}`.
128
- Base.:∘ (vn:: VarName{sym,<:IdentityLens} , lens:: Lens ) where {sym} = VarName {sym} (lens)
129
- Base.:∘ (vn:: VarName{sym,<:Lens} , lens:: Lens ) where {sym} = VarName {sym} (vn. indexing ∘ lens)
129
+ # Allow compositions with lenses.
130
+ Setfield. compose (vn:: VarName{sym,<:Lens} , lens:: Lens ) where {sym} =
131
+ VarName {sym} (vn. indexing ∘ lens)
130
132
131
- function Base. show (io:: IO , vn:: VarName{<:Any, <:Lens} )
133
+ function Base. show (io:: IO , vn:: VarName{<:Any,<:Lens} )
132
134
print (io, getsym (vn))
133
135
_print_application (io, vn. indexing)
134
136
end
@@ -138,7 +140,8 @@ function _print_application(io::IO, l::ComposedLens)
138
140
_print_application (io, l. outer)
139
141
_print_application (io, l. inner)
140
142
end
141
- _print_application (io:: IO , l:: IndexLens ) = print (io, " [" , join (map (prettify_index, l. indices), " ," ), " ]" )
143
+ _print_application (io:: IO , l:: IndexLens ) =
144
+ print (io, " [" , join (map (prettify_index, l. indices), " ," ), " ]" )
142
145
# This is a bit weird but whatever. We're almost always going to
143
146
# `concretize` anyways.
144
147
_print_application (io:: IO , l:: DynamicIndexLens ) = print (io, l, " (_)" )
247
250
subsumes (t:: IdentityLens , u:: Lens ) = true
248
251
subsumes (t:: Lens , u:: IdentityLens ) = false
249
252
250
- subsumes (t:: ComposedLens , u:: ComposedLens ) = subsumes (t. outer, u. outer) && subsumes (t. inner, u. inner)
253
+ subsumes (t:: ComposedLens , u:: ComposedLens ) =
254
+ subsumes (t. outer, u. outer) && subsumes (t. inner, u. inner)
251
255
252
256
# If `t` is still a composed lens, then there is no way it can subsume `u` since `u` is a
253
257
# leaf of the "lens-tree".
@@ -345,7 +349,7 @@ e.g. `x[:][1][2]` becomes `((Colon(), ), (1, ), (2, ))`.
345
349
The result is compatible with [`subsumes_index`](@ref) for `Tuple` input.
346
350
"""
347
351
combine_indices (lens:: Lens ) = (), lens
348
- combine_indices (lens:: IndexLens ) = (lens. indices, ), nothing
352
+ combine_indices (lens:: IndexLens ) = (lens. indices,), nothing
349
353
function combine_indices (lens:: ComposedLens{<:IndexLens} )
350
354
indices, next = combine_indices (lens. inner)
351
355
return (lens. outer. indices, indices... ), next
@@ -369,13 +373,13 @@ function subsumes_index(t::Tuple, u::Tuple) # does x[i]... subsume x[j]...?
369
373
return _issubindex (first (t), first (u)) && subsumes_index (Base. tail (t), Base. tail (u))
370
374
end
371
375
372
- const AnyIndex = Union{Int, AbstractVector{Int}, Colon}
376
+ const AnyIndex = Union{Int,AbstractVector{Int},Colon}
373
377
_issubindex_ (:: Tuple{Vararg{AnyIndex}} , :: Tuple{Vararg{AnyIndex}} ) = false
374
- function _issubindex (t:: NTuple{N, AnyIndex} , u:: NTuple{N, AnyIndex} ) where {N}
378
+ function _issubindex (t:: NTuple{N,AnyIndex} , u:: NTuple{N,AnyIndex} ) where {N}
375
379
return all (_issubrange (j, i) for (i, j) in zip (t, u))
376
380
end
377
381
378
- const ConcreteIndex = Union{Int, AbstractVector{Int}} # this include all kinds of ranges
382
+ const ConcreteIndex = Union{Int,AbstractVector{Int}} # this include all kinds of ranges
379
383
380
384
""" Determine whether indices `i` are contained in `j`, treating `:` as universal set."""
381
385
_issubrange (i:: ConcreteIndex , j:: ConcreteIndex ) = issubset (i, j)
@@ -455,12 +459,13 @@ julia> @varname(x[1,2][1+5][45][3]).indexing
455
459
Using `begin` in an indexing expression to refer to the first index requires at least
456
460
Julia 1.5.
457
461
"""
458
- macro varname (expr:: Union{Expr, Symbol} , concretize:: Bool = false )
462
+ macro varname (expr:: Union{Expr,Symbol} , concretize:: Bool = false )
459
463
return varname (expr, concretize)
460
464
end
461
465
462
- varname (sym:: Symbol , concretize:: Bool = false ) = :($ (AbstractPPL. VarName){$ (QuoteNode (sym))}())
463
- function varname (expr:: Expr , concretize:: Bool = false )
466
+ varname (sym:: Symbol , concretize:: Bool = false ) =
467
+ :($ (AbstractPPL. VarName){$ (QuoteNode (sym))}())
468
+ function varname (expr:: Expr , concretize:: Bool = false )
464
469
if Meta. isexpr (expr, :ref ) || Meta. isexpr (expr, :.)
465
470
# Split into object/base symbol and lens.
466
471
sym_escaped, lens = Setfield. parse_obj_lens (expr)
@@ -469,7 +474,10 @@ function varname(expr::Expr, concretize::Bool=false)
469
474
sym = drop_escape (sym_escaped)
470
475
471
476
return if concretize && Setfield. need_dynamic_lens (expr)
472
- :($ (AbstractPPL. concretize)($ (AbstractPPL. VarName){$ (QuoteNode (sym))}($ lens), $ sym_escaped))
477
+ :($ (AbstractPPL. concretize)(
478
+ $ (AbstractPPL. VarName){$ (QuoteNode (sym))}($ lens),
479
+ $ sym_escaped,
480
+ ))
473
481
else
474
482
:($ (AbstractPPL. VarName){$ (QuoteNode (sym))}($ lens))
475
483
end
@@ -503,7 +511,7 @@ julia> @vsym x[end]
503
511
:x
504
512
```
505
513
"""
506
- macro vsym (expr:: Union{Expr, Symbol} )
514
+ macro vsym (expr:: Union{Expr,Symbol} )
507
515
return QuoteNode (vsym (expr))
508
516
end
509
517
@@ -522,4 +530,3 @@ function vsym(expr::Expr)
522
530
error (" Malformed variable name $(expr) !" )
523
531
end
524
532
end
525
-
0 commit comments