Newbie Question

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

How about something like this?

Random.asJavaRandom()
	.ints(1, 15 + 1)
	.distinct()
	.asSequence()
	.zip(sequenceOf(textView1, textView2, textView3, textView4, textView5))
	.forEach { (number, textView) ->
		textView.text = number.toString()
	}

Random.asJavaRandom()
	.ints(16, 30 + 1)
	.distinct()
	.asSequence()
	.zip(sequenceOf(textView6, textView7, textView8, textView9, textView10))
	.forEach { (number, textView) ->
		textView.text = number.toString()
	}

or maybe something like this would be even better:

generateSequence { (1..15).random() }
	.distinct()
	.zip(sequenceOf(textView1, textView2, textView3, textView4, textView5))
	.forEach { (number, textView) ->
		textView.text = number.toString()
	}

generateSequence { (16..30).random() }
	.distinct()
	.zip(sequenceOf(textView6, textView7, textView8, textView9, textView10))
	.forEach { (number, textView) ->
		textView.text = number.toString()
	}
1 Like

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 :wink: 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/

sir madmax1028 do you have any social account that i can in touch with you?

if im being a nuisance just tell me so. by the way thank you so much again for the great help

generateSequence { (16..30).random() }
	.distinct()
	.zip(sequenceOf(textView6, textView7, textView8, textView9, textView10))
	.forEach { (number, textView) ->
		textView.text = number.toString()
	}

im trying to understand the code you send to me…
i have hundreds of question if you may :smiley:

Short version then :wink:

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()
	}

Is this explanation enough?

just to point out: try.kotlinlang.org is obsolete, the same content on the new platform is:

https://play.kotlinlang.org/koans/Introduction/Hello,%20world!/Task.kt

1 Like