Home > OS >  How do you call a variable created earlier in a when in kotlin?
How do you call a variable created earlier in a when in kotlin?

Time:03-03

I'm trying to list the first 100 of a shuffled list. I'm telling it to shuffle if the list is at 0 and then increment. I then am trying to call that list in another section of the when but it's not working. How can I accomplish this?

    when (countF) {
                    0 -> {
                        //shuffle at 0
                        val randomChaos = chaosList.asSequence().shuffled().take(chaosList.count()).toList()
                        cResult.text = randomChaos.elementAt(countF)   countF   "\n\n\n\n\n\n\n\n"   this.cResult.text
                        countF  
                    }
                    1-99 -> {
                        //show 1-99
                        cResult.text = randomChaos.elementAt(countF)   countF   "\n\n\n\n\n\n\n\n"   this.cResult.text
                        countF  
                    }
                    100 -> countF = 0

CodePudding user response:

You would need to create the val randomChaos before the when enclosure for it to be available in the scope of multiple branches of the when statement.

That said, the way you're getting a random element is very convoluted. take(chaosList.count()) is completely redundant. And since you don't use multiple sequence operators, creating a sequence is also redundant. Finally, you are only pulling a single item from the random list, so it's unnecessary to create a shuffled list in the first place. Using elementAt() on a shuffled list is no different than picking any element out of that shuffled list, or simply picking a random item out of a list that isn't shuffled at all.

Also, the first two branches of your when statement currently would produce exactly the same results so they can be merged.

Based on what you described, I'm guessing you had this when statement inside a loop that tries to run it 100 times so you can list all the items. For that to work, you would need to shuffle the list one time outside the loop, and then you could iterate its elements in the loop.

However, there are functions that can make it easier to do what you're suggesting. Here's an example:

val randomChaos = chaosList.shuffled()
cResult.text = randomChaos.asSequence()
    .take(100)
    .withIndex()
    .joinToString("\n") { (i, value) ->
        "$value-$i"
    }

In this case, using a Sequence helps avoid creating an intermediate list to hold the first 100 values.

CodePudding user response:

    var randomChaos  = chaosList.shuffled()
    fun cShuf() { randomChaos = chaosList.shuffled() }

    cRoll.setOnClickListener() {
        cResult.movementMethod = ScrollingMovementMethod()
        if (countF < 1) { cShuf() }
        cResult.text = randomChaos.elementAt(countF)   "\n\n\n\n\n\n\n\n"   this.cResult.text
        countF  
        if (countF > 100) countF = 0
    }

I have figured out how to use a function to generate a new shuffe of the list once I've hit > 100 shown. My issue with making it a function was I was trying to use val variable in the function but the variable already existed so I didn't need to use val, just the name of the variable.

  • Related