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

This simple example of function currying shows a simple use case as well as code snippets to show how function currying saves time.

Gaurav Gaur user avatar by
Gaurav Gaur
CORE ·
Jan. 02, 18 · Tutorial
Like (7)
Save
Tweet
Share
25.72K Views

Join the DZone community and get the full member experience.

Join For Free

Currying is named after Haskell Curry, an American mathematician. He is known for his work in combinatory logic.

Currying is a means of transforming a function that takes more than one argument into a chain of calls to functions, each of which takes a single argument.

Let us consider the function below to calculate the final price of the product. The function takes in 3 parameters:

  • VAT (vat) for the region
  • Service charge (serviceCharge) of the shop
  • Product price (productPrice)
def finalPrice(vat: Double, 
               serviceCharge: Double, 
               productPrice: Double): Double = 
productPrice + productPrice*serviceCharge/100 + productPrice*vat/100


But if you think about the function finalPrice again, a shopkeeper has to provide all the above values time and again whenever he wants to calculate the final price. Of course, that ignores the fact that the values of:

  • VAT is already defined for a country
  • Service charge for a shop is constant

So we will try to make life of our client a little bit easier. Let us define curried finalPrice:

def finalPriceCurried(vat: Double)
(serviceCharge: Double)
(productPrice: Double): Double = 
productPrice + productPrice*serviceCharge/100 + productPrice*vat/100


We are taking this approach because our vat and serviceCharge will not change very often. So, let's use currying to split our method. We will declare a new val: vatApplied. I will provide the value of vat to the finalPriceCurried method and assign it to vatApplied.

val vatApplied = finalPriceCurried(20) _


Next, we will provide a service charge to my vatApplied val, and we will leave the price to be provided by the shopkeeper whenever they need it.

val serviceChargeApplied = vatApplied(12.5)


Let us test our serviceChargeApplied function to calculate the final price of the product.

val finalProductPrice = serviceChargeApplied(120)

finalProductPrice: Double = 159.0


We have reduced our method from accepting 3 parameters to accept one parameter. So, we have split our method in such a way that we don't have to provide all the arguments at the same time. I can provide these arguments whenever they are available. This transformation is called currying.

We can also convert our existing methods to curry methods using function curried method. I have seen currying used a lot in my production code. It is always good to understand how these things work. You can refer to the video below to understand the concept in more detail and to check out few more examples.


Scala (programming language) Curry (programming language) Concept (generic programming) Haskell (programming language) Testing Production (computer science) Convert (command)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • ChatGPT Prompts for Agile Practitioners
  • Top 10 Secure Coding Practices Every Developer Should Know
  • Load Balancing Pattern
  • 5 Factors When Selecting a Database

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: