![]() First, let to see a basic example of a range operator. The range operators can be used with for loops, if conditions or even in when operator. A range can be defined with a start value and an end value with and without inclusion. This assumes Double ranges it could be made to handle other types too, though the type parameters get messy. Range Operator in Kotlin is a basic operator that is used to operate over a range. (This should work for negative numbers and for descending ranges the intersection is always given as ascending. ![]() ![]() And having intersect return null if there's no overlap is safer. Having the size explicit and separate from the intersection makes each one easier to understand, easier to test, and easier to reuse. With those, you can do e.g.: val a = 22.0. MaxOf(min, other.min).rangeTo(minOf(max, other.max)) = if (min <= other.max & other.min <= max) Infix fun ClosedFloatingPointRange.intersect(other: ClosedFloatingPointRange) So, building on what Adam Millerchip and cactustictacs have already done, I think I'd break the problem down into simpler parts: min, max, and size properties for a range, and an intersect function that calculates the common subrange of two ranges (if there is one): val ClosedFloatingPointRange.min What you're doing here is taking the intersection (overlap) of two ranges, and calculating its size.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |