Ended up doing a extension myself, but would gladly replace it with a library function
Copy code
fun <T> ArrayList<T>.transformAll(transform: (T) -> T) {
for(i in firstIndex..lastIndex) {
this[i] = transform(this[i])
}
}
Copy code
@Test
fun `Should apply the transformation to each element of the array list`() {
// GIVEN
val list = arrayListOf(2, 3, 4)
// WHEN
list.transformAll { it * 2 }
// THEN
assertThat(list, `is`(equalTo(arrayListOf(4, 6, 8))))
}
o
oranj
05/24/2017, 9:12 AM
ferranis: I am in a very "getting-strated" state - so a question. Why is 'is' in quotes?
oranj
05/24/2017, 9:12 AM
or backticks
a
Andreas Sinz
05/24/2017, 9:27 AM
@oranj because
is
is a reserved keyword
d
Daniel
05/24/2017, 9:27 AM
Don't worry about this kind of questions! 🙂 I am also stil a begginer.
is
is a keyword in Kotlin (the equivalent to instanceOf) so normally you coulndn't use it as a function name. The backticks tell the compiler that
is
is not a keyword in this context
o
oranj
05/24/2017, 9:27 AM
ooooh, okay. thank you ❤️
oranj
05/24/2017, 9:28 AM
so, using something like assertj would look prettier?