update
This commit is contained in:
65
src/util.jl
65
src/util.jl
@@ -6,7 +6,7 @@ export timedifference, showstracktrace, findHighestIndexKey, uuid4snakecase, rep
|
|||||||
dfToString, dataframe_to_json_list, dictToString, dictToString_noKey,
|
dfToString, dataframe_to_json_list, dictToString, dictToString_noKey,
|
||||||
dictToString_numbering, extract_triple_backtick_text,
|
dictToString_numbering, extract_triple_backtick_text,
|
||||||
countGivenWords, remove_french_accents, detect_keyword, extractTextBetweenCharacter,
|
countGivenWords, remove_french_accents, detect_keyword, extractTextBetweenCharacter,
|
||||||
convertCamelSnakeKebabCase, fitrange, lastElementsIndex
|
convertCamelSnakeKebabCase, fitrange, recentElementsIndex, nonRecentElementsIndex
|
||||||
|
|
||||||
using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames
|
using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames
|
||||||
|
|
||||||
@@ -1182,32 +1182,75 @@ end
|
|||||||
""" Find a unit range for a vector given a number of the most recent elements of interest.
|
""" Find a unit range for a vector given a number of the most recent elements of interest.
|
||||||
|
|
||||||
# Arguments
|
# Arguments
|
||||||
- `v::AbstractVector`
|
- `vectorLength::Integer`
|
||||||
the input vector to extract recent elements from
|
the length of the vector to generate range from
|
||||||
- `n::Integer`
|
- `n::Integer`
|
||||||
the number of most recent elements to include in the range
|
the number of most recent elements to include in range
|
||||||
|
|
||||||
|
|
||||||
# Return
|
# Return
|
||||||
- `UnitRange`
|
- `UnitRange`
|
||||||
a range representing the last `n` elements of the vector
|
a range representing the n most recent elements of a vector with length vectorLength
|
||||||
|
|
||||||
# Example
|
# Example
|
||||||
```jldoctest
|
```jldoctest
|
||||||
julia> a = [1, 2, 3, 4, 5]
|
julia> a = [1, 2, 3, 4, 5]
|
||||||
julia> lastElementsIndex(a, 3)
|
julia> recentElementsIndex(length(a), 3)
|
||||||
3:5
|
3:5
|
||||||
|
julia> recentElementsIndex(length(a), 0)
|
||||||
|
5:5
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
function lastElementsIndex(v::AbstractVector, n::Integer)
|
function recentElementsIndex(vectorlength::Integer, n::Integer)
|
||||||
len = length(v)
|
|
||||||
if n == 0
|
if n == 0
|
||||||
return 1:len
|
error("n must be greater than 0")
|
||||||
end
|
end
|
||||||
|
|
||||||
start = max(1, len - n + 1)
|
start = max(1, vectorlength - n + 1)
|
||||||
return start:len
|
return start:vectorlength
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
""" Find a unit range for a vector excluding the most recent elements.
|
||||||
|
|
||||||
|
# Arguments
|
||||||
|
- `vectorlength::Integer`
|
||||||
|
the length of the vector to generate range from
|
||||||
|
- `n::Integer`
|
||||||
|
the number of most recent elements to exclude from range
|
||||||
|
|
||||||
|
# Return
|
||||||
|
- `UnitRange`
|
||||||
|
a range representing the elements of the vector excluding the last `n` elements
|
||||||
|
|
||||||
|
# Example
|
||||||
|
```jldoctest
|
||||||
|
julia> a = [1, 2, 3, 4, 5]
|
||||||
|
julia> nonRecentElementsIndex(length(a), 3)
|
||||||
|
1:2
|
||||||
|
julia> nonRecentElementsIndex(length(a), 1)
|
||||||
|
1:4
|
||||||
|
julia> nonRecentElementsIndex(length(a), 0)
|
||||||
|
1:5
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
function nonRecentElementsIndex(vectorlength::Integer, n::Integer)
|
||||||
|
if n < 0
|
||||||
|
error("n must be non-negative")
|
||||||
|
end
|
||||||
|
if n > vectorlength
|
||||||
|
return 1:0 # empty range
|
||||||
|
end
|
||||||
|
return 1:(vectorlength-n)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
end # module util
|
end # module util
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
using Test
|
using Test
|
||||||
using GeneralUtils
|
using GeneralUtils
|
||||||
|
|
||||||
@testset "lastElementsIndex" begin
|
@testset "ealierElementsIndex" begin
|
||||||
@test GeneralUtils.lastElementsIndex([1,2,3,4,5], 3) == 3:5
|
@test GeneralUtils.ealierElementsIndex([1,2,3,4,5], 2) == 1:3
|
||||||
@test GeneralUtils.lastElementsIndex([1,2,3], 5) == 1:3
|
@test GeneralUtils.ealierElementsIndex([1,2,3], 0) == 1:3
|
||||||
@test GeneralUtils.lastElementsIndex([1], 1) == 1:1
|
@test GeneralUtils.ealierElementsIndex([1], 1) == 1:0
|
||||||
@test GeneralUtils.lastElementsIndex(collect(1:10), 4) == 7:10
|
@test GeneralUtils.ealierElementsIndex([], 0) == 1:0
|
||||||
@test GeneralUtils.lastElementsIndex(Float64[], 2) == 1:0
|
@test GeneralUtils.ealierElementsIndex([1,2,3,4], 4) == 1:0
|
||||||
@test GeneralUtils.lastElementsIndex([1,2,3], 0) == length([1,2,3])+1:length([1,2,3])
|
@test GeneralUtils.ealierElementsIndex([1,2,3,4], 5) == 1:0
|
||||||
@test GeneralUtils.lastElementsIndex(["a","b","c"], 2) == 2:3
|
@test GeneralUtils.ealierElementsIndex(collect(1:10), 3) == 1:7
|
||||||
|
@test_throws ErrorException GeneralUtils.ealierElementsIndex([1,2,3], -1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user