Skip to content

Commit

Permalink
Replace BijectionLens API with a factory function converting
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Jun 13, 2019
1 parent 2ffa2c3 commit a764da6
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/src/index.md
Expand Up @@ -23,6 +23,7 @@ Kaleido.FlatLens
## Bijective transformations as lenses

```@docs
Kaleido.converting
Kaleido.setting
Kaleido.getting
Kaleido.settingasℝ₊
Expand Down
2 changes: 1 addition & 1 deletion src/Kaleido.jl
Expand Up @@ -6,13 +6,13 @@ module Kaleido

export
@batchlens,
BijectionLens,
FlatLens,
IndexBatchLens,
KeyBatchLens,
MultiLens,
PropertyBatchLens,
batch,
converting,
getting,
gettingasℝ₊,
gettingasℝ₋,
Expand Down
2 changes: 1 addition & 1 deletion src/batching.jl
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions 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));
Expand All @@ -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
Expand Down Expand Up @@ -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)))

Expand Down
2 changes: 1 addition & 1 deletion 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, "[]"),
Expand Down
2 changes: 1 addition & 1 deletion test/test_bijection.jl
Expand Up @@ -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
Expand Down

0 comments on commit a764da6

Please sign in to comment.