Skip to content

Commit

Permalink
also delete(filter)
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Mar 31, 2023
1 parent c00686c commit e83c8b1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ 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)])

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
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 "not supported" 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

0 comments on commit e83c8b1

Please sign in to comment.