Tuesday, 16 May 2017

Functional Programming – Try Reduce in JavaScript and in ABAP

In that blog I use the curry idea in the example below:

SAP ABAP Tutorials and Materials, SAP ABAP Certifications

output:

jerry-java

In this blog, as blog title, I will show another approach by leveraging built-in function reduce in JavaScript Array and Reduce keyword in ABAP to fulfill the same requirement.

First see official documentation for Array.reduce.

Follow the idea of functional programming, I first declare three dedicated functions which are responsible for a specific task in line 6 ~ 8.

SAP ABAP Tutorials and Materials, SAP ABAP Certifications

Then in line 10, I construct a new composite function which will finally executes the three small functions driven by reduce.

Output:
jerry-java-scala

Reduce in ABAP


Now let’s see how we can achieve the same function in ABAP.
If we use the traditional imperative programming paradigm, the code is written as:
DATA: lv_input TYPE string value 'Jerry Java Scala',
      lv_result TYPE string.

SPLIT lv_input AT space INTO TABLE DATA(lt_temp).
LOOP AT lt_temp ASSIGNING FIELD-SYMBOL(<temp>).
   lv_result = lv_result && |{ <temp>  case = lower }| .
   IF sy-tabix <> lines( lt_temp ).
      lv_result = lv_result && '-'.
   ENDIF.
ENDLOOP.

WRITE: / lv_result.
Output:

jerry-java-scala

Now let’s try with Reduce. Here is the ABAP documentation for keyword REDUCE.

Source Code


REPORT demo_reduce_cond_iteration.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    DATA(out) = cl_demo_output=>new(
      )->next_section( 'Summation'
      )->write( REDUCE i( INIT sum = 0
                          FOR n = 1 UNTIL n > 10
                          NEXT sum = sum + n )
      )->next_section( 'Concatenation without THEN'
      )->write( REDUCE string( INIT text = `Count up:`
                               FOR n = 1 UNTIL n > 10
                               NEXT text = text && | { n }| )
      )->next_section( 'Concatenation with THEN'
      )->write( REDUCE string( INIT text = `Count down:`
                               FOR n = 10 THEN n - 1 WHILE n > 0
                               NEXT text = text && | { n }| )
      )->next_section( 'Non arithmetic expression'
      )->write( REDUCE string( INIT text = ``
                               FOR t = `x` THEN t && `y`
                                           UNTIL strlen( t ) > 10
                               NEXT text = text && |{ t } | )
      )->display( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

See how ABAP code can be re-written using REDUCE:

SAP ABAP Tutorials and Materials, SAP ABAP Certifications

This version looks quite clear and more human-readable compared with the first imperative one:

1. in line 11~13 there are three command instance created and inserted into a command queue held by variable lo_queue.

2. command queue ZCL_COMMAND_QUEUE internally execute the commands one by one using REDUCE as the execution engine.
The rough design of ABAP solution using REDUCE is listed below:

SAP ABAP Tutorials and Materials, SAP ABAP Certifications

1. I have declare an interface ZIF_COMMAND which has one method DO to perform concrete command, one method GET_RESULT to return command execution result and method SET_TASK to specify command input.

2. ZCL_COMMAND_QUEUE has internally maintained a command queue. The enqueue operation is provided by public method INSERT.

Another public method EXECUTE is defined to fire the command queue execution.
Inside this method, ABAP keyword is used. The last command in the queue will be returned and its interface method get_result is called to return the final calculation request to consumer call.

SAP ABAP Tutorials and Materials, SAP ABAP Certifications

No comments:

Post a Comment