-
Notifications
You must be signed in to change notification settings - Fork 15
Lenstransform #95
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
Merged
Merged
Lenstransform #95
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a5b5a15
Make extension of macros easier
jw3126 6a4d84f
add demo of custom macros
jw3126 4529d21
refactor atset_impl into setmacro
jw3126 c9ed690
Add custom macro example to docs
jw3126 554f143
add docstrings to lensmacro and setmacro
jw3126 df724f4
fix docstring
jw3126 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| [deps] | ||
| Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" | ||
| Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306" | ||
| StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| *.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # # Extending `@set` and `@lens` | ||
| # This code demonstrates how to extend the `@set` and `@lens` mechanism with custom | ||
| # lenses. | ||
| # As a demo, we want to implement `@mylens!` and `@myset!`, which work much like | ||
| # `@lens` and `@set`, but mutate objects instead of returning modified copies. | ||
|
|
||
| using Setfield | ||
| using Setfield: IndexLens, PropertyLens, ComposedLens | ||
|
|
||
| struct Lens!{L <:Lens} <: Lens | ||
| pure::L | ||
| end | ||
|
|
||
| Setfield.get(o, l::Lens!) = Setfield.get(o, l.pure) | ||
| function Setfield.set(o, l::Lens!{<: ComposedLens}, val) | ||
| o_inner = get(o, l.pure.outer) | ||
| set(o_inner, Lens!(l.pure.inner), val) | ||
| end | ||
| function Setfield.set(o, l::Lens!{PropertyLens{prop}}, val) where {prop} | ||
| setproperty!(o, prop, val) | ||
| o | ||
| end | ||
| function Setfield.set(o, l::Lens!{<:IndexLens}, val) where {prop} | ||
| o[l.pure.indices...] = val | ||
| o | ||
| end | ||
|
|
||
| # Now this implements the kind of `lens` the new macros should use. | ||
| # Of course there are more variants like `Lens!(<:DynamicIndexLens)`, for which we might | ||
| # want to overload `set`, but lets ignore that. Instead we want to check, that everything works so far: | ||
|
|
||
| using Test | ||
| mutable struct M | ||
| a | ||
| b | ||
| end | ||
|
|
||
| o = M(1,2) | ||
| l = Lens!(@lens _.b) | ||
| set(o, l, 20) | ||
| @test o.b == 20 | ||
|
|
||
| l = Lens!(@lens _.foo[1]) | ||
| o = (foo=[1,2,3], bar=:bar) | ||
| set(o, l, 100) | ||
| @test o == (foo=[100,2,3], bar=:bar) | ||
|
|
||
| # Now we can implement the syntax macros | ||
|
|
||
| using Setfield: setmacro, lensmacro | ||
|
|
||
| macro myset!(ex) | ||
| setmacro(Lens!, ex) | ||
| end | ||
|
|
||
| macro mylens!(ex) | ||
| lensmacro(Lens!, ex) | ||
| end | ||
|
|
||
| o = M(1,2) | ||
| @myset! o.a = :hi | ||
| @myset! o.b += 98 | ||
| @test o.a == :hi | ||
| @test o.b == 100 | ||
|
|
||
| deep = [[[[1]]]] | ||
| @myset! deep[1][1][1][1] = 2 | ||
| @test deep[1][1][1][1] === 2 | ||
|
|
||
| l = @mylens! _.foo[1] | ||
| o = (foo=[1,2,3], bar=:bar) | ||
| set(o, l, 100) | ||
| @test o == (foo=[100,2,3], bar=:bar) | ||
|
|
||
| # Everything works, we can do arbitrary nesting and also use `+=` syntax etc. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| module TestExamples | ||
| using Test | ||
| dir = joinpath("..", "examples") | ||
| @testset "example $filename" for filename in readdir(dir) | ||
| path = joinpath(dir, filename) | ||
| include(path) | ||
| end | ||
| end#module |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| module TestSetMacro | ||
|
|
||
| module Clone | ||
| using Setfield: setmacro, lensmacro | ||
|
|
||
| macro lens(ex) | ||
| lensmacro(identity, ex) | ||
| end | ||
|
|
||
| macro set(ex) | ||
| setmacro(identity, ex) | ||
| end | ||
|
|
||
| end#module Clone | ||
|
|
||
| using Setfield: Setfield | ||
| using Test | ||
| using .Clone: Clone | ||
|
|
||
| using StaticArrays: @SMatrix | ||
|
|
||
| @testset "setmacro, lensmacro isolation" begin | ||
|
|
||
| # test that no symbols like `IndexLens` are needed: | ||
| @test Clone.@lens(_ ) isa Setfield.Lens | ||
| @test Clone.@lens(_.a ) isa Setfield.Lens | ||
| @test Clone.@lens(_[1] ) isa Setfield.Lens | ||
| @test Clone.@lens(first(_) ) isa Setfield.Lens | ||
| @test Clone.@lens(_[end] ) isa Setfield.Lens | ||
| @test Clone.@lens(_[$1] ) isa Setfield.Lens | ||
| @test Clone.@lens(_.a[1][end, end-2].b[$1, $1]) isa Setfield.Lens | ||
|
|
||
| @test Setfield.@lens(_.a) === Clone.@lens(_.a) | ||
| @test Setfield.@lens(_.a.b) === Clone.@lens(_.a.b) | ||
| @test Setfield.@lens(_.a.b[1,2]) === Clone.@lens(_.a.b[1,2]) | ||
|
|
||
| o = (a=1, b=2) | ||
| @test Clone.@set(o.a = 2) === Setfield.@set(o.a = 2) | ||
| @test Clone.@set(o.a += 2) === Setfield.@set(o.a += 2) | ||
|
|
||
| m = @SMatrix [0 0; 0 0] | ||
| m2 = Clone.@set m[end-1, end] = 1 | ||
| @test m2 === @SMatrix [0 1; 0 0] | ||
| m3 = Clone.@set(first(m) = 1) | ||
| @test m3 === @SMatrix[1 0; 0 0] | ||
| end | ||
|
|
||
| end#module | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.