Skip to content

Instantly share code, notes, and snippets.

@chris-martin
Last active December 19, 2015 00:58
Show Gist options
  • Save chris-martin/5872155 to your computer and use it in GitHub Desktop.
Save chris-martin/5872155 to your computer and use it in GitHub Desktop.

Corner cases of mathematical definitions tend to take people by surprise, such as that 0 factorial equals 1 and that universal quantification over the empty set is always true. In these examples, functional programming (but never procedural programming) is illuminating. In Scala:

def factorial(n: Int): Int =
  (1 to n).fold(1)(_*_)
def all[A](set: Set[A], p: A => Boolean): Boolean =
  set.map(p).fold(true)(_&&_)

It's impossible to write these folds without working out the value of the corner cases, and each shows up explicitly in the code as the first argument to a fold: 1 and true.

@chris-martin
Copy link
Author

Yes, the return types are inferred. Although in most real cases you would declare a function def's return type, because it's safer to be explicit about types that are part of a public API.

@chris-martin
Copy link
Author

Updated the gist to declare the return types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment