https://kotlinlang.org logo
Title
d

Daniel

05/24/2017, 8:42 AM
Ended up doing a extension myself, but would gladly replace it with a library function
fun <T> ArrayList<T>.transformAll(transform: (T) -> T) {
    for(i in firstIndex..lastIndex) {
        this[i] = transform(this[i])
    }
}
@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?
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 ❤️
so, using something like assertj would look prettier?
d

Daniel

05/24/2017, 9:30 AM
Or this: https://github.com/MarkusAmshove/Kluent To make asserts even more kotlin-ish. (Want to try this out myself! :-))