Skip to content

Commit 6087e2f

Browse files
authored
Merge pull request #163 from aplavin/moreoptics
more function optics
2 parents 4c297d1 + bb39276 commit 6087e2f

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/functionlenses.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,14 @@ function set(x, ::typeof(abs), y)
138138
s = sign(x)
139139
iszero(s) ? y * one(x) : y * s
140140
end
141+
set(x, ::typeof(abs2), y) = set(x, abs, y)
141142

142143
set(x, ::typeof(mod2pi), y) = set(x, @optic(mod(_, 2π)), y)
143144
set(x, f::Base.Fix2{typeof(fld)}, y) = set(x, @optic(first(fldmod(_, f.x))), y)
144145
set(x, f::Base.Fix2{typeof(mod)}, y) = set(x, @optic(last(fldmod(_, f.x))), y)
145146
set(x, f::Base.Fix2{typeof(div)}, y) = set(x, @optic(first(divrem(_, f.x))), y)
146147
set(x, f::Base.Fix2{typeof(rem)}, y) = set(x, @optic(last(divrem(_, f.x))), y)
148+
set(x, f::Base.Fix2{typeof(mod),<:AbstractUnitRange}, y) = @set mod($x - first(f.x), length(f.x)) + first(f.x) = y
147149

148150
set(x::AbstractString, f::Base.Fix1{typeof(parse), Type{T}}, y::T) where {T} = string(y)
149151

@@ -171,6 +173,7 @@ delete(s::AbstractString, o::typeof(last)) = chop(s; head=0, tail=1)
171173
delete(s::AbstractString, o::Base.Fix2{typeof(first)}) = chop(s; head=o.x, tail=0)
172174
delete(s::AbstractString, o::Base.Fix2{typeof(last)}) = chop(s; head=0, tail=o.x)
173175

176+
set(s::AbstractString, o::typeof(chomp), v) = endswith(s, '\n') ? v * '\n' : v
174177
if VERSION >= v"1.8"
175178
set(s::AbstractString, o::Base.Fix2{typeof(chopsuffix), <:AbstractString}, v) =
176179
endswith(s, o.x) ? v * o.x : v

test/test_extensions.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ VERSION >= v"1.9-" && @testset "IntervalSets" begin
6666
@test 2 === @set 2 |> mod(_, 20..23) = 20
6767
@test 33 === @set 32 |> mod(_, 20..23) = 21
6868
test_getset_laws(@optic(mod(_, 5..8)), 20, 6, 5)
69+
70+
@test_throws Exception mod(3, 1..0)
71+
@test_throws Exception @set mod($3, 1..0) = 1
72+
@test_throws Exception @set mod($3, 1..5) = 10
6973
end
7074

7175
@testset "StaticArrays" begin

test/test_functionlenses.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,19 @@ end
263263
test_getset_laws(o, x, 10, 20; cmp=isapprox)
264264
@inferred set(x, o, 10)
265265
end
266+
267+
@testset for (rng, x, y) in [
268+
(0:3, 2, 1),
269+
(0:3, 32, 1),
270+
(20:23, 2, 20),
271+
(20:23, 32, 21),
272+
(5:8, 20, 6),
273+
]
274+
test_getset_laws((@o mod(_, rng)), x, y, y+1)
275+
end
276+
@test_throws Exception mod(3, 1:0)
277+
@test_throws Exception @set mod($3, 1:0) = 1
278+
@test_throws Exception @set mod($3, 1:5) = 10
266279

267280
x = 3 + 4im
268281
@test @set(abs(-2u"m") = 1u"m") === -1u"m"
@@ -272,6 +285,7 @@ end
272285
@test set(0+0im, abs, 10) == 10
273286
@test set(0+1e-100im, abs, 10) == 10im
274287
@test_throws DomainError @set(abs(x) = -10)
288+
test_getset_laws(abs2, 1+2im, 3, 4, cmp=())
275289

276290
# composition
277291
o = @optic 1/(1 + exp(-_))
@@ -362,6 +376,9 @@ end
362376
test_getset_laws(@optic(lstrip(==(' '), _)), " abc ", "def", "")
363377
test_getset_laws(@optic(rstrip(==(' '), _)), " abc ", "def", "")
364378
test_getset_laws(@optic(strip(==(' '), _)), " abc ", "def", "")
379+
test_getset_laws(chomp, "abc", "def", "")
380+
test_getset_laws(chomp, "abc\n", "def", "")
381+
test_getset_laws(chomp, "abc\n\n", "def\n", "")
365382

366383
if VERSION >= v"1.8"
367384
test_getset_laws(@optic(chopprefix(_, "def")), "def abc", "xyz", "")

0 commit comments

Comments
 (0)