This commit is contained in:
narawat lamaiin
2025-03-27 13:09:20 +07:00
parent 840b0e6205
commit 562f528c01
3 changed files with 87 additions and 26 deletions

View File

@@ -6,7 +6,7 @@ export timedifference, showstracktrace, findHighestIndexKey, uuid4snakecase, rep
dfToString, dataframe_to_json_list, dictToString, dictToString_noKey,
dictToString_numbering, extract_triple_backtick_text,
countGivenWords, remove_french_accents, detect_keyword, extractTextBetweenCharacter,
convertCamelSnakeKebabCase
convertCamelSnakeKebabCase, fitrange, lastElementsIndex
using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames
@@ -1134,14 +1134,80 @@ true
````
"""
function issomething(x)
return x === nothing ? false : true
return x === nothing ? false : true
end
""" Adjust a given range to fit within the bounds of a vector's length.
# Arguments
- `v::T1`
the input vector to check against
- `range::UnitRange`
the original range to be adjusted
# Return
- `adjusted_range::UnitRange`
a range that is constrained to the vector's length, preventing out-of-bounds indexing
# Example
julia> v = [1, 2, 3, 4, 5]
julia> fitrange(v, 3:10)
3:5
"""
function fitrange(v::T1, range::UnitRange) where {T1<:AbstractVector}
totalelements = length(v)
startind =
# check if user put start range greater than total event
if range.start > totalelements
totalelements
else
range.start
end
stopind =
if range.stop > totalelements
totalelements
else
range.stop
end
return startind:stopind
end
""" Find a unit range for a vector given a number of the most recent elements of interest.
# Arguments
- `v::AbstractVector`
the input vector to extract recent elements from
- `n::Integer`
the number of most recent elements to include in the range
# Return
- `UnitRange`
a range representing the last `n` elements of the vector
# Example
```jldoctest
julia> a = [1, 2, 3, 4, 5]
julia> lastElementsIndex(a, 3)
3:5
```
"""
function lastElementsIndex(v::AbstractVector, n::Integer)
len = length(v)
if n == 0
return 1:len
end
start = max(1, len - n + 1)
return start:len
end
end # module util