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

more setters for collections #96

Merged
merged 4 commits into from
Apr 2, 2023
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
26 changes: 25 additions & 1 deletion src/functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ function set(x::TX, f::Base.Fix1{typeof(convert)}, v) where {TX}
convert(TX, v)
end

set(obj::Tuple, ::Type{Tuple}, val::Tuple) = val
set(obj::NamedTuple{KS}, ::Type{Tuple}, val::Tuple) where {KS} = NamedTuple{KS}(val)
set(obj::CartesianIndex, ::Type{Tuple}, val::Tuple) = CartesianIndex(val)
aplavin marked this conversation as resolved.
Show resolved Hide resolved
set(obj::AbstractVector, ::Type{Tuple}, val::Tuple) = similar(obj, eltype(val)) .= val
aplavin marked this conversation as resolved.
Show resolved Hide resolved

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"))
setproperties(obj, NamedTuple{KS}(val))
end

################################################################################
##### eltype
################################################################################
Expand All @@ -47,7 +58,7 @@ set(obj::Type{<:Dict{<:Any,V}}, lens::typeof(keytype), ::Type{K}) where {K,V} =
set(obj::Type{<:Dict{K}}, lens::typeof(valtype), ::Type{V}) where {K,V} = Dict{K,V}

################################################################################
##### array shapes
##### arrays
################################################################################
set(obj, ::typeof(size), v::Tuple) = reshape(obj, v)

Expand All @@ -66,6 +77,19 @@ function set(x::AbstractVector, ::typeof(reverse), v::AbstractVector)
res
end


set(obj, o::Base.Fix1{typeof(map)}, val) = map((ob, v) -> set(ob, o.x, v), obj, val)

set(obj, o::Base.Fix1{typeof(filter)}, val) = @set obj[findall(o.x, obj)] = val
modify(f, obj, o::Base.Fix1{typeof(filter)}) = @modify(f, obj[findall(o.x, obj)])
delete(obj, o::Base.Fix1{typeof(filter)}) = filter(!o.x, obj)

set(obj, o::typeof(skipmissing), val) = @set obj |> filter(!ismissing, _) = collect(val)
modify(f, obj, o::typeof(skipmissing)) = @modify(f, obj |> filter(!ismissing, _))

set(obj, ::typeof(sort), val) = @set obj[sortperm(obj)] = val
modify(f, obj, ::typeof(sort)) = @modify(f, obj[sortperm(obj)])

################################################################################
##### os
################################################################################
Expand Down
3 changes: 3 additions & 0 deletions src/optics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ function modify(f, obj, w::If)
end
end

# it doesn't seem possible to support delete(If) for collections
delete(obj, o::If) = error("`delete(obj, ::If)` not supported, try using `filter` as an optic instead")

"""
mapproperties(f, obj)

Expand Down
2 changes: 1 addition & 1 deletion src/testing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function test_getset_laws(lens, obj, val1, val2; cmp=(==))
# set idempotent
obj12 = set(obj1, lens, val2)
obj2 = set(obj12, lens, val2)
@test obj12 == obj2
@test cmp(obj12, obj2)
end

function test_modify_law(f, lens, obj)
Expand Down
3 changes: 3 additions & 0 deletions test/test_delete.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ using StaticArrays
@test_throws BoundsError delete(A, @optic(_[10]))
@test delete(A, @optic(_[end])) == [1, 2]
@test A == [1, 2, 3] # not changed

@test_throws ErrorException delete(A, @optic _ |> Elements() |> If(isodd))
@test delete(A, @optic filter(isodd, _)) == [2]
end
let A = Dict("a" => 1, "b" => 2, "c" => 3)
@test delete(A, @optic(_["a"])) == Dict("b" => 2, "c" => 3)
Expand Down
34 changes: 31 additions & 3 deletions test/test_functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using Test
using Dates
using Unitful
using InverseFunctions: inverse
using Accessors: test_getset_laws
using Accessors: test_getset_laws, test_modify_law
using Accessors

@testset "os" begin
Expand Down Expand Up @@ -76,11 +76,29 @@ end
Accessors.test_getset_laws(Base.tail, obj, (123,), ("456", 7))
end

@testset "convert" begin
@testset "change types" begin
x = Second(180)
@test @modify(m -> m + 1, x |> convert(Minute, _).value) === Second(240)
@test_throws ArgumentError @set x |> convert(Minute, _) = 123
test_getset_laws(@optic(convert(Minute, _)), x, Minute(10), Minute(20))

test_getset_laws(Tuple, (1, 'a'), ('x', 'y'), (1, 2))
test_getset_laws(Tuple, (a=1, b='a'), ('x', 'y'), (1, 2))
test_getset_laws(Tuple, [0, 1], ('x', 'y'), (1, 2))
test_getset_laws(Tuple, CartesianIndex(1, 2), (3, 4), (5, 6))

cmp(a::NamedTuple, b::NamedTuple) = Set(keys(a)) == Set(keys(b)) && NamedTuple{keys(b)}(a) === b
cmp(a::T, b::T) where {T} = a == b
test_getset_laws(NamedTuple{(:x, :y)}, (1, 'a'), (x='x', y='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, (1, 'a'), (y='x', x='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, (y=1, x='a'), (x='x', y='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, (y=1, x='a'), (y='x', x='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, (y=1, z=10, x='a'), (x='x', y='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, (y=1, z=10, x='a'), (y='x', x='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, [0, 1], (x='x', y='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, [0, 1], (y='x', x='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, CartesianIndex(1, 2), (x=3, y=4), (x=5, y=6); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, CartesianIndex(1, 2), (y=3, x=4), (x=5, y=6); cmp=cmp)
end

@testset "eltype on Number" begin
Expand Down Expand Up @@ -125,7 +143,7 @@ end
@test typeof(@set eltype(obj) = Pair{UInt, Float64}) === Dict{UInt, Float64}
end

@testset "array shapes" begin
@testset "arrays" begin
A = [1 2 3; 4 5 6]

B = @insert size(A)[2] = 1
Expand All @@ -146,6 +164,16 @@ end
test_getset_laws(size, A, (1, 6), (3, 2))
test_getset_laws(vec, A, 10:15, 21:26)
test_getset_laws(reverse, collect(1:6), 10:15, 21:26)

@test @inferred(modify(x -> x ./ sum(x), [1, -2, 3], @optic filter(>(0), _))) == [0.25, -2, 0.75]
@test isequal(modify(x -> x ./ sum(x), [1, missing, 3], skipmissing), [0.25, missing, 0.75])
@test modify(cumsum, [2, 3, 1], sort) == [3, 6, 1]

test_getset_laws(@optic(map(first, _)), [(1,), (2,)], [(3,), (4,)], [(5,), (6,)])
test_getset_laws(@optic(filter(>(0), _)), [1, -2, 3, -4, 5, -6], [1, 2, 3], [1, 3, 5])
test_modify_law(reverse, @optic(filter(>(0), _)), [1, -2, 3, -4, 5, -6])
test_getset_laws(skipmissing, [1, missing, 3], [0, 1], [5, 6]; cmp=(x,y) -> isequal(collect(x), collect(y)))
test_modify_law(cumsum, sort, [1, -2, 3, -4, 5, -6])
end

@testset "math" begin
Expand Down