DZone
Java Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > 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 · Java Zone · Opinion
Like (14)
Save
Tweet
11.02K 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

  • What Are Microservices?
  • How to Submit a Post to DZone
  • 12 Modern CSS Techniques For Older CSS Problems
  • Automation Testing vs. Manual Testing: What's the Difference?

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo