Created
July 6, 2014 10:54
-
-
Save travisbrown/957ddfd83b9b6fc329c4 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
/** | |
* Pretend type-case doesn't exist in Scala. We know nothing about `A` and `B`, | |
* so there's only thing we can do here: apply `f` to `a` and return the result. | |
*/ | |
def foo[A, B](a: A)(f: A => B): B = ??? | |
/** | |
* Now with type-case. Since the implementation can look at the type of `a` we | |
* have no idea what it might be doing. | |
*/ | |
def foo[A, B](a: A)(f: A => B): B = a match { | |
case i: Int => f(i * 2) | |
case c: Char if c >= 'a' && c <= 'z' => f(('a' + (c + 13 - 'a') % 26).toChar) | |
case x => f(x) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment