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 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
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
  1. DZone
  2. Coding
  3. Languages
  4. Currying Functions in Scala

Currying Functions in Scala

An explanation of what currying functions are in Scala and how they work.

Alexey Zvolinskiy user avatar by
Alexey Zvolinskiy
·
Apr. 11, 16 · Opinion
Like (14)
Save
Tweet
Share
11.32K Views

Join the DZone community and get the full member experience.

Join For Free

For a long time I couldn’t understand currying functions in Scala and how they work. That was really horrible! Occasionally I met the currying functions in the code and wasted too much time on reading them. So finally I decided to learn how they work and where they could be applied.

Let’s start from a definition. A currying function is a function which could accept a fewer number of parameters than are declared, then it returns a function with unused parameters. This definition is totally weird. In order to understand it we need to go through several examples. And be sure that you already know how simple Scala functions work.

Before Currying

The most efficient way to understand the currying is to work with higher-order functions. Let’s look at following code snippet:

def concatenator(w1: String): String => String = w2 => w1 +" "+ w2

What’s going on in the string above? Well, there is declared concatenator function. It accepts w1 argument of String type. It returns another function of String => String type. Moreover the returning function has its own body w2 => w1 +" "+ w2.

Now we can see how it works:

Image title

As you see we assigned to heyWord the function. Then we made a callheyWord("currying"). The result of that call is “Hey currying” string.

After this demonstration we can move further. Keep in mind how we used the function which was returned.

Currying Time!

Let’s declare a curried function:

def concatenator(w1: String)(w2: String) = w1 + " " + w2

So parameters in the concatenator function defined in a separate brackets. How this circumstance affects the function usage? If we want to call it as a normal function we could pass all arguments in the same time and the result will be:

Image title

As you see we get the expected output Hey currying string. But what if we don't have all required elements at the moment of first call? In this case we could use underscore or empty curly brackets instead of missed parameters:

Image title

In the code sample from the first section of this article we used currying to achieve the same effect. So we assigned the result of the first call to heyWord:

val heyWord = concatenator("Hey")_

Then we invoked the heyWord function with the currying parameter. I think now currying is more or less clear to you. In order to complete this topic I want to provide one more example of a curried function.

More Currying

What about a curried function that has more than one parameter per set of brackets? It’s easy:

Image title

By the way, if you want to specify not to look at the first argument, you could use the following approach:

val isFiveInRange = isInRange(_:Int,_:Int)(5)
//isFiveInRange: (Int, Int) => Boolean = <function2>

isFiveInRange(0, 10) //true

isFiveInRange(-10, 0) //false

Thanks to Luka Jacobowitz for his remarks on this case.

Summary

The basics of currying are simple if you are interested in learning them. A more important aspect of them is a practical usage. In this respect I’m not so experienced and I’d like to see your own samples in comments.

Scala (programming language)

Published at DZone with permission of Alexey Zvolinskiy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevSecOps Benefits and Challenges
  • The Importance of Delegation in Management Teams
  • Cloud Native London Meetup: 3 Pitfalls Everyone Should Avoid With Cloud Data
  • Playwright vs. Cypress: The King Is Dead, Long Live the King?

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
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: