Introduction

StaticRanges

StaticRanges.DynamicAxisType
DynamicAxis(n::Int)

A range spanning 1 to n by steps of 1. The value of n can be changed after construction, but cannot be any less than 0.

StaticRanges.GapRangeType
GapRange{T,F,L}

Represents a range that is broken up by gaps making it noncontinuous. This allows more compact storage of numbers where the majority are known to be continuous.

Examples

julia> using StaticRanges

julia> findall(and(>(4), <(10)), 1:10)
5-element Vector{Int64}:
 5
 6
 7
 8
 9

julia> find_all(or(<(4), >(6)), 1:10)
7-element GapRange{Int64, UnitRange{Int64}, UnitRange{Int64}}:
  1
  2
  3
  7
  8
  9
 10
StaticRanges.StaticRangeType
StaticRange{T,R}

Wraps a range parametrically, making it static.

julia> using StaticRanges

julia> StaticRange(1:10)
static(1:10)

julia> StaticRange{Float64}(1.0:1:10)
static(1.0:1.0:10.0)
StaticRanges.find_firstMethod
find_first(predicate::Function, A)

Return the index or key of the first element of A for which predicate returns true. Return nothing if there is no such element.

Indices or keys are of the same type as those returned by keys(A) and pairs(A).

Examples

julia> using StaticRanges

julia> A = [1, 4, 2, 2];

julia> find_first(iseven, A)
2

julia> find_first(x -> x>10, A) # returns nothing, but not printed in the REPL

julia> find_first(isequal(4), A)
2

julia> find_first(iseven, [1 4; 2 2])
CartesianIndex(2, 1)
StaticRanges.find_lastMethod
find_last(predicate::Function, A)

Return the index or key of the last element of A for which predicate returns true. Return nothing if there is no such element.

Indices or keys are of the same type as those returned by keys(A) and pairs(A).

Examples

julia> using StaticRanges

julia> find_last(iseven, [1, 4, 2, 2])
4

julia> find_last(x -> x>10, [1, 4, 2, 2]) # returns nothing, but not printed in the REPL

julia> find_last(isequal(4), [1, 4, 2, 2])
2

julia> find_last(iseven, [1 4; 2 2])
CartesianIndex(2, 2)

"Find" Functions

"find" methods (findall, findfirst, findlast, filter, and count) for operators that can produce fixed methods (e.g., <(1) -> Base.Fix2(<, 1)) have been optimized. This includes <, <=, >, >=, ==, and != operators.

For example, find_all is able to preserve ranges as opposed to findall.

julia> @btime findall(<(5), $fr)
  142.072 ns (3 allocations: 208 bytes)
2-element Array{Int64,1}:
 1
 2

julia> @btime find_all(<(5), $fr)
  0.027 ns (0 allocations: 0 bytes)
1:2
julia> r = 1:10
1:10

julia> mr = MutableRange(r)
UnitMRange(1:10)

julia> findall(or(<(4), >(12)), mr)
7-element Vector{Int64}:
  1
  2
  3
  7
  8
  9
 10

julia> find_all(or(<(4), >(6)), r)
7-element GapRange{Int64, UnitRange{Int64}, UnitRange{Int64}}:
  1
  2
  3
  7
  8
  9
 10

julia> @btime filter(or(<(4), >(6)), $r)
  124.496 ns (3 allocations: 320 bytes)
7-element Vector{Int64}:
  1
  2
  3
  7
  8
  9
 10

julia> @btime filter(or(<(4), >(6)), $mr)
  72.911 ns (3 allocations: 208 bytes)
7-element Vector{Int64,1}:
  1
  2
  3
  7
  8
  9
 10