Skip to content

Commit b224dac

Browse files
greimelnalimilan
andcommitted
Update src/extras.jl according to @nalimilan's comments
Co-Authored-By: Milan Bouchet-Valat <[email protected]>
1 parent d1a3461 commit b224dac

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/extras.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ end
4646
4747
Provide the default label format for the `cut` function.
4848
"""
49-
_default_formatter_(from, to, i; extend=false) = string("[", from, ", ", to, extend ? "]" : ")")
49+
default_formatter(from, to, i; extend=false) = string("[", from, ", ", to, extend ? "]" : ")")
5050

5151
"""
5252
cut(x::AbstractArray, breaks::AbstractVector;
53-
extend::Bool=false, labels::AbstractVector=[], allow_missing::Bool=false)
53+
labels::Union{AbstractVector{<:AbstractString},Function},
54+
extend::Bool=false, allow_missing::Bool=false)
5455
5556
Cut a numeric array into intervals and return an ordered `CategoricalArray` indicating
5657
the interval into which each entry falls. Intervals are of the form `[lower, upper)`,
@@ -63,15 +64,17 @@ also accept them.
6364
* `extend::Bool=false`: when `false`, an error is raised if some values in `x` fall
6465
outside of the breaks; when `true`, breaks are automatically added to include all
6566
values in `x`, and the upper bound is included in the last interval.
66-
* `labels::Union{AbstractVector,Function}=_default_formatter_`: a vector of strings giving the names to use for the
67-
intervals; or a function `f(from,to,i;extend=false)` that generates the labels from the left and right interval boundaries and the group index. Defaults to `string("[", from, ", ", to, extend ? "]" : ")")`, e.g. `"[1, 5)"`.
67+
* `labels::Union{AbstractVector,Function}: a vector of strings giving the names to use for
68+
the intervals; or a function `f(from, to, i; extend)` that generates the labels from the
69+
left and right interval boundaries and the group index. Defaults to
70+
`string("[", from, ", ", to, extend ? "]" : ")")`, e.g. `"[1, 5)"`.
6871
* `allow_missing::Bool=true`: when `true`, values outside of breaks result in missing values.
6972
only supported when `x` accepts missing values.
7073
"""
7174
function cut(x::AbstractArray{T, N}, breaks::AbstractVector;
72-
extend::Bool=false, labels=_default_formatter_,
73-
allow_missing::Bool=false) where {T, N, U<:AbstractString}
74-
(labels isa AbstractVector) || (labels isa Function) || throw(ArgumentError("labels must be a formatter function or an AbstractVector"))
75+
extend::Bool=false,
76+
labels::Union{AbstractVector{<:AbstractString},Function}=default_formatter,
77+
allow_missing::Bool=false) where {T, N}
7578

7679
if !issorted(breaks)
7780
breaks = sort(breaks)
@@ -137,8 +140,5 @@ Cut a numeric array into `ngroups` quantiles, determined using
137140
[`quantile`](@ref).
138141
"""
139142
cut(x::AbstractArray, ngroups::Integer;
140-
labels=_default_formatter_) =
141-
cut(x, Statistics.quantile(x, (1:ngroups-1)/ngroups); extend=true, labels=labels)
142-
143-
144-
143+
labels::Union{AbstractVector{<:AbstractString},Function}=default_formatter) =
144+
cut(x, Statistics.quantile(x, (1:ngroups-1)/ngroups); extend=true, labels=labels)

test/15_extras.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ end
109109
end
110110

111111
@testset "formatter function" begin
112-
my_formatter1(from,to,i;extend=false) = "group $i"
113-
my_formatter2(from,to,i;extend=false) = "$i: $from -- $to"
114-
function my_formatter3(from,to,i;extend=true)
112+
my_formatter1(from, to, i; extend) = "group $i"
113+
my_formatter2(from, to, i; extend) = "$i: $from -- $to"
114+
function my_formatter3(from, to, i; extend)
115115
percentile(x) = Int(round(100 * parse.(Float64,x),digits=0))
116116
string("P",percentile(from),"P",percentile(to))
117117
end

0 commit comments

Comments
 (0)