Skip to content

Commit

Permalink
minor cleanup and typos (#112)
Browse files Browse the repository at this point in the history
* fix name typo
* some cleanup
* fix undef name
  • Loading branch information
aplavin authored Jun 8, 2023
1 parent c814bec commit 6d57418
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 41 deletions.
38 changes: 5 additions & 33 deletions examples/specter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,6 @@ out = @set data |> Vals() |> Elements() |> Vals() |> If(iseven) += 1

@test out == (a = [(aa = 1, bb = 3), (cc = 3,)], b = [(dd = 5,)])

struct Filter{F}
keep_condition::F
end
OpticStyle(::Type{<:Filter}) = ModifyBased()
(o::Filter)(x) = filter(o.keep_condition, x)
function modify(f, obj, optic::Filter)
I = eltype(eachindex(obj))
inds = I[]
for i in eachindex(obj)
x = obj[i]
if optic.keep_condition(x)
push!(inds, i)
end
end
vals = f(obj[inds])
setindex(obj, vals, inds)
end


# ### Append to nested vector
data = (a = 1:3,)
out = @modify(v -> vcat(v, [4,5]), data.a)
Expand All @@ -58,7 +39,7 @@ out = @modify(v -> vcat(v, [4,5]), data.a)
# ### Increment last odd number in a sequence

data = 1:4
out = @set data |> Filter(isodd) |> last += 1
out = @set data |> filter(isodd, _) |> last += 1
@test out == [1,2,4,4]

### Map over a sequence
Expand All @@ -80,31 +61,22 @@ out = @set data |> Elements() |> _[:a] += 1
@test out == [Dict(:a => 2), Dict(:a => 3), Dict(:a => 5), Dict(:a => 4)]

# ### Retrieve every number divisible by 3 out of a sequence of sequences

function getall(obj, optic)
out = Any[]
modify(obj, optic) do val
push!(out, val)
end
out
end

data = [[1,2,3,4],[], [5,3,2,18],[2,4,6], [12]]
data = [[1,2,3,4], Int[], [5,3,2,18], [2,4,6], [12]]
optic = @optic _ |> Elements() |> Elements() |> If(x -> mod(x, 3) == 0)
out = getall(data, optic)
@test out == [3, 3, 18, 6, 12]
@test_broken eltype(out) == Int
@test eltype(out) == Int

# ### Increment the last odd number in a sequence

data = [2, 1, 3, 6, 9, 4, 8]
out = @set data |> Filter(isodd) |> _[end] += 1
out = @set data |> filter(isodd, _) |> _[end] += 1
@test out == [2, 1, 3, 6, 10, 4, 8]
@test eltype(out) == Int

# ### Remove nils from a nested sequence

data = (a = [1,2,missing, 3, missing],)
optic = @optic _.a |> Filter(!ismissing)
optic = @optic _.a |> filter(!ismissing, _)
out = optic(data)
@test out == [1,2,3]
2 changes: 1 addition & 1 deletion src/functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ set(obj::AbstractVector, ::Type{Tuple}, val::Tuple) = similar(obj, eltype(val))

set(obj, ::Type{NamedTuple{KS}}, val::NamedTuple) where {KS} = set(obj, Tuple, values(NamedTuple{KS}(val)))
function set(obj::NamedTuple, ::Type{NamedTuple{KS}}, val::NamedTuple) where {KS}
length(KS) == length(val) || throw(ArgumentError("Cannot assign NamedTuple with keys $KSV to NamedTuple with keys $KS"))
length(KS) == length(val) || throw(ArgumentError("Cannot assign NamedTuple with keys $(keys(val)) to NamedTuple with keys $KS"))
setproperties(obj, NamedTuple{KS}(val))
end

Expand Down
4 changes: 2 additions & 2 deletions src/getsetall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ getall(obj::AbstractString, ::Elements) = collect(obj)
getall(obj, ::Elements) = error("Elements() not supported for $(typeof(obj))")
getall(obj, ::Properties) = getproperties(obj) |> values
getall(obj, o::If) = o.modify_condition(obj) ? (obj,) : ()
getall(obj, f) = OpticStyle(f) == SetBased() ? (f(obj),) : error("`getall` not supported for $f")
getall(obj, o) = OpticStyle(o) == SetBased() ? (o(obj),) : error("`getall` not supported for $o")

function setall(obj, ::Properties, vs)
names = propertynames(obj)
Expand All @@ -80,7 +80,7 @@ function setall(obj, o::If, vs)
obj
end
end
setall(obj, o, vs) = OpticStyle(o) == SetBased() ? set(obj, o, only(vs)) : error("`setall` not supported for $f")
setall(obj, o, vs) = OpticStyle(o) == SetBased() ? set(obj, o, only(vs)) : error("`setall` not supported for $o")


# implementations for composite optics
Expand Down
8 changes: 4 additions & 4 deletions test/perf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ is_fast_composition_order(lens::ComposedOptic{<:Any, <:ComposedOptic}) = false
is_fast_composition_order(lens::ComposedOptic{<:ComposedOptic, <:ComposedOptic}) = false

@testset "default composition orders are fast" begin
@assert is_fast_composition_order((first, last, eltype))
@assert is_fast_composition_order((first last) eltype)
@assert !is_fast_composition_order(first (last eltype))
@test is_fast_composition_order((first, last, eltype))
@test is_fast_composition_order((first last) eltype)
@test !is_fast_composition_order(first (last eltype))
@test is_fast_composition_order(opcompose(eltype, last, first))
# @test_broken is_fast_composition_order(first ⨟ last ⨟ eltype)
@test_broken is_fast_composition_order(first last eltype)
@test is_fast_composition_order(first last eltype)
@test is_fast_composition_order(@optic _)
@test is_fast_composition_order(@optic _ |> first |> last |> eltype)
Expand Down
2 changes: 1 addition & 1 deletion test/test_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ end
o21 = TT(o211, o212)
o2 = TT(o21, o22)
obj = TT(o1, o2)
@assert obj === TT(2, TT(TT(3,(4,4)), 2))
@test obj === TT(2, TT(TT(3,(4,4)), 2))
i = 1
@testset "$lens" for (lens, val) [
((@optic _.a ), o1 ),
Expand Down

0 comments on commit 6d57418

Please sign in to comment.