diff --git a/docs/src/index.md b/docs/src/index.md index 93da3aeca..53c6bdeaa 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -23,6 +23,7 @@ Kaleido.FlatLens ## Bijective transformations as lenses ```@docs +Kaleido.converting Kaleido.setting Kaleido.getting Kaleido.settingasℝ₊ diff --git a/src/Kaleido.jl b/src/Kaleido.jl index d414fa398..0efe15261 100644 --- a/src/Kaleido.jl +++ b/src/Kaleido.jl @@ -6,13 +6,13 @@ module Kaleido export @batchlens, - BijectionLens, FlatLens, IndexBatchLens, KeyBatchLens, MultiLens, PropertyBatchLens, batch, + converting, getting, gettingasℝ₊, gettingasℝ₋, diff --git a/src/batching.jl b/src/batching.jl index 2c3c3e415..460c2227c 100644 --- a/src/batching.jl +++ b/src/batching.jl @@ -94,7 +94,7 @@ julia> using Kaleido, Setfield julia> lens = @batchlens begin _.a.b.c - _.a.b.d ∘ BijectionLens(x -> parse(Int, x), string) + _.a.b.d ∘ converting(fromfield = x -> parse(Int, x), tofield = string) _.a.e end; diff --git a/src/bijection.jl b/src/bijection.jl index 29bafe61f..c164d7b9f 100644 --- a/src/bijection.jl +++ b/src/bijection.jl @@ -1,11 +1,11 @@ """ - BijectionLens(fromfield, tofield) :: Lens + converting(; fromfield, tofield) :: Lens # Examples ```jldoctest julia> using Setfield, Kaleido -julia> l = (@lens _.y[2]) ∘ BijectionLens(x -> x/2, x -> 2x); +julia> l = (@lens _.y[2]) ∘ converting(fromfield = x -> x/2, tofield = x -> 2x); julia> obj = (x=0, y=(1, 2, 3)); @@ -14,7 +14,7 @@ julia> @assert get(obj, l) == 1.0 == 2/2 julia> @assert set(obj, l, 0.5) == (x=0, y=(1, 1.0, 3)) ``` """ -BijectionLens +converting """ setting(xf::TransformVariables.AbstractTransform) :: Lens @@ -69,6 +69,8 @@ Setfield.set(::Any, l::BijectionLens, x) = tofield(l.bijection, x) BijectionLens(fromfield, tofield) = BijectionLens(FunctionPair(fromfield, tofield)) +converting(; fromfield, tofield) = BijectionLens(FunctionPair(fromfield, tofield)) + setting(thing) = BijectionLens(Bijection(thing)) getting(thing) = BijectionLens(inv(Bijection(thing))) diff --git a/test/test_base.jl b/test/test_base.jl index 67b6691b7..5a78364ef 100644 --- a/test/test_base.jl +++ b/test/test_base.jl @@ -1,7 +1,7 @@ module TestBase include("preamble.jl") -using Kaleido: prefer_singleton_callable +using Kaleido: prefer_singleton_callable, BijectionLens lenses_as_shown = include("lenses_as_shown.jl") :: Array desired_show = filter(x -> !occursin(x, "[]"), diff --git a/test/test_bijection.jl b/test/test_bijection.jl index c9bbf2de5..aa1ad1501 100644 --- a/test/test_bijection.jl +++ b/test/test_bijection.jl @@ -3,7 +3,7 @@ module TestBijection include("preamble.jl") @testset begin - l = (@lens _.y[2]) ∘ BijectionLens(y -> y - 1, x -> x + 1) + l = (@lens _.y[2]) ∘ converting(fromfield = y -> y - 1, tofield = x -> x + 1) obj = (x=0, y=(1, 2, 3)) @test get(obj, l) == 1 @test set(obj, l, 10).y[2] == 11