@@ -36,7 +36,11 @@ struct VarName{sym,T}
36
36
37
37
function VarName {sym} (optic= identity) where {sym}
38
38
if ! is_static_optic (typeof (optic))
39
- throw (ArgumentError (" attempted to construct `VarName` with unsupported optic of type $(nameof (typeof (optic))) " ))
39
+ throw (
40
+ ArgumentError (
41
+ " attempted to construct `VarName` with unsupported optic of type $(nameof (typeof (optic))) " ,
42
+ ),
43
+ )
40
44
end
41
45
return new {sym,typeof(optic)} (optic)
42
46
end
168
172
169
173
function Base. show (io:: IO , vn:: VarName{sym,T} ) where {sym,T}
170
174
print (io, getsym (vn))
171
- _show_optic (io, getoptic (vn))
175
+ return _show_optic (io, getoptic (vn))
172
176
end
173
177
174
178
# modified from https://github.com/JuliaObjects/Accessors.jl/blob/01528a81fdf17c07436e1f3d99119d3f635e4c26/src/sugar.jl#L502
@@ -181,7 +185,7 @@ function _show_optic(io::IO, optic)
181
185
print (io, " ∘ " )
182
186
end
183
187
shortstr = reduce (_shortstring, inner; init= " " )
184
- print (io, shortstr)
188
+ return print (io, shortstr)
185
189
end
186
190
187
191
_shortstring (prev, o:: IndexLens ) = " $prev [$(join (map (prettify_index, o. indices), " , " )) ]"
@@ -207,7 +211,6 @@ Symbol("x[1][:]")
207
211
"""
208
212
Base. Symbol (vn:: VarName ) = Symbol (string (vn)) # simplified symbol
209
213
210
-
211
214
"""
212
215
inspace(vn::Union{VarName, Symbol}, space::Tuple)
213
216
@@ -244,7 +247,6 @@ inspace(vn::VarName, space::Tuple) = any(_in(vn, s) for s in space)
244
247
_in (vn:: VarName , s:: Symbol ) = getsym (vn) == s
245
248
_in (vn:: VarName , s:: VarName ) = subsumes (s, vn)
246
249
247
-
248
250
"""
249
251
subsumes(u::VarName, v::VarName)
250
252
@@ -297,8 +299,9 @@ subsumes(::typeof(identity), ::typeof(identity)) = true
297
299
subsumes (:: typeof (identity), :: ALLOWED_OPTICS ) = true
298
300
subsumes (:: ALLOWED_OPTICS , :: typeof (identity)) = false
299
301
300
- subsumes (t:: ComposedOptic , u:: ComposedOptic ) =
301
- subsumes (t. outer, u. outer) && subsumes (t. inner, u. inner)
302
+ function subsumes (t:: ComposedOptic , u:: ComposedOptic )
303
+ return subsumes (t. outer, u. outer) && subsumes (t. inner, u. inner)
304
+ end
302
305
303
306
# If `t` is still a composed lens, then there is no way it can subsume `u` since `u` is a
304
307
# leaf of the "lens-tree".
@@ -317,11 +320,12 @@ subsumes(t::PropertyLens, u::PropertyLens) = false
317
320
# FIXME : Does not support `DynamicIndexLens`.
318
321
# FIXME : Does not correctly handle cases such as `subsumes(x, x[:])`
319
322
# (but neither did old implementation).
320
- subsumes (
323
+ function subsumes (
321
324
t:: Union{IndexLens,ComposedOptic{<:ALLOWED_OPTICS,<:IndexLens}} ,
322
- u:: Union{IndexLens,ComposedOptic{<:ALLOWED_OPTICS,<:IndexLens}}
323
- ) = subsumes_indices (t, u)
324
-
325
+ u:: Union{IndexLens,ComposedOptic{<:ALLOWED_OPTICS,<:IndexLens}} ,
326
+ )
327
+ return subsumes_indices (t, u)
328
+ end
325
329
326
330
"""
327
331
subsumedby(t, u)
@@ -444,7 +448,6 @@ subsumes_index(i::Colon, j) = true
444
448
subsumes_index (i:: AbstractVector , j) = issubset (j, i)
445
449
subsumes_index (i, j) = i == j
446
450
447
-
448
451
"""
449
452
ConcretizedSlice(::Base.Slice)
450
453
@@ -455,10 +458,13 @@ struct ConcretizedSlice{T,R} <: AbstractVector{T}
455
458
range:: R
456
459
end
457
460
458
- ConcretizedSlice (s:: Base.Slice{R} ) where {R} = ConcretizedSlice {eltype(s.indices),R} (s. indices)
461
+ function ConcretizedSlice (s:: Base.Slice{R} ) where {R}
462
+ return ConcretizedSlice {eltype(s.indices),R} (s. indices)
463
+ end
459
464
Base. show (io:: IO , s:: ConcretizedSlice ) = print (io, " :" )
460
- Base. show (io:: IO , :: MIME"text/plain" , s:: ConcretizedSlice ) =
461
- print (io, " ConcretizedSlice(" , s. range, " )" )
465
+ function Base. show (io:: IO , :: MIME"text/plain" , s:: ConcretizedSlice )
466
+ return print (io, " ConcretizedSlice(" , s. range, " )" )
467
+ end
462
468
Base. size (s:: ConcretizedSlice ) = size (s. range)
463
469
Base. iterate (s:: ConcretizedSlice , state... ) = Base. iterate (s. range, state... )
464
470
Base. collect (s:: ConcretizedSlice ) = collect (s. range)
@@ -480,8 +486,9 @@ The only purpose of this are special cases like `:`, which we want to avoid beco
480
486
`ConcretizedSlice` based on the `lowered_index`, just what you'd get with an explicit `begin:end`
481
487
"""
482
488
reconcretize_index (original_index, lowered_index) = lowered_index
483
- reconcretize_index (original_index:: Colon , lowered_index:: Base.Slice ) =
484
- ConcretizedSlice (lowered_index)
489
+ function reconcretize_index (original_index:: Colon , lowered_index:: Base.Slice )
490
+ return ConcretizedSlice (lowered_index)
491
+ end
485
492
486
493
"""
487
494
concretize(l, x)
@@ -495,7 +502,9 @@ the result close to the original indexing.
495
502
"""
496
503
concretize (I:: ALLOWED_OPTICS , x) = I
497
504
concretize (I:: DynamicIndexLens , x) = concretize (IndexLens (I. f (x)), x)
498
- concretize (I:: IndexLens , x) = IndexLens (reconcretize_index .(I. indices, to_indices (x, I. indices)))
505
+ function concretize (I:: IndexLens , x)
506
+ return IndexLens (reconcretize_index .(I. indices, to_indices (x, I. indices)))
507
+ end
499
508
function concretize (I:: ComposedOptic , x)
500
509
x_inner = I. inner (x) # TODO : get view here
501
510
return ComposedOptic (concretize (I. outer, x_inner), concretize (I. inner, x))
@@ -646,11 +655,9 @@ function varname(expr::Expr, concretize=Accessors.need_dynamic_optic(expr))
646
655
end
647
656
648
657
if concretize
649
- return :(
650
- $ (AbstractPPL. VarName){$ sym}(
658
+ return :($ (AbstractPPL. VarName){$ sym}(
651
659
$ (AbstractPPL. concretize)($ optics, $ sym_escaped)
652
- )
653
- )
660
+ ))
654
661
elseif Accessors. need_dynamic_optic (expr)
655
662
error (" Variable name `$(expr) ` is dynamic and requires concretization!" )
656
663
else
672
679
function _parse_obj_optic (ex)
673
680
obj, optics = _parse_obj_optics (ex)
674
681
optic = Expr (:call , Accessors. opticcompose, optics... )
675
- obj, optic
682
+ return obj, optic
676
683
end
677
684
678
685
# Accessors doesn't have the same support for interpolation
@@ -688,7 +695,8 @@ function _parse_obj_optics(ex)
688
695
indices = Accessors. replace_underscore .(indices, collection)
689
696
dims = length (indices) == 1 ? nothing : 1 : length (indices)
690
697
lindices = esc .(Accessors. lower_index .(collection, indices, dims))
691
- optics = :($ (Accessors. DynamicIndexLens)($ (esc (collection)) -> ($ (lindices... ),)))
698
+ optics =
699
+ :($ (Accessors. DynamicIndexLens)($ (esc (collection)) -> ($ (lindices... ),)))
692
700
else
693
701
index = esc (Expr (:tuple , indices... ))
694
702
optics = :($ (Accessors. IndexLens)($ index))
@@ -702,16 +710,20 @@ function _parse_obj_optics(ex)
702
710
elseif Meta. isexpr (property, :$ , 1 )
703
711
optics = :($ (Accessors. PropertyLens){$ (esc (property. args[1 ]))}())
704
712
else
705
- throw (ArgumentError (
706
- string (" Error while parsing :($ex ). Second argument to `getproperty` can only be" ,
707
- " a `Symbol` or `String` literal, received `$property ` instead." )
708
- ))
713
+ throw (
714
+ ArgumentError (
715
+ string (
716
+ " Error while parsing :($ex ). Second argument to `getproperty` can only be" ,
717
+ " a `Symbol` or `String` literal, received `$property ` instead." ,
718
+ ),
719
+ ),
720
+ )
709
721
end
710
722
else
711
723
obj = esc (ex)
712
724
return obj, ()
713
725
end
714
- obj, tuple (frontoptics... , optics)
726
+ return obj, tuple (frontoptics... , optics)
715
727
end
716
728
717
729
"""
@@ -778,12 +790,27 @@ Convert an index `i` to a dictionary representation.
778
790
"""
779
791
index_to_dict (i:: Integer ) = Dict (" type" => _BASE_INTEGER_TYPE, " value" => i)
780
792
index_to_dict (v:: Vector{Int} ) = Dict (" type" => _BASE_VECTOR_TYPE, " values" => v)
781
- index_to_dict (r:: UnitRange ) = Dict (" type" => _BASE_UNITRANGE_TYPE, " start" => r. start, " stop" => r. stop)
782
- index_to_dict (r:: StepRange ) = Dict (" type" => _BASE_STEPRANGE_TYPE, " start" => r. start, " stop" => r. stop, " step" => r. step)
783
- index_to_dict (r:: Base.OneTo{I} ) where {I} = Dict (" type" => _BASE_ONETO_TYPE, " stop" => r. stop)
793
+ function index_to_dict (r:: UnitRange )
794
+ return Dict (" type" => _BASE_UNITRANGE_TYPE, " start" => r. start, " stop" => r. stop)
795
+ end
796
+ function index_to_dict (r:: StepRange )
797
+ return Dict (
798
+ " type" => _BASE_STEPRANGE_TYPE,
799
+ " start" => r. start,
800
+ " stop" => r. stop,
801
+ " step" => r. step,
802
+ )
803
+ end
804
+ function index_to_dict (r:: Base.OneTo{I} ) where {I}
805
+ return Dict (" type" => _BASE_ONETO_TYPE, " stop" => r. stop)
806
+ end
784
807
index_to_dict (:: Colon ) = Dict (" type" => _BASE_COLON_TYPE)
785
- index_to_dict (s:: ConcretizedSlice{T,R} ) where {T,R} = Dict (" type" => _CONCRETIZED_SLICE_TYPE, " range" => index_to_dict (s. range))
786
- index_to_dict (t:: Tuple ) = Dict (" type" => _BASE_TUPLE_TYPE, " values" => map (index_to_dict, t))
808
+ function index_to_dict (s:: ConcretizedSlice{T,R} ) where {T,R}
809
+ return Dict (" type" => _CONCRETIZED_SLICE_TYPE, " range" => index_to_dict (s. range))
810
+ end
811
+ function index_to_dict (t:: Tuple )
812
+ return Dict (" type" => _BASE_TUPLE_TYPE, " values" => map (index_to_dict, t))
813
+ end
787
814
788
815
"""
789
816
dict_to_index(dict)
@@ -839,9 +866,17 @@ function dict_to_index(dict)
839
866
end
840
867
841
868
optic_to_dict (:: typeof (identity)) = Dict (" type" => " identity" )
842
- optic_to_dict (:: PropertyLens{sym} ) where {sym} = Dict (" type" => " property" , " field" => String (sym))
869
+ function optic_to_dict (:: PropertyLens{sym} ) where {sym}
870
+ return Dict (" type" => " property" , " field" => String (sym))
871
+ end
843
872
optic_to_dict (i:: IndexLens ) = Dict (" type" => " index" , " indices" => index_to_dict (i. indices))
844
- optic_to_dict (c:: ComposedOptic ) = Dict (" type" => " composed" , " outer" => optic_to_dict (c. outer), " inner" => optic_to_dict (c. inner))
873
+ function optic_to_dict (c:: ComposedOptic )
874
+ return Dict (
875
+ " type" => " composed" ,
876
+ " outer" => optic_to_dict (c. outer),
877
+ " inner" => optic_to_dict (c. inner),
878
+ )
879
+ end
845
880
846
881
function dict_to_optic (dict)
847
882
if dict[" type" ] == " identity"
@@ -857,9 +892,13 @@ function dict_to_optic(dict)
857
892
end
858
893
end
859
894
860
- varname_to_dict (vn:: VarName ) = Dict (" sym" => getsym (vn), " optic" => optic_to_dict (getoptic (vn)))
895
+ function varname_to_dict (vn:: VarName )
896
+ return Dict (" sym" => getsym (vn), " optic" => optic_to_dict (getoptic (vn)))
897
+ end
861
898
862
- dict_to_varname (dict:: Dict{<:AbstractString, Any} ) = VarName {Symbol(dict["sym"])} (dict_to_optic (dict[" optic" ]))
899
+ function dict_to_varname (dict:: Dict{<:AbstractString,Any} )
900
+ return VarName {Symbol(dict["sym"])} (dict_to_optic (dict[" optic" ]))
901
+ end
863
902
864
903
"""
865
904
varname_to_string(vn::VarName)
0 commit comments