Mark is a graph advocate and field engineer for Neo Technology, the company behind the Neo4j graph database. As a field engineer, Mark helps customers embrace graph data and Neo4j building sophisticated solutions to challenging data problems. When he's not with customers Mark is a developer on Neo4j and writes his experiences of being a graphista on a popular blog at http://markhneedham.com/blog. He tweets at @markhneedham. Mark is a DZone MVB and is not an employee of DZone and has posted 527 posts at DZone. You can read more from them at their website. View Full User Profile

Scala: Pattern matching a pair inside map/filter

07.14.2011
| 8599 views |
  • submit to reddit

More than a few times recently we’ve wanted to use pattern matching on a collection of pairs/tuples and have run into trouble doing so.

It’s easy enough if you don’t try and pattern match:

> List(("Mark", 4), ("Charles", 5)).filter(pair => pair._2 == 4)
res6: List[(java.lang.String, Int)] = List((Mark,4))

But if we try to use pattern matching:

List(("Mark", 4), ("Charles", 5)).filter(case(name, number) => number == 4)

We end up with this error:

<console>:1: error: illegal start of simple expression
       List(("Mark", 4), ("Charles", 5)).filter(case(name, number) => number == 4)

It turns out that we can only use this if we pass the function to filter using {} instead of ():

> List(("Mark", 4), ("Charles", 5)).filter { case(name, number) => number == 4 }
res7: List[(java.lang.String, Int)] = List((Mark,4))

It was pointed out to me on the Scala IRC channel that the reason for the compilation failure has nothing to do with trying to do a pattern match inside a higher order function but that it’s not actually possible to use a case token without the {}.

[23:16] mneedham: hey – trying to understand how pattern matching works inside higher order functions. Don’t quite get this code -> https://gist.github.com/1079110 any ideas?

[23:17] dwins: mneedham: scala requires that “case” statements be inside curly braces. nothing to do with higher-order functions

[23:17] mneedham: is there anywhere that’s documented or is that just a known thing?

[23:18] mneedham: I expected it to work in normal parentheses

[23:21] amacleod: mneedham, it’s documented. Whether it’s documented simply as “case statements need to be in curly braces” is another question

The first line of Section 8.5 ‘Pattern Matching Anonymous Functions’ of the Scala language spec proves what I was told:

Syntax:
BlockExpr ::= ‘{’ CaseClauses ‘}

It then goes into further detail about how the anonymous function gets converted into a pattern matching statement which is quite interesting reading.

From http://www.markhneedham.com/blog/2011/07/12/scala-pattern-matching-a-pair-inside-mapfilter/

Published at DZone with permission of Mark Needham, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Michael Kebe replied on Fri, 2011/07/15 - 1:56am

An interesting post about this topic can be found here: http://jim-mcbeath.blogspot.com/2009/10/scala-case-statements-as-partial.html

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.