It seems the min and max functions abort the sieve if the first pass doesn't yield a value.
The closure passed min or max needs to be consistent in comparing orders.
In a consistent ordering, one (and only one) of the following gets true:
a < b
a == b
a > b
Your closure {$0 != 0...0 && $1 != 0...0 && $0.lowerBound < $1.lowerBound} may return false,
for both yourClosure(a,b) and yourClosure(b,a) even when a != b.
Don't you think it is strange for a comparison function?
In the header doc of min or max, you can find this description:
The predicate must be a strict weak ordering over the elements. That
is, for any elements a, b, and c, the following conditions must
hold:
areInIncreasingOrder(a, a) is always false. (Irreflexivity)
If areInIncreasingOrder(a, b) and areInIncreasingOrder(b, c) are
both true, then areInIncreasingOrder(a, c) is also
true. (Transitive comparability)
Two elements are incomparable if neither is ordered before the other
according to the predicate. If a and b are incomparable, and b
and c are incomparable, then a and c are also incomparable.
(Transitive incomparability)
It is not clear enough, but I guess you want to do something like this:
import UIKit
/*
let ranges = [
//1...4,
0...0,
0...1,
3...5,
0...0,
2...4,
3...7,
]
*/
let ranges = [
1...4,
0...0,
1...6,
3...5,
0...0,
2...4,
3...7,
]
let defaultMinValue = 0
let ordinateMinimum = CGFloat(
ranges
.filter{$0 != 0...0}
.min(by: {$0.lowerBound < $1.lowerBound})?
.lowerBound ?? defaultMinValue
)
let defaultMaxValue = Int.max
let ordinateMaximum = CGFloat(
ranges
.filter{$0 != 0...0}
.max(by: {$0.upperBound < $1.upperBound})?
.upperBound ?? defaultMaxValue
)
print("ordinateMinimum: \(ordinateMinimum)\nordinateMaximum: \(ordinateMaximum)")
One more.
You should not include two or more topics in a single thread. For example, if you made a separate thread for this min, max issue, more readers who were suffering from the same issue would be able to find the thread more easily.