For curiosity, why extension of array return list?...
# stdlib
s
For curiosity, why extension of array return list? It does not make sense to me. intArrayOf(1,2,3).filter { it > 1} = List<Int>
d
I guess that arrays are there for Java interop and for cases where its efficiency is really needed. http://www.informit.com/articles/article.aspx?p=2861454&amp;seqNum=3
filter
performs a new allocation, so it is not really minimizing the memory footprint anyways
h
Well, you don't know up front how many of the array elements will match the predicate – so you can't allocate an array up front. So you need the list allocation anyway, and then you can just return it as well (making
IntArray.filter {…}
line up nicely with
Collection.filter {…}
). If you want an
IntArray
out, you'll have to call
.toIntArray()
on the resulting list – and yes, this will make another copy of the data.
👍 4
☝️ 2
s
Okay, now I get it. It looks a bit weird but make sense.