Skip to content

Instantly share code, notes, and snippets.

@ajaypro
Created August 20, 2020 15:12
Show Gist options
  • Save ajaypro/2cc81fa93ae3be95cbbf8236f71ee339 to your computer and use it in GitHub Desktop.
Save ajaypro/2cc81fa93ae3be95cbbf8236f71ee339 to your computer and use it in GitHub Desktop.
Destructor declarations and using partition()
data class Bus(val model:String, val route:Int, val isSpecialService: Boolean)
val volvo = Bus("volvo", 23, true)
val leyland = Bus("leyland", 45, false)
val benz = Bus("benz", 109, true)
val scania = Bus("scania", 901, false)
/**
* Usage of partition and also destructing constructors
*/
fun main(){
val buses = arrayOf(volvo, leyland, benz, scania)
// destructing declarations usage of partition() note: lambda function
val (specialService, notSpecialService) = buses.partition { it.isSpecialService }
println("Special Service Buses are: Model - ${specialService[0].model} , Route - ${specialService[0].route}")
println("Regular Service Buses are: Model - ${notSpecialService[0].model} , Route - ${notSpecialService[0].route}")
/**
* Output :
* Special Service Buses are: Model - volvo , Route - 23
Regular Service Buses are: Model - leyland , Route - 45
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment