Sourabh Rawat
10/22/2020, 6:13 PMvar toPlayer = players.random()
while (toPlayer == fromPlayer) {
toPlayer = players.random()
}
I am not liking the multiple assignments + var here...Sebastian Galkin
10/22/2020, 6:19 PMval toPlayers = generateSequence {players.random()}.filter {it != fromPlayer}
Sebastian Galkin
10/22/2020, 6:20 PMfromPlayer
Sourabh Rawat
10/22/2020, 6:32 PMnanodeath
10/22/2020, 6:41 PMnanodeath
10/22/2020, 6:41 PMRuckus
10/22/2020, 6:43 PM(players - fromPlayer).random()
Sebastian Galkin
10/22/2020, 6:43 PMnanodeath
10/22/2020, 6:44 PMRuckus
10/22/2020, 6:51 PMfun <T> List<T>.randomBut(exclude: T): T {
var value: T
do value = random() while (value == exclude)
return value
}
Then just do
players.randomBut(fromPlayer)
Ruckus
10/22/2020, 6:54 PMinline fun <T> List<T>.randomWhere(predicate: (T) -> Boolean): T {
var value: T
do value = random() while (!predicate(value))
return value
}
And
players.randomWhere { it != fromPlayer }
Sourabh Rawat
10/22/2020, 7:07 PMRuckus
10/22/2020, 7:09 PMnanodeath
10/22/2020, 7:11 PMSourabh Rawat
10/22/2020, 7:13 PMbjonnh
10/22/2020, 7:39 PMdoWhileStatement
(used by loopStatement)
: 'do' controlStructureBody? 'while' '(' expression ')'
;
in https://kotlinlang.org/docs/reference/grammar.html#whileStatementbjonnh
10/22/2020, 7:40 PMbjonnh
10/22/2020, 7:40 PMMichael de Kaste
10/23/2020, 10:05 AM(players - fromPlayer).random()
Yes you could technically use some sort of smart indexer where you exclude the result, but that would include searching for that element in the first place so the complexity would be the same.