-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
PharmCat
committed
Aug 9, 2021
1 parent
16c48d9
commit 566100d
Showing
6 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
################################################################################ | ||
struct SkipNonPositive{T} | ||
x::T | ||
end | ||
skipnonpositive(itr) = SkipNonPositive(itr) | ||
|
||
Base.IteratorEltype(::Type{SkipNonPositive{T}}) where {T} = Base.IteratorEltype(T) | ||
#Base.eltype(::Type{SkipNonPositive{T}}) where {T} = nonmissingtype(eltype(T)) | ||
function Base.iterate(itr::SkipNonPositive, state...) | ||
y = iterate(itr.x, state...) | ||
y === nothing && return nothing | ||
item, state = y | ||
while !ispositive(item) | ||
y = iterate(itr.x, state) | ||
y === nothing && return nothing | ||
item, state = y | ||
end | ||
item, state | ||
end | ||
Base.IndexStyle(::Type{<:SkipNonPositive{T}}) where {T} = IndexStyle(T) | ||
Base.eachindex(itr::SkipNonPositive) = | ||
Iterators.filter(i -> ispositive(@inbounds(itr.x[i])), eachindex(itr.x)) | ||
Base.keys(itr::SkipNonPositive) = | ||
Iterators.filter(i -> ispositive(@inbounds(itr.x[i])), keys(itr.x)) | ||
Base.@propagate_inbounds function getindex(itr::SkipNonPositive, I...) | ||
v = itr.x[I...] | ||
!ispositive(v) && throw(ErrorException("the value at index $I is non positive")) | ||
v | ||
end | ||
function Base.length(itr::SkipNonPositive) | ||
n = 0 | ||
for i in itr n+=1 end | ||
n | ||
end | ||
################################################################################ | ||
struct SkipNaNorMissing{T} | ||
x::T | ||
end | ||
skipnanormissing(itr) = SkipNaNorMissing(itr) | ||
|
||
Base.IteratorEltype(::Type{SkipNaNorMissing{T}}) where {T} = Base.IteratorEltype(T) | ||
#Base.eltype(::Type{SkipNaNorMissing{T}}) where {T} = nonmissingtype(eltype(T)) | ||
function Base.iterate(itr::SkipNaNorMissing, state...) | ||
y = iterate(itr.x, state...) | ||
y === nothing && return nothing | ||
item, state = y | ||
while isnanormissing(item) | ||
y = iterate(itr.x, state) | ||
y === nothing && return nothing | ||
item, state = y | ||
end | ||
item, state | ||
end | ||
Base.IndexStyle(::Type{<:SkipNaNorMissing{T}}) where {T} = IndexStyle(T) | ||
Base.eachindex(itr::SkipNaNorMissing) = | ||
Iterators.filter(i -> isnanormissing(@inbounds(itr.x[i])), eachindex(itr.x)) | ||
Base.keys(itr::SkipNaNorMissing) = | ||
Iterators.filter(i -> isnanormissing(@inbounds(itr.x[i])), keys(itr.x)) | ||
Base.@propagate_inbounds function getindex(itr::SkipNaNorMissing, I...) | ||
v = itr.x[I...] | ||
!isnanormissing(v) && throw(ErrorException("The value at index $I is NaN or missing!")) | ||
v | ||
end | ||
function Base.length(itr::SkipNaNorMissing) | ||
n = 0 | ||
for i in itr n+=1 end | ||
n | ||
end | ||
################################################################################ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters