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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. The Difference Between Currying and Partially Applied Functions

The Difference Between Currying and Partially Applied Functions

Currying and partially applying may seem the same — both turn your functions into ones with fewer arguments. But there's a difference between them.

Alexey Zvolinskiy user avatar by
Alexey Zvolinskiy
·
Mar. 20, 17 · Tutorial
Like (11)
Save
Tweet
Share
16.78K Views

Join the DZone community and get the full member experience.

Join For Free

in this article, i want to show a real difference between curried functions and partially applied functions in scala. this question is pretty common for those developers who started learning scala without previous experience in functional programming. moreover, this blog post may be useful even for experienced scala developers because, based on my experience, i have had an incorrect understanding of the difference between the curried functions and partially applied functions.

so here is the most classic example of misunderstanding in the question of currying and partially applied functions. i wrote that article ~9 months ago. since then, i've dived deeper into scala and figured out what a curried function is and what a partially applied function is.

definitions

before demonstration of code examples, it would be rational to get acquainted with the definitions. for most people, it is enough in order to understand what is the difference.

currying : decomposition of functions with multiple arguments into a chain of single-argument functions.

notice, that scala allows passing a function as an argument to another function.

partial application of function : pass to a function fewer arguments than it has in its declaration. scala does not throw an exception when you provide fewer arguments to the function, it simply applies them and returns a new function with rest of arguments that need to be passed.

in general, you can stop reading this article if everything is clear. otherwise, i recommend looking at some examples for each of function types.

currying examples

let’s start from the most trivial example of a curried function:

def sum(a: int, b: int): int = a + b
//(int, int) => int

def curriedsum = (sum _).curried
//int => (int => int)

curriedsum(5)
//int => int

res0(10)
//int = 15


what happened in the code snippet above? at first, i declared the function sum . it has two arguments of the int type. its returning type is int as well.

then, i create a curried version of the sum function. the curriedsum function accepts a single argument of the int type and returns a function of the int => int type.

when i pass 5 into the curriedsum , i receive back a new function res0: int => int (this sample was performed in repl).

finally, invoking res0 with any int parameter we will add this parameter to 5.

curried function example

by invoking a curried method on any function of type functionn where n > 1, we receive a curried version of the function.

here is another example of currying:

def isinrange(left: int, n: int, right: int): boolean = {
    if (left < n && n < right) true else false
}

(isinrange _).curried
//int => (int => (int => boolean))


the logic of the isinrange function is pretty simple: if argument n is more than left but less than right , the function returns true . otherwise, it returns false .

after we made a currying version of isinrange , we can perform the following operations with it:

currying function with multiple arguments

in this screenshot, you can see how we pass arguments one by one and, as a result, we see two boolean results in res3 and res4 .

partial application examples

now let’s see how partially applied functions look likein practice. i’m going to use the isinrange function from the previous section.

def isinrange(left: int, n: int, right: int): boolean = {
    if (left < n && n < right) true else false
}

def is5inrange = isinrange(_: int, 5, _: int)
//(int, int) => boolean

is5inrange(0, 8)
//true

def between0and10 = isinrange(0, _: int, 10)
//int => boolean

between0and10(5)
//true

between0and10(100)
//false


so what happened in the code snippet above? i used isinrange as a base for two new functions: is5inrange and between0and10 . notice, that for missed parameters, you can use an underscore.

partial application is extremely helpful when you want to create a set of different functions on top of one particular function. in the same way, as i create between0and10 , it’s easy to create between50and75 , for instance.

summary

so after the theory and practice part, i hope you understood how curried functions differ from partially applied functions. once again: currying – transformation of a function with multiple arguments into a chain of single-argument functions. partially applied function – a function that was called with a fewer number of arguments.

Scala (programming language) application Pass (software) Snippet (programming) dev Clear (Unix) Decomposition (computer science) IT POST (HTTP)

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

  • 7 Ways for Better Collaboration Among Your Testers and Developers
  • Getting a Private SSL Certificate Free of Cost
  • 5 Steps for Getting Started in Deep Learning
  • Create a CLI Chatbot With the ChatGPT API and Node.js

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: