i've been creating classes every time. i'll look i...
# announcements
h
i've been creating classes every time. i'll look into destructive declarations, thanks!
a
You'd still need to create a data class to be returned by the function, but then you can easily destructure at the call site. The docs linked have a good example of this.
h
yeah, this is awesome. i learned about two kotlin features today, and i'm all giddy
❤️ 1
s
Or, I mean,
Pair
or
Triple
can do in a pinch
2
h
yeah, i love pair
a
You can also destructure a pair!
So if you want to use it as the return type on your function, you can still use destructuring at the call site. 🙂
h
you mean like it.first and it.second?
a
Copy code
fun foo(): Pair<Int, Int> { }

val (x, y) = foo()
s
@potter ^
might be relevant to what you’re asking for
unless you want named accessors, in which case you’ll need an inline
object { ... }
expression
p
oh yeah, I forgot about those. still not a general solution (4+ returns), but covers most of my use cases
a
You can destructure a
List<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).
👍 1
h
yeah
if someone is returning like 8 distinct values from a fn then it is doing way more than it should
p
yeah, makes sense
a
Definitely. And if not that, then it means those 8 values are actually connected in some way, which leads me to believe they're better off as a data class because they represent something larger than the individual values.
👍 1
Knowing that you can destructure a list is really nice, because I think a good example use case is coordinates. This is my favorite example for showing off destructuring:
val (x, y, z) = arrayOf(5, 10, 5)
Or, looping through a map by destructuring the entry class:
for ((key, value) in map) { }
h
haha, i am literally working with coordinates right now
a
😎 nailed it
p
for the list, all of the returned values have to have the same type, right? otherwise type inference won't work?
a
For the list I believe that's true. Whatever
<T>
is for you list, that's what you have to destructure to.
As far as I'm aware. I would have to test that out.
h
so if i'm rolling through an array that stores x1y1x2y2x3y3.... is there a nicer way to iterate through them than
Copy code
val arr = arrayOf(0,0,1,3,4,2....)
for (i in arr.indices step 2) {
  arr[i]++
  arr[i+1]++
}
using destructuring?
p
could whatever generates them generate a
Pair<x,y>
instead?
a
There is something like that.
windowed
I believe. But let me back up - why have an array of x1y1x2y2... and not a list of pairs?
s
or
chunked
, depending on your use case
h
well, yeah, I could do
List<Pair<Int,Int>>
but I'm doing breadth first search and wanna be really efficient with memory
so i'm using a bytearray
a
hmm
h
i've never used chunked either
a
chunked
is nice
if you have your coordinates in the byte array you mentioned, you can use
chunked
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.
h
i have trouble with collections in kotlin. generating alterations on a base set of coordinates required some funkiness like this:
Copy code
val 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
        }
this is AI stuff, or i'm throttled by memory, not cpu
a
Hmm let me tinker for a sec
p
doesn't kotlin have lazy operations (like java8 streams) that let you prettify and consume in one iteration?
h
i don't care how long it takes for the operation to run 😛
p
I forget what they're called
h
delegates
i've only ever used them for observables
a
Casey - can you give me an example of an input array, and what you'd expect as the output, and I can tinker to see if there's a more idiomatic way to pull it off?
h
it's not a large program if you want me to just hand it all over for context
h
it's not working correctly atm
though...
a
hmm
one sec
I think I see what this is trying to do
p
Copy code
fun 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()
}
that prints this:
Copy code
1,1
2,2
3,3
obviously replace the println with an actual mapping
h
alright. slick!
p
but only one
Pair
reference is held at a time, so it doesn't require a lot of memory (it'll create garbage, though)
a
yeah I think that's what you're looking for
h
i also like to write
Pair(it[0], it[1])
as
it[0] to it[1]
p
oh yeah, good call