Skip to content

Commit

Permalink
improve r_split
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Sep 22, 2024
1 parent ee93dae commit e713dcb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/r_base.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import DataStructures: OrderedDict
# using LoopVectorization: @turbo

# function r_in(x::AbstractVector, y::AbstractVector)::BitVector
Expand Down Expand Up @@ -59,20 +60,19 @@ end
# DataFrame(vals, names)
# end


function r_split(lst::AbstractVector, by::AbstractVector)
grps = unique(by) |> sort
res = []
res = OrderedDict()
for grp in grps
inds = by .== grp
res[grp] = lst[inds]
# ans = Dict(grp => lst[inds])
ans = lst[inds]
push!(res, ans)
# ans = lst[inds]
# push!(res, ans)
end
res
end


r_summary(x; digits=2) = nothing

function r_summary(x::AbstractArray{<:Real}; digits=2)
Expand Down
5 changes: 3 additions & 2 deletions test/test-r_base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ end


@testset "r_split" begin
@test r_split(1:4, [1, 1, 1, 2]) == [[1, 2, 3], [4]]
@test r_split(1:4, [1, 1, 2, 2]) == [[1, 2], [3, 4]]
r = r_split(1:4, [1, 1, 1, 2])
@test r[1] == [1, 2, 3]
@test r[2] == [4]
end

@testset "writelines" begin
Expand Down

0 comments on commit e713dcb

Please sign in to comment.