Hullaballoonatic
07/19/2019, 6:57 PM(0 to 0)..(1 to 1)
iterates through 0 to 0, 0 to 1, 1 to 0, 1 to 1
. Might be a stretch, but I think it's intuitive.
operator fun Pair<Int, Int>.rangeTo(that: Pair<Int, Int>) = object : Iterable<Pair<Int, Int>> {
override fun iterator() = object : Iterator<Pair<Int, Int>> {
var i = this@rangeTo.first
var j = this@rangeTo.second
val m = that.first - i
val n = that.second - j
override fun hasNext() = i <= m && j <= n
override fun next(): Pair<Int, Int> {
val res = i to j
if (j == n) {
j = 0
i++
} else {
j++
}
return res
}
}
}
could also be expanded to higher dimensions with List<Int>.rangeTo(other: List<Int>)
, IntArray
, etcDico
07/19/2019, 7:12 PMHullaballoonatic
07/19/2019, 7:13 PMVector
type, which Kotlin does not haveDico
07/19/2019, 7:19 PMdata class
which partially covers the use case of Vector
karelpeeters
07/19/2019, 7:33 PMkarelpeeters
07/19/2019, 7:34 PM(0..1) cart (0..1)
but it would also work for other iterables.Hullaballoonatic
07/19/2019, 7:34 PMoperator fun <T> Iterable<T>.rangeTo(other: Iterable<T>) = this cart other
karelpeeters
07/19/2019, 7:51 PMDico
07/19/2019, 11:07 PMkarelpeeters
07/19/2019, 11:37 PM..
to represent cartesian product? Isn't that something different from the code sample, where ..
is a kind of specific range-cartesian on pairs?Hullaballoonatic
07/19/2019, 11:38 PM