Hey all, I have a best practices question regardin...
# stdlib
a
Hey all, I have a best practices question regarding stdlib lists vs sequences. My understanding is when it comes to list builtins like map and filter, they will create a new list whereas sequences will lazily evaluate and combine the operations when appropriate. If I am using more than one of the collection builtins, should I always prefer to transform it to a sequence? Or combine them myself into one
.mapNotNull
if possible? Or is it strictly a judgement call based on the size of the data expected since iterating over a list of size 2 twice is really not a big deal
☝️ 1
tl;dr: it depends 😄
🥲 1
a
Okay so let’s say I decide to use collection. One of buddies offered up
mapNotNull { if (condition) transform else null }
as an alternative to
filter { condition }.map { transform }
. I admitted it is technically more efficient but argued that the filter/map was more readable. Any thoughts on that?
m
Personally I tend to go for readability (within reason) until it becomes a problem. No need to prematurely optimize unless you have strict performance requirements.
5
f
Exactly what @Matthew Gast wrote, and if performance is the most important thing Kotlin itself might already be a problem, and in Java not every loop is as fast as another. It all boils down to the bytecode that is being generated and how fast it is in interpreted mode as well as how easy it is for the JVM to optimize it. Assuming JVM is actually the target here that is. What’s always true, regardless of the target, is to mutate, instead of creating something new.