Last active
May 28, 2019 19:29
-
-
Save scottashipp/28eec8431413717662b2 to your computer and use it in GitHub Desktop.
Passing functions in Scala, seen at https://code.scottshipp.com/2015/12/22/passing-functions-in-scala-or-how-scala-embarrasses-oo-languages-and-their-pretentious-strategy-pattern/
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
object SuperHeroEscape extends App { | |
def getFreeFromATrap(doSuperPower: String => Unit, trap: String) = { | |
println() | |
println(s"Oh no I'm trapped by ${trap}.") | |
println(s"I will break free!") | |
doSuperPower(trap) | |
} | |
def flyOut(trap: String) = println(s"I fly out of the ${trap} and into the sky!") | |
def superStrength(trap: String) = println(s"With my super strength, I can break the ${trap} and escape!") | |
def laserVision(trap: String) = println(s"My laser vision destroys ${trap} and I escape!") | |
def invisibility(trap: String) = println(s"Using invisibility I evade the ${trap}!") | |
getFreeFromATrap(flyOut, "quick sand") | |
getFreeFromATrap(superStrength, "iron bars") | |
getFreeFromATrap(laserVision, "a collapsed tunnel") | |
getFreeFromATrap(invisibility, "armed guards") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment