Created
August 20, 2020 15:12
-
-
Save ajaypro/2cc81fa93ae3be95cbbf8236f71ee339 to your computer and use it in GitHub Desktop.
Destructor declarations and using partition()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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