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. Data Engineering
  3. Data
  4. Currying Vs. Partially Applied Function

Currying Vs. Partially Applied Function

Don't get these two functions confused.

Jyoti Sachdeva user avatar by
Jyoti Sachdeva
·
Nov. 19, 18 · Presentation
Like (9)
Save
Tweet
Share
15.00K Views

Join the DZone community and get the full member experience.

Join For Free

In this blog, I’m going to discuss the difference between currying and partially applied functions.

Currying splits methods with multiple parameters into a chain of functions — each with one parameter.

Currying

First, let’s understand currying using an example:

scala> def multiply(a: Int)(b: Int)(c: Int) = a * b * c                  


This is is the same as:

def multiply(a: Int) = (b: Int) => (c: Int) => a * b * c                  


 Multiply is a curried function that takes three parameters: a, b, and c. Invoke the multiply function as:

multiply(1)(_)                              
res9: Int => (Int => Int) = $$Lambda$1118/1682999176@30b1c5d5                                                                             1                    


This will result in another function that takes an integer as an input and yields a lambda expression (Int => Int).

scala> res9(2)(_) 
res10: Int => Int = $$Lambda$1119/2012237082@27682fa9                  


This will result in another function that takes an integer as an input and returns an integer as result.

scala> res10(3)
res11: Int = 6                  


 Res11 is the final result.

Partially Applied Functions

Pass to function less arguments than it has in its declaration. Scala returns a new function with rest of arguments that need to be passed.

def isInRange(leftBound: Int, num: Int, rightBound: Int): Boolean = {     
if (leftBound < num && num < rightBound) true      
 else false                  
}
isInRange(_: Int, 5, _: Int)
will return another function that will take two integers as an argument and 
result type will be of type Boolean (Int, Int) => Boolean.</p>
scala> isInRange(0, 8)
res0: Boolean = true
scala>(isInRange _).curried
res28: Int => (Int => (Int => Boolean)) = scala.Function3$$Lambda$1322/926382023@3e9cceff                  


This will split the isInRange method into a chain of functions each with one parameter.

Difference Between Currying and Partially Applied Functions

Partially Applied Function Example

scala> def isDivisible(numberOne: Int, numberTwo: Int) = ((numberOne % numberTwo) == 0)
isDivisible: (numberOne: Int, numberTwo: Int)Boolean
scala> isDivisible _
res23: (Int, Int) => Boolean = $$Lambda$1285/1740173358@6fdc624                  


By partially applying a normal function ( isDivisible), it results in a function (res21) that takes all parameters [ (Int, Int) => Boolean ].

scala> res23(4)
res26: Int => Boolean = scala.Function2$$Lambda$1320/451962020@40c78bd1                  3                                      4                    scala> res26(2)                  5                    res27:Boolean = true                  


This will give us a true result.

Currying Example

scala> def isDivisibleCurried(numberOne: Int)(numberTwo: Int) = ((numberOne % numberTwo) == 0)  
isDivisibleCurried: (numberOne: Int)(numberTwo: Int)Boolean                 
scala> isDivisibleCurried _
res22: Int => (Int => Boolean) = $$Lambda$1298/27138712@4ec8083                  


Partially applying a function on isDivisibleCurried will create a chain of functions, one per parameter list [ Int => (Int => Boolean) ].

Thanks for reading!

Data Types IS-IS Pass (software) Scala (programming language) Blog

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL
  • Key Considerations When Implementing Virtual Kubernetes Clusters
  • The Future of Cloud Engineering Evolves
  • 7 Awesome Libraries for Java Unit and Integration Testing

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: