Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add shortcuts for Elements(), Properties(), @optic #125

Merged
merged 4 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/optics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ end
Elements

Access all elements of a collection that implements `map`.
An alias for `Elements()` is available as `∗` (`\\ast`). This optic can also be written as `@optic _[∗]`.

```jldoctest
julia> using Accessors
Expand Down Expand Up @@ -295,7 +296,8 @@ end
"""
Properties()

Access all properties of an objects.
Access all properties of an object.
An alias for `Properties()` is available as `∗ₚ` (`\\ast\\_p`). This optic can also be written as `@optic _[∗ₚ]`.

```jldoctest
julia> using Accessors
Expand Down
25 changes: 20 additions & 5 deletions src/sugar.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export @set, @optic, @reset, @modify, @delete, @insert, @accessor
export @set, @optic, @o, @reset, @modify, @delete, @insert, @accessor, ∗, ∗ₚ
using MacroTools

"""
Expand Down Expand Up @@ -430,11 +430,24 @@ macro accessor(ex)
end |> esc
end

### shortcuts:
# optic macro
const var"@o" = var"@optic"

# Elements, Properties
const ∗ = Elements()
const ∗ₚ = Properties()
IndexLens(::Tuple{Elements}) = Elements()
IndexLens(::Tuple{Properties}) = Properties()

Comment on lines +437 to +441
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks much better than doing it at "parse time". Can you add a test where the user defines these symbols?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optionally we could add a layer of indirection and instead of overloading the IndexLens constructor with something that is not an IndexLens, do this dispatch in another function index_like_lens.

### nice show() for optics
_shortstring(prev, o::PropertyLens{field}) where {field} = "$prev.$field"
_shortstring(prev, o::IndexLens) ="$prev[$(join(repr.(o.indices), ", "))]"
_shortstring(prev, o::Function) = "$o($prev)"
_shortstring(prev, o::Base.Fix1) = "$(o.f)($(o.x), $prev)"
_shortstring(prev, o::Base.Fix2) = "$(o.f)($prev, $(o.x))"
_shortstring(prev, o::Elements) = "$prev[∗]"
_shortstring(prev, o::Properties) = "$prev[∗ₚ]"

function show_optic(io, optic)
opts = deopcompose(optic)
Expand All @@ -452,13 +465,15 @@ function show_optic(io, optic)
end
end

Base.show(io::IO, optic::Union{IndexLens, PropertyLens}) = show_optic(io, optic)
Base.show(io::IO, ::MIME"text/plain", optic::Union{IndexLens, PropertyLens}) = show(io, optic)
const _OpticsTypes = Union{IndexLens,PropertyLens,Elements,Properties}

Base.show(io::IO, optic::_OpticsTypes) = show_optic(io, optic)
Base.show(io::IO, ::MIME"text/plain", optic::_OpticsTypes) = show(io, optic)

# only need show(io, optic) here because Base defines show(io::IO, ::MIME"text/plain", c::ComposedFunction) = show(io, c)
Base.show(io::IO, optic::ComposedFunction{<:Any, <:Union{IndexLens, PropertyLens}}) = show_optic(io, optic)
Base.show(io::IO, optic::ComposedFunction{<:Any, <:_OpticsTypes}) = show_optic(io, optic)
# resolve method ambiguity with Base:
Base.show(io::IO, optic::ComposedFunction{typeof(!), <:Union{IndexLens, PropertyLens}}) = show_optic(io, optic)
Base.show(io::IO, optic::ComposedFunction{typeof(!), <:_OpticsTypes}) = show_optic(io, optic)

# debugging
show_composition_order(optic) = (show_composition_order(stdout, optic); println())
Expand Down
6 changes: 6 additions & 0 deletions test/test_optics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ end
@test Accessors.DynamicIndexLens(lastindex).([(1,2,3), (4,5)]) == [3, 5]
end

@testset "shortcuts" begin
@test (@o _.a[2]) === (@optic _.a[2])
@test (@optic _[∗]) === Elements()
@test (@optic _.a[∗][2]) === (@optic _.a |> Elements() |> _[2])
end

end#module
Loading