Skip to content

Commit 0effa2a

Browse files
committed
more function optics
1 parent 270e78f commit 0effa2a

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
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_functionlenses.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ end
257257
# invertible lenses below: no need for extensive testing, simply forwarded to InverseFunctions
258258
inv, +, exp, sqrt, @optic(2 + _), @optic(_ * 3), @optic(log(2, _)),
259259
# non-invertible lenses, indirectly forwarded to InverseFunctions
260-
@optic(mod(_, 21)), @optic(fld(_, 3)), @optic(rem(_, 21)), @optic(div(_, 3)),
260+
@optic(mod(_, 21)), @optic(fld(_, 3)), @optic(rem(_, 21)), @optic(div(_, 3)), @optic(mod(_, 1:22)),
261261
]
262262
x = 5
263263
test_getset_laws(o, x, 10, 20; cmp=isapprox)
@@ -272,6 +272,7 @@ end
272272
@test set(0+0im, abs, 10) == 10
273273
@test set(0+1e-100im, abs, 10) == 10im
274274
@test_throws DomainError @set(abs(x) = -10)
275+
test_getset_laws(abs2, 1+2im, 3, 4, cmp=())
275276

276277
# composition
277278
o = @optic 1/(1 + exp(-_))
@@ -362,6 +363,9 @@ end
362363
test_getset_laws(@optic(lstrip(==(' '), _)), " abc ", "def", "")
363364
test_getset_laws(@optic(rstrip(==(' '), _)), " abc ", "def", "")
364365
test_getset_laws(@optic(strip(==(' '), _)), " abc ", "def", "")
366+
test_getset_laws(chomp, "abc", "def", "")
367+
test_getset_laws(chomp, "abc\n", "def", "")
368+
test_getset_laws(chomp, "abc\n\n", "def\n", "")
365369

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

0 commit comments

Comments
 (0)