The name “Currying”, coined by Christopher Strachey in 1967, is a reference to logician Haskell Curry. Although the concept defined in wikipedia looks a little bit obscure, in fact every one should have already learned it during his / her high school in mathematics class.

Still remember how you resolve this equation below? During the process, you are actually applying the Curry idea!

1. in line 3, I define a Curry function as a factory, which will return a NEW function with bound argument passed in Curry call.

2. in line 9, I get a new curried function curriedPrint with a default programming language ABAP. It means from now on I can call this new function without passing ABAP as argument any more.

3. In line 11 I call this new function by specifying other languages.

and finally in output ABAP are still included:

A typical advantage of Curry is, it is now possible to develop a couple of small but REUSABLE functions with fine granularity and fulfill complex requirement by constructing them via Curry.

See this example which will generate output: jerry-abap

The conversion task is not done via the idea of functional programming: it simply leverages the built-in method provided by String and Array.

See how it could be rewritten in a PURE functional programming way using Curry:

Among lines 8 ~ 11, four dedicated functions are defined and each one has its single responsibility. Finally a new conversion function is composited in line 13, this process is called function composition.

Output in console: jerry-java

Curry in ABAP

Now let’s see how to implement the first language print example in ABAP using Curry. Always keep in mind the idea of Curry: suppose I have a function with m numbers of arguments. I construct a new function bound with a set of say n numbers of argument passed during Curry process, and after that I can just call the new curried function with ( m – n ) numbers of argument.

Let’s first see what can be achieved in ABAP.

I have a simple print function module ZJERRY_PRINT in ABAP with 3 optional importing parameters:

And this function module can simply be curried via factory method ZCL_CURRY by passing name of original function module plug bound argument value.

Test output

In the first time, when I call first curried function module, although I didn’t explicitly specify the first argument with value ABAP, still it appears in the result output, since the value of this argument is already bound to the “context” of curried function module when it is constructed.

The same rule holds for the second round test, where I bind two arguments to the second curried function module.

How it is implemented in ABAP?

Where in JavaScript, a curried function can simply be created together with bound argument passed by consumer just WITHIN A SINGLE LINE,

actually what you see is just a tip of the iceberg. Take V8 JavaScript engine in Chrome for example, the implementation of bind behind the scene is really tremendous.

And my ABAP implementation consists of three steps.

Step1: parse the signature of function module to be curried, to prepare the dynamical generation of curried function module in next step.

Step2: technically speaking the curried function module has the same signature as the original function module, so just generate it dynamically by making use of signature parsed from first step.

Step3: source code post adaptation. This post activity consists of two further steps.

1. For those parameters which already have bound value passed in during curry call, use the bound one instead.

2. Populate source code which will internally delegate the call to the original function module.

Below is the source code of curried function module for original function ZJERRY_PRINT with first argument bound with value ‘ABAP’: