Created
February 27, 2023 11:48
-
-
Save droid-lover/a38ea65f6fb9f73dfb88dca22eb565a4 to your computer and use it in GitHub Desktop.
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
/** | |
* Native Mobile Bits Evening Coding Paper @5pm IST | |
* "Infix - very useful" | |
*/ | |
fun main() { | |
// val result = 20.add(3) Traditional way | |
val result = 20 add 3 //with Infix function | |
println(result) | |
val fruits = setOf<String>("apples","oranges") | |
//println("oranges".isInMenu(fruits)) Traditional way | |
//println("mangoes".isInMenu(fruits)) Traditional way | |
println("oranges" isInMenu fruits) //with Infix function | |
println("mangoes" isInMenu fruits) //with Infix function | |
} | |
infix fun String.isInMenu(setOfFruits:Set<String>) :Boolean{ | |
return setOfFruits.contains(this) | |
} | |
infix fun Int.add(number: Int): Int { | |
return this + number | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment