From ec17e62ed9ac25c546f79cbf09018f9f82a6ed66 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Fri, 14 May 2021 04:36:04 -0400 Subject: [PATCH] RFC: Support JuliaFolds interfaces --- Project.toml | 6 +++++- src/SentinelArrays.jl | 1 + src/folds.jl | 17 +++++++++++++++++ test/runtests.jl | 21 ++++++++++++++++++++- 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 src/folds.jl diff --git a/Project.toml b/Project.toml index 73ac73c..6303885 100644 --- a/Project.toml +++ b/Project.toml @@ -5,13 +5,17 @@ version = "1.2.16" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" +FGenerators = "4fd0377b-cfdc-4941-97f4-8d7ddbb8981e" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +SplittablesBase = "171d559e-b47b-412a-8079-5efa626c420e" [compat] julia = "1" [extras] +Folds = "41a02a25-b8f0-4f67-bc48-60067656b558" +SplittablesTesting = "3bda5eb5-c32a-4f64-8618-df3be8968470" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Test"] +test = ["Folds", "SplittablesTesting", "Test"] diff --git a/src/SentinelArrays.jl b/src/SentinelArrays.jl index 487227e..c110b0c 100644 --- a/src/SentinelArrays.jl +++ b/src/SentinelArrays.jl @@ -451,6 +451,7 @@ end include("chainedvector.jl") include("missingvector.jl") +include("folds.jl") include("precompile.jl") _precompile_() diff --git a/src/folds.jl b/src/folds.jl new file mode 100644 index 0000000..2d226ce --- /dev/null +++ b/src/folds.jl @@ -0,0 +1,17 @@ +using FGenerators: @fgenerator, @yield +using SplittablesBase: SplittablesBase + +@fgenerator(A::ChainedVector) do + for array in A.arrays + for x in array + @yield x + end + end +end + +function SplittablesBase.halve(A::ChainedVector) + chunk = searchsortedfirst(A.inds, length(A) รท 2) + left = @view A.arrays[1:chunk] + right = @view A.arrays[chunk+1:end] + return (Iterators.flatten(left), Iterators.flatten(right)) +end diff --git a/test/runtests.jl b/test/runtests.jl index 842bc6a..e91c121 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ -using SentinelArrays, Test, Random +using SentinelArrays, Test, Random, SplittablesTesting, Folds @testset "SentinelArrays" begin @@ -580,4 +580,23 @@ deleteat!(c2, Int[]) end end +unitrange_chainedvectors = map(1:100) do seed + rng = MersenneTwister(seed) + ends = cumsum(rand(rng, 0:9, rand(rng, 1:100))) + starts = pushfirst!(ends[1:end-1] .+ 1, 1) + cv = ChainedVector((:).(starts, ends)) + return (label = "seed=$seed", data = cv) +end + +@testset "Folds" begin + @testset "$label" for (label, data) in unitrange_chainedvectors + @test Folds.collect(data, SequentialEx()) == 1:length(data) + @test Folds.collect(data) == 1:length(data) + end +end + +@testset "SplittablesBase" begin + SplittablesTesting.test_unordered(unitrange_chainedvectors) +end + end