Scrivio Interview library
CodingIntermediate6 min read · updated 12 November 2026

Show how to write a curried function in Java for add(a, b, c) and how to partially apply a BiFunction. When is this useful in real code?

A curried add(a, b, c) in Java is Function<Integer, Function<Integer, Function<Integer, Integer>>> add = a -> b -> c -> a + b + c;, called as add.apply(1).apply(2).apply(3), which gives 6. Partial application of a BiFunction fixes its first argument: static <A, B, R> Function<B, R> partial(BiFunction<A, B, R> f, A a) { return b -> f.apply(a, b); }. It is useful for configuring a function with a fixed dependency, such as a tax rate, before passing it to a stream, but deep curried types are hard to read and box primitives, so a small record or class with a method is often clearer.

Why this question is asked

Currying sounds academic, but its everyday cousin, partial application, appears in real Java code whenever we prepare a function with some settings and then hand it to a stream, a callback or a scheduler. Interviewers ask this to see whether we can write the types correctly, distinguish the two ideas, and, at this level, judge when the technique helps and when a plain class is better. In this answer, we will recap the two terms, write a curried add, write a generic partial helper, use it for a realistic tax example, and then look at readability and boxing.

1A Quick Recap of Currying and Partial Application

Currying turns a function of several arguments into a chain of functions of one argument each. Instead of add(a, b, c), we get a function that takes a and returns a function that takes b, which returns a function that takes c and finally computes the result. The name comes from the logician Haskell Curry.

Partial application is related but different: we take a function of several arguments, fix some of them now, and get back a function of the remaining ones. Currying makes partial application trivial, because applying the first function in the chain is a partial application. In Java, which has no built-in currying, we usually write partial application directly.

2A Curried add in Java

Java has no special syntax, so we write the nested function types out in full.

Java
Function<Integer, Function<Integer, Function<Integer, Integer>>> add =
a -> b -> c -> a + b + c;
int six = add.apply(1).apply(2).apply(3); // 6
Function<Integer, Function<Integer, Integer>> addTen = add.apply(10); // a fixed to 10
Function<Integer, Integer> addTenAndFive = addTen.apply(5); // b fixed to 5
addTenAndFive.apply(1); // 16
addTenAndFive.apply(100); // 115
Applying the curried add one argument at a time
ExpressionTypeValue
addFunction<Integer, Function<Integer, Function<Integer, Integer>>>a -> b -> c -> a + b + c
add.apply(10)Function<Integer, Function<Integer, Integer>>b -> c -> 10 + b + c
add.apply(10).apply(5)Function<Integer, Integer>c -> 10 + 5 + c
add.apply(10).apply(5).apply(1)Integer16

Read the lambda from the left: a -> (b -> (c -> a + b + c)). Each arrow returns the next function, and each inner lambda captures the values of the outer ones. Notice the type: three nested Functions for three arguments, which is already hard to read, and it only grows with more parameters.

3Partial Application of a BiFunction

More often we already have a two-argument function and want to fix one argument. A small generic helper does it.

Java
static <A, B, R> Function<B, R> partial(BiFunction<A, B, R> f, A a) {
return b -> f.apply(a, b); // a is captured; b arrives later
}
BiFunction<String, String, String> greet = (greeting, name) -> greeting + ", " + name;
Function<String, String> hello = partial(greet, "Hello");
hello.apply("Ana"); // "Hello, Ana"
hello.apply("Bo"); // "Hello, Bo"

The helper returns a new lambda that remembers a and waits for b. The generic types make it work for any BiFunction, and the result is an ordinary Function, so it fits anywhere a Function is expected, including Stream.map.

4A Realistic Use: Configuring a Function Before a Stream

Here is where the technique earns its place. A calculation needs a dependency, such as a tax rate, that is the same for a whole batch, and a value that varies per element. We fix the dependency once and pass the resulting function to the stream.

Java
BiFunction<BigDecimal, BigDecimal, BigDecimal> withTax =
(rate, net) -> net.add(net.multiply(rate));
Function<BigDecimal, BigDecimal> withUkVat = partial(withTax, new BigDecimal("0.20"));
List<BigDecimal> gross = List.of(new BigDecimal("10.00"), new BigDecimal("2.50"))
.stream()
.map(withUkVat)
.toList(); // [12.0000, 3.0000]

The stream code does not know about tax rates at all; it just maps with a function. The same withTax can produce withGermanVat or withZeroRate, and each is configured once, outside the loop. This is the pattern behind many everyday APIs too, such as Comparator.comparing taking a key extractor or a factory method that returns a configured validator.

5When a Small Class or Record Is Clearer

Deeply curried types are hard to read in Java. A reviewer seeing Function<BigDecimal, Function<Region, Function<Order, Money>>> has to count brackets to know what goes where, the parameters have no names, and IDE hints show only apply. A small record with a method expresses the same idea, a configured calculation, with names.

Java
record TaxRate(BigDecimal rate) {
BigDecimal applyTo(BigDecimal net) { return net.add(net.multiply(rate)); }
}
TaxRate ukVat = new TaxRate(new BigDecimal("0.20"));
prices.stream().map(ukVat::applyTo).toList(); // [12.0000, 3.0000]: same result, named parts

The record is the partially applied function: its fields are the fixed arguments, and its method takes the rest. It also has a name for logs and stack traces, can be documented, and can gain a second method later without changing any types. For one fixed argument, partial is fine; for more than that, or in a public API, I would choose the record.

6The Cost of Boxing

Generic functional interfaces work only with reference types, so Function<Integer, ...> boxes every int into an Integer. In the curried add, each apply call may allocate Integer objects outside the small cache from -128 to 127, plus the intermediate function objects that capture a and b. For occasional calls this is negligible, but in a hot numeric loop it adds garbage and indirection.

Java offers primitive specialisations such as IntUnaryOperator, IntBinaryOperator and ToIntFunction for this reason. A curried version would be IntFunction<IntFunction<IntUnaryOperator>> add = a -> b -> c -> a + b + c, which avoids boxing the arguments, though it is even harder to read. In performance-sensitive code, a plain method is usually the best answer.

7Follow-up questions

Is currying the same as method overloading with fewer arguments?

No. Overloading gives several methods with different parameter lists that the compiler chooses between at compile time. Currying and partial application create new function values at runtime with some arguments already fixed, which can then be passed around and called later.

Could I write a generic curry helper for BiFunction?

Yes: static <A, B, R> Function<A, Function<B, R>> curry(BiFunction<A, B, R> f) { return a -> b -> f.apply(a, b); }. It is a nice exercise, but in real code partial application of a specific argument is what is usually needed.

Does capturing values in lambdas cause memory leaks?

A lambda keeps references to the values it captures for as long as the lambda itself is reachable. If a configured function captures a large object and is stored in a long-lived cache, that object stays in memory too. Capturing small values such as a rate is harmless.

8Conclusion

In this answer, we saw that currying turns add(a, b, c) into a chain of one-argument functions, written in Java as nested Function types and called with apply three times, while partial application fixes some arguments of an existing function, which a generic partial helper does for any BiFunction. The technique is useful for configuring a function with a fixed dependency, like a tax rate, before passing it to a stream. Deep curried types are hard to read and box primitives, so a small record with a named method is often the clearer way to express the same idea.

What to say in the interviewAbout 90 seconds aloud

Open with

Currying turns a function of several arguments into a chain of one-argument functions. In Java I'd write the curried add as a Function of Integer to a Function of Integer to a Function of Integer to Integer, defined as a -> b -> c -> a + b + c, and call it with add.apply(1).apply(2).apply(3), which gives 6.

Then, if they want more

Each inner lambda captures the outer values, so add.apply(10) is already a useful function that adds ten to whatever comes next. Partial application is the more practical idea: take a function and fix some arguments now. A generic helper does it for a BiFunction: partial takes the BiFunction and a value a, and returns b -> f.apply(a, b).

Where I'd actually use it is configuring a function with a fixed dependency before a stream. For example, a withTax BiFunction of rate and net amount, partially applied with 0.20, gives a withUkVat function I can pass straight to map, and the stream code knows nothing about rates.

That said, deep curried types are hard to read in Java: no parameter names, lots of nested generics, and IDE hints just say apply. So beyond one fixed argument, or in a public API, I'd use a small record like TaxRate with an applyTo method, and pass ukVat::applyTo.

And because generic functions only take reference types, everything gets boxed, so in hot numeric code I'd use primitive interfaces or just a plain method.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Show how to write a curried function in Java for add(a, b, c) and how to partially apply...": https://scrivio-interview-library.netlify.app/java/functional-design/ca52852096/