-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
109 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module AccessorsDatesExt | ||
|
||
using Accessors | ||
import Accessors: set | ||
using Dates | ||
|
||
set(x::DateTime, ::Type{Date}, y) = DateTime(y, Time(x)) | ||
set(x::DateTime, ::Type{Time}, y) = DateTime(Date(x), y) | ||
set(x::T, ::Type{T}, y) where {T <: Union{Date, Time}} = y | ||
|
||
# directly mirrors Dates.value implementation in stdlib | ||
set(x::Date, ::typeof(Dates.value), y) = @set x.instant.periods.value = y | ||
set(x::DateTime, ::typeof(Dates.value), y) = @set x.instant.periods.value = y | ||
set(x::Time, ::typeof(Dates.value), y) = @set x.instant.value = y | ||
|
||
set(x::Date, ::typeof(year), y) = Date(y, month(x), day(x)) | ||
set(x::Date, ::typeof(month), y) = Date(year(x), y, day(x)) | ||
set(x::Date, ::typeof(day), y) = Date(year(x), month(x), y) | ||
set(x::Date, ::typeof(yearmonth), y::NTuple{2}) = Date(y..., day(x)) | ||
set(x::Date, ::typeof(monthday), y::NTuple{2}) = Date(year(x), y...) | ||
set(x::Date, ::typeof(yearmonthday), y::NTuple{3}) = Date(y...) | ||
set(x::Date, ::typeof(dayofweek), y) = firstdayofweek(x) + Day(y - 1) | ||
|
||
set(x::Time, ::typeof(hour), y) = Time(y, minute(x), second(x), millisecond(x), microsecond(x), nanosecond(x)) | ||
set(x::Time, ::typeof(minute), y) = Time(hour(x), y, second(x), millisecond(x), microsecond(x), nanosecond(x)) | ||
set(x::Time, ::typeof(second), y) = Time(hour(x), minute(x), y, millisecond(x), microsecond(x), nanosecond(x)) | ||
set(x::Time, ::typeof(millisecond), y) = Time(hour(x), minute(x), second(x), y, microsecond(x), nanosecond(x)) | ||
set(x::Time, ::typeof(microsecond), y) = Time(hour(x), minute(x), second(x), millisecond(x), y, nanosecond(x)) | ||
set(x::Time, ::typeof(nanosecond), y) = Time(hour(x), minute(x), second(x), millisecond(x), microsecond(x), y) | ||
|
||
set(x::DateTime, optic::Union{typeof.((year, month, day, yearmonth, monthday, yearmonthday, dayofweek))...}, y) = modify(d -> set(d, optic, y), x, Date) | ||
set(x::DateTime, optic::Union{typeof.((hour, minute, second, millisecond))...}, y) = modify(d -> set(d, optic, y), x, Time) | ||
|
||
|
||
set(x::AbstractString, optic::Base.Fix2{Type{T}}, dt::T) where {T <: Union{Date, Time, DateTime}} = Dates.format(dt, optic.x) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module AccessorsLinearAlgebraExt | ||
|
||
import Accessors: set | ||
using LinearAlgebra: norm, normalize | ||
|
||
set(arr, ::typeof(normalize), val) = norm(arr) * val | ||
set(arr, ::typeof(norm), val) = map(Base.Fix2(*, val / norm(arr)), arr) # should we check val is positive? | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module AccessorsTestExt | ||
|
||
using Accessors | ||
using Test: @test | ||
|
||
function Accessors.test_getset_laws(lens, obj, val1, val2; cmp=(==)) | ||
# set ⨟ get | ||
val = lens(obj) | ||
@test cmp(set(obj, lens, val), obj) | ||
|
||
# get ⨟ set | ||
obj1 = set(obj, lens, val1) | ||
@test cmp(lens(obj1), val1) | ||
|
||
# set idempotent | ||
obj12 = set(obj1, lens, val2) | ||
obj2 = set(obj12, lens, val2) | ||
@test cmp(obj12, obj2) | ||
end | ||
|
||
function Accessors.test_modify_law(f, lens, obj) | ||
obj_modify = modify(f, obj, lens) | ||
old_val = lens(obj) | ||
val = f(old_val) | ||
obj_setfget = set(obj, lens, val) | ||
@test obj_modify == obj_setfget | ||
end | ||
|
||
function Accessors.test_getsetall_laws(optic, obj, vals1, vals2; cmp=(==)) | ||
# setall ⨟ getall | ||
vals = getall(obj, optic) | ||
@test cmp(setall(obj, optic, vals), obj) | ||
|
||
# getall ⨟ setall | ||
obj1 = setall(obj, optic, vals1) | ||
@test cmp(collect(getall(obj1, optic)), collect(vals1)) | ||
|
||
# setall idempotent | ||
obj12 = setall(obj1, optic, vals2) | ||
obj2 = setall(obj12, optic, vals2) | ||
@test obj12 == obj2 | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,5 @@ | ||
using Test: @test | ||
function test_getset_laws(lens, obj, val1, val2; cmp=(==)) | ||
|
||
# set ⨟ get | ||
val = lens(obj) | ||
@test cmp(set(obj, lens, val), obj) | ||
|
||
# get ⨟ set | ||
obj1 = set(obj, lens, val1) | ||
@test cmp(lens(obj1), val1) | ||
|
||
# set idempotent | ||
obj12 = set(obj1, lens, val2) | ||
obj2 = set(obj12, lens, val2) | ||
@test cmp(obj12, obj2) | ||
end | ||
|
||
function test_modify_law(f, lens, obj) | ||
obj_modify = modify(f, obj, lens) | ||
old_val = lens(obj) | ||
val = f(old_val) | ||
obj_setfget = set(obj, lens, val) | ||
@test obj_modify == obj_setfget | ||
end | ||
|
||
function test_getsetall_laws(optic, obj, vals1, vals2; cmp=(==)) | ||
|
||
# setall ⨟ getall | ||
vals = getall(obj, optic) | ||
@test cmp(setall(obj, optic, vals), obj) | ||
|
||
# getall ⨟ setall | ||
obj1 = setall(obj, optic, vals1) | ||
@test cmp(collect(getall(obj1, optic)), collect(vals1)) | ||
|
||
# setall idempotent | ||
obj12 = setall(obj1, optic, vals2) | ||
obj2 = setall(obj12, optic, vals2) | ||
@test obj12 == obj2 | ||
end | ||
# placeholders only | ||
# actual definitions in ext/AccessorsTestExt.jl | ||
function test_getset_laws end | ||
function test_modify_law end | ||
function test_getsetall_laws end |