Functional Programming with Java Tutorial
Selected Reading
- Discussion
- Resources
- Quick Guide
- Fixed Length Streams
- Infinite Streams
- Terminal methods
- Intermediate Methods
- Exception Handling
- Type Inference
- Pure Functions
- First Class Functions
- Returning a Function
- High Order Functions
- Collections
- Constructor References
- Method References
- Functional Interfaces
- Default Methods
- Lambda Expressions
- Reducing
- Currying
- Closure
- Optionals & Monads
- Parallelism
- Recursion
- Persistent Data Structure
- Eager vs Lazy Evaluation
- Functional Composition
- Functions
- Overview
- Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Currying
Functional Programming with Java - Currying
Currying is a technique where a many arguments function call is replaced with multiple method calls with lesser arguments.
See the below equation.
(1 + 2 + 3) = 1 + (2 + 3) = 1 + 5 = 6
In terms of functions:
f(1,2,3) = g(1) + h(2 + 3) = 1 + 5 = 6
This cascading of functions is called currying and calls to cascaded functions must gives the same result as by calpng the main function.
Following example shows how Currying works.
import java.util.function.Function; pubpc class FunctionTester { pubpc static void main(String[] args) { Function<Integer, Function<Integer, Function<Integer, Integer>>> addNumbers = u -> v -> w -> u + v + w; int result = addNumbers.apply(2).apply(3).apply(4); System.out.println(result); } }
Output
9Advertisements