(functions == objects) => awesome!

One thing I increasingly love about Scala is this: functions are objects, and for many common objects it is true that objects are functions as well. Some examples are arrays, lists, maps etc. For example, lets say we have a map from numeric characters to English words corresponding to these numbers:

val n2s = Map(
      '0' -> "zero",
      '1' -> "one",
      '2' -> "two",
      '3' -> "three",
      '4' -> "four",
      '5' -> "five",
      '6' -> "six",
      '7' -> "seven",
      '8' -> "eight",
      '9' -> "nine")

This map moonlights as (okay, okay, “inherits from”) a function of the type (Char => String) and you can pass it where such a function is expected as parameter, like here:

n.toString.map(n2s).mkString(" ")

Quick explanation for those new to Scala:

  • n.toString converts n to its String representation
  • a String is also a Seq[Char] in Scala land, and the map method applies a function of Char => B (where B is any type) to each element in the sequence, thus producing a new Seq[B]
  • we pass our map n2s to this function, which means we will map the Seq[Char] to a Seq[String]
  • that sequence is then turned into a single String using mkString (and specifying space as the separator between elements)

This function is just an example, it probably isn’t actually useful for anything. I just saw something like it in Python and thought I’d try to make the same program in Scala before realizing that it didn’t do anything really interesting (like converting 123 into “one hundred twenty-three” instead of “one two three”).

Another awesome thing about functions in Scala are partial functions, which are functions taking a single argument that are defined only for some argument values (the domain of the function). Map is also a partial function — defined only for the keys it contains — and pattern matching, which is a very important part of Scala, is related to partial functions as well. I won’t go into more detail in this post, but Doug Pardee has written a more in-depth article about functions in Scala.


2 thoughts on “(functions == objects) => awesome!

  1. Pingback: links for 2008-03-09

  2. The first scala-code I saw that exploited the fact that functions are objects and hence classes can extend functions was in Szeigers excellent parsing series: http://szeiger.de/blog/2008/07/27/formal-language-processing-in-scala-part-1/

    I love the simplicity of your example. Perhaps it would be even easier to understand if n had a value in example or leave out n entirely:

    “1234567”.map(n2s).mkString(” “)

    That’s a minor detail that didn’t keep me from sharing your excitement with this feature of Scala. I did not know that maps are also functions, I’ll have to keep my eye out for hidden treats like this in the future.

    Thanks for posting this!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s