Created
June 2, 2017 16:49
-
-
Save brunkb/ae47068d88ec380523255bb84cfab386 to your computer and use it in GitHub Desktop.
Removing conditional logic
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
def addOne = { a -> | |
return a + 1 | |
} | |
def addTwo = { b -> | |
return b + 2 | |
} | |
def decideWhatToRun = [0: addOne, 1: addTwo] | |
def runSomethingBasedOnInput = { val, input -> | |
def fn = decideWhatToRun[val] | |
def result = fn.call(input) | |
result | |
} | |
println runSomethingBasedOnInput(0, 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment