sorry for a very dumb question, I’m trying to build an app using kotlin language but I’m having a difficulty simplifying my code: i am just self learning
here is my sample code
do {
val b = mutableListOf<kotlin.Int>()
val b2 = (1..5).map { (1..15).random() }
val b3 = b2.distinct()
if (b3.count() == 5) {
for (n in b3) {
b.add(n)
cardid.add(n)
}
for (n in 1..5){
textView1.text = b[0].toString()
textView2.text = b[1].toString()
textView3.text = b[2].toString()
textView4.text = b[3].toString()
textView5.text = b[4].toString()
}
}
}while (b3.count() <=4)
do {
val i = mutableListOf<kotlin.Int>()
val i2 = (1..5).map { (16..30).random() }
val i3 = i2.distinct()
if (i3.count() == 5) {
for (en in i3) {
i.add(en)
cardid.add(en)
}
for (en in 1..5){
textView6.text = i[0].toString()
textView7.text = i[1].toString()
textView8.text = i[2].toString()
textView9.text = i[3].toString()
textView10.text = i[4].toString()
}
}
}while (i3.count() <=4)
The code mention above is perfectly fine. the problem is i want to simplify it:
like for example
var n = 2
var x = 3
textview(n).text = i.tostring()
i have an error using variable in
textview(n).text
its telling me to make a prive var String:… etc etc
sorry for long post… thank you for any help
wow thank you so much for your help it is shorter in deed, do you have any books or website for someone like me without any formal education about kotlin thank you
Even though I indeed possess a master degree in computer science I also had to learn those on my own My university for example only started to teach Scala and functional approach after I graduated.
My recomendation would be to get more accustomed to those subjects:
Functional paradigm
Monads
Eager vs lazy collections
Streams (including infinite ones) like Java8 Stream, Kotlin’s Sequence or RxJava
As for conrete learning materials I would need to look for some, but you could start by completing the official Kotlin’s tutorial: https://try.kotlinlang.org/
generateSequence { (16..30).random() } // creates an infinite sequence generating numbers within a range from 16 and 30 inclusive
.distinct() // removes duplicates
.zip(sequenceOf(textView6, textView7, textView8, textView9, textView10)) // combines an existing sequence with another one with a fixed set of data, limiting at the same time the infinite sequence execution, resulting data structure would be `Pair<Int, TextView>`
.forEach { (number, textView) -> // () braces destructure pairs into variables
textView.text = number.toString()
}