Created
January 12, 2022 02:35
-
-
Save optimho/77ec5a590dabfe11f90b30c5a638b682 to your computer and use it in GitHub Desktop.
Examples Of lambda functions
This file contains hidden or 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
| fun main(args: Array<String>) { | |
| // val lambdaName:(InputType, InputType , ..) -> OutputType = {arguementN:InputType, argumentN:InputType -> body} | |
| // greeter ("Michael", "duplessis") | |
| // "Hello Michael du Plessis" | |
| val greeter:(firstName:String, LastName:String) ->String = {FirstName:String, LastName:String -> | |
| "Hello $FirstName $LastName" | |
| } | |
| val greeting: String = greeter("Michael", "du Plessis") | |
| println(greeting) | |
| lineLogger { | |
| println("Here is a message") | |
| println("Here is a message") | |
| println("Here is a message") | |
| repeat(3){ println(" this is repeated 3 time")} | |
| } | |
| repeater(21){ | |
| println("Helllo from the repeater block") | |
| println(it) | |
| } | |
| derbyAnnouncer { player: String -> | |
| "$player is a greate player" | |
| } | |
| } | |
| fun derbyAnnouncer(block: (String) -> String) { | |
| val players:List<String> = listOf( | |
| "McGwire", | |
| "Canseco", | |
| "Honeycut", | |
| "Davis", | |
| "Dawely", | |
| "Weiss" ) | |
| val randomPlayer:String = players.random() | |
| println("The next player is .....$randomPlayer") | |
| val announceMessage:String = block(randomPlayer) | |
| println("$announceMessage") | |
| } | |
| fun repeater (times:Int, block:(Int)->Unit){ | |
| repeat(times){index -> block(index) | |
| } | |
| } | |
| fun lineLogger(block:() -> Unit){ | |
| repeat(5){println("==================")} | |
| block() | |
| repeat(5){println("==================")} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment