java-design-patterns icon indicating copy to clipboard operation
java-design-patterns copied to clipboard

Currying

Open javatlacati opened this issue 2 years ago • 7 comments

I's a functional programming design pattern. As far as I understand you call a function with less parameters than usual and it can be used to defer processing when it has many steps involved.

UML example

@startuml
'https://plantuml.com/class-diagram

interface BiFunction <ParamType1, ParamType2, ResultType>{
}

interface Curried<ParamType1, ParamType2 extends Simple> {
  +ParamType2 apply(final ParamType1 a);
}

interface Simple<ParamType1, ParamType2, ResultType> extends BiFunction {
  +ResultType apply(final ParamType1 a, final ParamType2 b);
}

@enduml

image

With the curried version you can do stuff like:

   System.out.println(curriedAdd.apply(4).apply(5));

    Function1<Integer, Integer> adder5 = curriedAdd.apply(5);
    //some complicate and time consuming process here
    System.out.println(adder5.apply(4));
    System.out.println(adder5.apply(6));

javatlacati avatar Oct 07 '21 16:10 javatlacati

There is a concrete example about currying in Java on Baeldung's website: https://www.baeldung.com/java-currying

iluwatar avatar Oct 08 '21 17:10 iluwatar

hi @iluwatar , can I work on this issue for Hacktoberfest?

januslinhc avatar Oct 11 '21 16:10 januslinhc

Sure @januslinhc, it's now assigned to you

iluwatar avatar Oct 13 '21 16:10 iluwatar

Hey @iluwatar, could I work on this issue?

hkattt avatar Oct 10 '22 09:10 hkattt

All right @hkattt, you're free to proceed

iluwatar avatar Oct 10 '22 15:10 iluwatar

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 28 '22 12:11 stale[bot]

@hkattt is still working on this

iluwatar avatar Dec 03 '22 17:12 iluwatar