Hullaballoonatic
08/31/2018, 4:19 PMadam-mcneilly
08/31/2018, 4:20 PMHullaballoonatic
08/31/2018, 4:21 PMShawn
08/31/2018, 4:22 PMPair
or Triple
can do in a pinchHullaballoonatic
08/31/2018, 4:22 PMadam-mcneilly
08/31/2018, 4:22 PMHullaballoonatic
08/31/2018, 4:23 PMadam-mcneilly
08/31/2018, 4:23 PMfun foo(): Pair<Int, Int> { }
val (x, y) = foo()
Shawn
08/31/2018, 4:23 PMobject { ... }
expressionpotter
08/31/2018, 4:25 PMadam-mcneilly
08/31/2018, 4:25 PMList<T>
up to 5 items. Any more than that, and you'd need a data class.
I'd also argue that any more than that means you might want to question if destructuring is the most readable solution (you may say yes, but worth questioning after a point).Hullaballoonatic
08/31/2018, 4:26 PMpotter
08/31/2018, 4:27 PMadam-mcneilly
08/31/2018, 4:28 PMval (x, y, z) = arrayOf(5, 10, 5)
Or, looping through a map by destructuring the entry class:
for ((key, value) in map) { }
Hullaballoonatic
08/31/2018, 4:31 PMadam-mcneilly
08/31/2018, 4:31 PMpotter
08/31/2018, 4:32 PMadam-mcneilly
08/31/2018, 4:33 PM<T>
is for you list, that's what you have to destructure to.Hullaballoonatic
08/31/2018, 4:34 PMval arr = arrayOf(0,0,1,3,4,2....)
for (i in arr.indices step 2) {
arr[i]++
arr[i+1]++
}
using destructuring?potter
08/31/2018, 4:36 PMPair<x,y>
instead?adam-mcneilly
08/31/2018, 4:36 PMwindowed
I believe. But let me back up - why have an array of x1y1x2y2... and not a list of pairs?Shawn
08/31/2018, 4:37 PMchunked
, depending on your use caseHullaballoonatic
08/31/2018, 4:37 PMList<Pair<Int,Int>>
but I'm doing breadth first search and wanna be really efficient with memoryadam-mcneilly
08/31/2018, 4:37 PMHullaballoonatic
08/31/2018, 4:38 PMadam-mcneilly
08/31/2018, 4:38 PMchunked
is nicechunked
to get the List<Pair>
and then search through that. I guess the performance hit is that you go through the list twice - one to prettify it, one to handle the coordinates I would think - hard to know your exact use case.Hullaballoonatic
08/31/2018, 4:40 PMval children: List<ByteArray>
get() {
val result = ArrayList<ByteArray>()
val original = positions.toList().toImmutableList()
original.forEachIndexed { i, byte ->
result += original.set(i, (byte + 1).b).toByteArray()
result += original.set(i, (byte - 1).b).toByteArray()
}
return result
}
adam-mcneilly
08/31/2018, 4:41 PMpotter
08/31/2018, 4:41 PMHullaballoonatic
08/31/2018, 4:41 PMpotter
08/31/2018, 4:41 PMHullaballoonatic
08/31/2018, 4:41 PMadam-mcneilly
08/31/2018, 4:41 PMHullaballoonatic
08/31/2018, 4:42 PMpotter
08/31/2018, 4:42 PMHullaballoonatic
08/31/2018, 4:42 PMadam-mcneilly
08/31/2018, 4:42 PMHullaballoonatic
08/31/2018, 4:43 PMpotter
08/31/2018, 4:46 PMfun main(args: Array<String>) {
listOf(1, 1, 2, 2, 3, 3)
.asSequence()
.chunked(2) { Pair(it[0], it[1]) }
.map { println("${it.first},${it.second}") }
.toList()
}
1,1
2,2
3,3
Hullaballoonatic
08/31/2018, 4:47 PMpotter
08/31/2018, 4:48 PMPair
reference is held at a time, so it doesn't require a lot of memory (it'll create garbage, though)adam-mcneilly
08/31/2018, 4:48 PMHullaballoonatic
08/31/2018, 5:40 PMPair(it[0], it[1])
as it[0] to it[1]
potter
08/31/2018, 7:40 PM