DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Containers Trend Report: Explore the current state of containers, containerization strategies, and modernizing architecture.

Low-Code Development: Learn the concepts of low code, features + use cases for professional devs, and the low-code implementation process.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • Event-Driven Fractals
  • Easily Update and Reload SSL for a Server and an HTTP Client
  • Writing a Chat With Akka
  • Snowflake Data Processing With Snowpark DataFrames

Trending

  • Navigating Software Leadership in a Dynamic Era
  • The Future of Java: Virtual Threads in JDK 21 and Their Impact
  • From Docker Swarm to Kubernetes: Transitioning and Scaling
  • Challenges of Legacy System Integration: An In-Depth Analysis
  1. DZone
  2. Coding
  3. Languages
  4. Scala: Pattern matching a pair inside map/filter

Scala: Pattern matching a pair inside map/filter

Mark Needham user avatar by
Mark Needham
·
Jul. 15, 11 · Interview
Like (0)
Save
Tweet
Share
18.7K Views

Join the DZone community and get the full member experience.

Join For Free

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/

Scala (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Event-Driven Fractals
  • Easily Update and Reload SSL for a Server and an HTTP Client
  • Writing a Chat With Akka
  • Snowflake Data Processing With Snowpark DataFrames

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: