Description: Functional Programming in ABAP test by PDH
Submitted on December 30, 2014 at 09:23 AM

Section 1 (Text)

*&---------------------------------------------------------------------*
*& Report  Y_FUNCTIONAL_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  y_functional_test.

DATA: gd_result TYPE i.

*----------------------------------------------------------------------*
*       CLASS lcl_functional_programming DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_functional_programming DEFINITION.
  PUBLIC SECTION.
    METHODS: do_fib IMPORTING number_to_add  TYPE i
                              result_so_far  TYPE i
                              loops_left     TYPE i
                    RETURNING value(result)  TYPE i,
             functional IMPORTING loops_left    TYPE i
                        RETURNING value(result) TYPE i,
             no_more    IMPORTING loops_left      TYPE i
                        RETURNING value(finished) TYPE abap_bool.

ENDCLASS.                    "lcl_functional_programming DEFINITION

DATA: go_lcl_functional_programming TYPE REF TO lcl_functional_programming.

START-OF-SELECTION.
*--------------------------------------------------------------------*
* Imperative Programing
*--------------------------------------------------------------------*
  PERFORM imperative USING    8
                     CHANGING gd_result.

  WRITE:/ gd_result.

*--------------------------------------------------------------------*
* Functional Programming
*--------------------------------------------------------------------*
  CREATE OBJECT go_lcl_functional_programming.

  gd_result = go_lcl_functional_programming->functional( 8 ).

  WRITE:/ gd_result.

*&---------------------------------------------------------------------*
*&      Form  IMPERATIVE
*&---------------------------------------------------------------------*
FORM imperative  USING    pud_loops  TYPE i
                 CHANGING pcd_result TYPE i.

  PERFORM imperative_fibonacci USING     0
                                         1
                                         8
                               CHANGING  gd_result.

ENDFORM.                    " IMPERATIVE
*&---------------------------------------------------------------------*
*&      Form  imperative_fibonacci
*&---------------------------------------------------------------------*
* In an imperative programing style with use LOOPS such as
* WHILE/ENDWHILE
*--------------------------------------------------------------------*
FORM imperative_fibonacci  USING    pud_first_number    TYPE i
                                    pud_second_number   TYPE i
                                    pud_number_of_loops TYPE i
                           CHANGING pcd_result          TYPE i.
* Local Variables
  DATA: ld_number_to_add TYPE i,
        ld_result_so_far TYPE i,
        ld_loops_left    TYPE i.

  CLEAR pcd_result.

  ld_number_to_add = pud_first_number.
  ld_result_so_far = pud_second_number.
  ld_loops_left    = pud_number_of_loops.

  WHILE ld_loops_left GT 1.

    pcd_result = ld_number_to_add + ld_result_so_far.

    ld_number_to_add = ld_result_so_far.

    ld_result_so_far = pcd_result.

    SUBTRACT 1 FROM ld_loops_left.

  ENDWHILE.

ENDFORM.                    " imperative_fibonacci
*----------------------------------------------------------------------*
*       CLASS lcl_functional_programming IMPLEMENTATION
*----------------------------------------------------------------------*
* In a Functional stype we use RECURSION instead of loops and use
* ternary operators instead of If statements except there are no
* ternary operators in ABAP (I think)
*----------------------------------------------------------------------*
CLASS lcl_functional_programming IMPLEMENTATION.

  METHOD do_fib.
*--------------------------------------------------------------------*
* IMPORTING number_to_add  TYPE i
*           result_so_far  TYPE i
*           loops_left     TYPE i
* RETURNING value(result)  TYPE i
*--------------------------------------------------------------------*
    IF no_more( loops_left ) = abap_true.
      result = result_so_far.
    ELSE.

      result = me->do_fib( number_to_add = result_so_far
                           result_so_far = ( number_to_add + result_so_far )
                           loops_left    = ( loops_left - 1 ) ).

    ENDIF.

  ENDMETHOD.                                               "do_fib

  METHOD functional.
*--------------------------------------------------------------------*
* IMPORTING loops_left    TYPE i
* RETURNING value(result) TYPE i
*--------------------------------------------------------------------*
    result = do_fib( number_to_add = 0
                     result_so_far = 1
                     loops_left    = loops_left ).

  ENDMETHOD.                                               "functional

  METHOD no_more.
*--------------------------------------------------------------------*
* IMPORTING loops_left      TYPE i
* RETURNING value(finished) TYPE abap_bool
*--------------------------------------------------------------------*
    IF loops_left = 1.
      finished = abap_true.
    ELSE.
      finished = abap_false.
    ENDIF.

  ENDMETHOD."No More

ENDCLASS.                    "lcl_functional_programming IMPLEMENTATION