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. Understanding Currying with Scala

Understanding Currying with Scala

Carlo Scarioni user avatar by
Carlo Scarioni
·
Feb. 16, 13 · Interview
Like (7)
Save
Tweet
Share
29.42K Views

Join the DZone community and get the full member experience.

Join For Free

Learning Scala is not an easy task. There are many things to learn. Some of them are very different and some of them seem complex.

Currying is a technique not necessarily complex, but is one that you probably are not familiar with if you come from Java background as it is not a straightforward technique to apply in that language. Currying allows to turn a function that expects two arguments into a function that expects only one, and that function returns a function that expects the second argument. Creating basically a chain of functions.

Let’s see with a simplistic example how it works.

Let’s say we have this two simple classes:

case class Message(value: String){

}

case class Endpoint(prompt: String){
 def send(m: Message) {
   println(this.prompt + " " + m.value)
 }
}

Now let’s pretend we want to send a Message to an Endpoint in a general function. We can create a function that looks like this:

def routeTo(m:Message, e:Endpoint) = {
  e.send(m)
}

To call this function we would do something like:

routeTo(Message("hola"),Endpoint("sending"))

(You could ask why we just don’t create the endpoint and call send with the message like Endpoint(“sending”).send(Message(“hola”)) and there is no reason in this example. Simply to illustrate currying).

Now let’s suppose we want to send the same message to different endpoints. For that we could use the currying technique. We will basically turn our function into a function that returns another function:

def route(m:Message) = {
  (e: Endpoint) => e.send(m)
}
 
You can see we have created a function that just receives the Message and returns a function that receives the Endpoint and sends the message to that endpoint.

Now we could do something like the following:
val routeCiao = route(Message("ciao"))

routeCiao(Endpoint("sending again"))
routeCiao(Endpoint("sending over again"))
You can see that we are assigning the return of route(Message(“ciao”)), which is a function (Endpoint => Unit) to the val routeCiao. In practice this is basically returning the function (e: Endpoint) => e.send(“ciao”) as the value of the variable m is replaced by “ciao” when calling to the route function.

Then we are simply calling the returned function passing the value of the endpoint as the only argument. So this function is executed and the message is sent to the endpoint.

You could of course call the currying function without storing the mid result in a val. Like this:

route(Message("hi"))(Endpoint("sender"))
Scala supports a more compact way to defining currying functions. We could replace the route function with the following:

def route(m:Message)(e:Endpoint) = {
   e.send(m)
}
The effect is the same if you call like this: route(Message("hi"))(Endpoint("sender")) However to call and store the mid result in a val you need to do it like this:

val routeCiao = route(Message("ciao")) _

Note the underscore at the end of the assignment.



Scala (programming language)

Published at DZone with permission of Carlo Scarioni, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Comparing Kubernetes Gateway and Ingress APIs
  • Leaders Make Their Own Problems
  • Deduplication and Data Stewardship Process in MDM
  • A ChatGPT Job Interview for a Scrum Master Position

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: