用ABAP模擬JavaScript的柯里化語言特性(Curry)
As I mentioned in What should an ABAPer continue to learn as an application developer, function programming language is a mind -blower to ABAPers who have got used to ABAP as an imperative programming language.
One of important concept in functional programming is Currying.
Currying intensively manipulates functions.
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!
Let’s now see some real example in code.
<script type="text/javascript">function jerryPrint(person, ...arg){
console.log(person + " can program with: " + arg);}jerryPrint("Jerry", "Java", "JavaScript", "Scala", "C++");</script>
No doubt we can get the following output:
Now use Curry concept.
(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:
Its implementation is simple:
WRITE: / 'Jerry can program with the following: ' COLOR COL_GROUP.IF iv_first IS SUPPLIED.
WRITE: / iv_first COLOR COL_NEGATIVE.ENDIF.IF iv_second IS SUPPLIED.
WRITE: / iv_second COLOR COL_HEADING.ENDIF.IF iv_third IS SUPPLIED.
WRITE: / iv_third COLOR COL_POSITIVE.ENDIF.
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’:
FUNCTION zcurry20170226043103.*"--------------------------------------------------------------------*"*"Local Interface:*" IMPORTING*" REFERENCE(IV_FIRST) TYPE STRING OPTIONAL*" REFERENCE(IV_SECOND) TYPE STRING OPTIONAL*" REFERENCE(IV_THIRD) TYPE STRING OPTIONAL*"--------------------------------------------------------------------
DATA: lt_ptab TYPE abap_func_parmbind_tab.
DATA: ls_para LIKE LINE OF lt_ptab.
DATA: _iv_first LIKE iv_first.
_iv_first = 'ABAP'.
ls_para = VALUE #( name = 'IV_FIRST' kind = abap_func_exporting value = REF #( _iv_first ) ).
APPEND ls_para TO lt_ptab.
DATA: _iv_second LIKE iv_second.
_iv_second = iv_second.
ls_para = VALUE #( name = 'IV_SECOND' kind = abap_func_exporting value = REF #( _iv_second ) ).
APPEND ls_para TO lt_ptab.
DATA: _iv_third LIKE iv_third.
_iv_third = iv_third.
ls_para = VALUE #( name = 'IV_THIRD' kind = abap_func_exporting value = REF #( _iv_third ) ).
APPEND ls_para TO lt_ptab.
TRY.
CALL FUNCTION 'ZJERRY_PRINT' PARAMETER-TABLE lt_ptab.
CATCH cx_root INTO DATA(cx_root).
WRITE: / cx_root->get_text( ).
ENDTRY.ENDFUNCTION.
Finally ZCL_CURRY provides a method CLEANUP to delete those automatically generated function module.
Hope this little prototype can help ABAPers to have a basic understanding about Curry concept in functional programming world.
Further reading
I have written a series of blogs which compare the language feature among ABAP, JavaScript and Java. You can find a list of them below:
- Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP
- Functional programming – Simulate Curry in ABAP
- Functional Programming – Try Reduce in JavaScript and in ABAP
- Simulate Mockito in ABAP
- A simulation of Java Spring dependency injection annotation @Inject in ABAP
- Singleton bypass – ABAP and Java
- Weak reference in ABAP and Java
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2715904/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JavaScript 函數語言程式設計---柯里化JavaScript函數程式設計
- JavaScript柯里化JavaScript
- JavaScript 函數語言程式設計技巧 - 反柯里化JavaScript函數程式設計
- 瞭解 JavaScript 函數語言程式設計 - 柯里化JavaScript函數程式設計
- 簡單理解JavaScript中的柯里化和反柯里化JavaScript
- 「譯」理解JavaScript的柯里化JavaScript
- JavaScript函式柯里化JavaScript函式
- 【譯】理解JavaScript中的柯里化JavaScript
- JavaScript函式柯里化的作用JavaScript函式
- 柯里化函式之Javascript函式JavaScript
- 高階函式應用 —— 柯里化與反柯里化函式
- JavaScript 中的函數語言程式設計:函式,組合和柯里化JavaScript函數程式設計函式
- js函數語言程式設計(二)-柯里化JS函數程式設計
- [譯] JavaScript中的函式柯里化JavaScript函式
- 【新手教程】JavaScript的柯里化函式JavaScript函式
- Javascript currying柯里化詳解JavaScript
- JavaScript函式柯里化詳解JavaScript函式
- JAVAScript柯里化、部分應用引數終極理解JavaScript
- JavaScript進階之函式柯里化JavaScript函式
- JavaScript 專題之函式柯里化JavaScript函式
- 理解柯里化
- JavaScript函數語言程式設計(純函式、柯里化以及組合函式)JavaScript函數程式設計函式
- JavaScript函式柯里化的一些思考JavaScript函式
- 大佬,JavaScript 柯里化,瞭解一下?JavaScript
- 函式式JavaScript(4):函式柯里化函式JavaScript
- 函式柯里化函式
- 前端戰五渣學JavaScript——函式柯里化前端JavaScript函式
- 深入理解javascript系列(十七):函式柯里化JavaScript函式
- JS:函式柯里化JS函式
- Js函式柯里化JS函式
- JavaScript 函數語言程式設計中的 curry 實現JavaScript函數程式設計
- 用大白話介紹柯里化函式函式
- 函式的合成與柯里化函式
- JavaScript中的事件迴圈機制跟函式柯里化JavaScript事件函式
- 函式柯里化和偏函式應用函式
- js柯里化函式的好處JS函式
- 到底JS柯里化在專案中怎麼用?JS
- 一道柯里化面試題面試題