Skip to content

Instantly share code, notes, and snippets.

@travisbrown
Created July 6, 2014 10:54
Show Gist options
  • Save travisbrown/957ddfd83b9b6fc329c4 to your computer and use it in GitHub Desktop.
Save travisbrown/957ddfd83b9b6fc329c4 to your computer and use it in GitHub Desktop.
/**
* 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