Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Does ABAP support functional programming?

Former Member
3,311

Does ABAP support functional programming?

1 ACCEPTED SOLUTION
Read only

Former Member
2,223

Hi,

ABAP does NOT support true functional programming (FP). Functions are not first class citizens in ABAP. As a side note, ABAP is also not a pure object oriented language as well. Those two facts together defeat possibility to use many many handy FP constructs at the moment. Maybe in future relases it will come, who knows. But at the moment, you can not pass code bits as a parameters to functions if that's what you were asking about.

For those who are not properly educated on what FP really is: FP is not defined by the ability to use functions and funcional expresions in this manner:

DATA(segment) = document->create_simple_element_ns(

  name   = |{ replace( val = <edidd>-segnam regex = |^Z1IDE_(.*)_D14B$| with = |$1| ) }|

  parent = map[ segnum = <edidd>-psgnum ]-element

).

FP is defined as ability to store functions and code bits in variables like any other type of data (in the end, code is also a bit of data - code compiled into instructions for example, or a pointer to a place with the compiled code, both with metadata about parameters, return values and thrown exceptions...). If ABAP supported true FP, I could something like this (my ABAP pseudocode used here):

TYPES t_func TYPE FUNCTION IMPORTING segnam TYPE string RETURNING value(name) TYPE string.

DATA(strip) = VALUE t_func(

  " ABAP code here

  name = replace( val = segnam regex = |^Z1IDE_(.*)_D14B$| with = |$1| ) }|

).

And then in every place I want to strip the IDoc segment name, I would do this (note how strip variable would now be used just like a built in function):

DATA(segment) = document->create_simple_element_ns(

  name   = strip( <edidd>-segnam )

  parent = map[ segnum = <edidd>-psgnum ]-element

).

With this concept, one can define generic algorithms and then just plug in simple code bit as a parameter that defines what the whole generic construct does then. For example this FP concept almost entirely defeats purpose of loops and need to use variables.

If you check newest ABAP additions, you can find that SAP added 2 functional concepts into ABAP lately:

1) table comprehensions - like map functions in FP

2) table reductions - like fold functions in FP

Generally, the new additions into ABAP in version 7.40 are amazing from my point of view. The code verbosity is now extremely in some places and quite neat constructs can now be created that allow to do the same amount of work in maybe 1/3 of code than before.

David

13 REPLIES 13
Read only

rosenberg_eitan
Active Contributor
0 Likes
2,223

Hi,

If you refer to somthing like the new Lambda expressions in java 8 .

Did not see any thing like it since 2006 (When I was introduced to ABAP).

Regards.

Read only

0 Likes
2,223

Yes, that's what I'm referring.

Read only

custodio_deoliveira
Active Contributor
0 Likes
2,223

Yes.

Read only

0 Likes
2,223

Hi,


An example please.


Regards.

Read only

0 Likes
2,223

yeah? How?

Any document?

function in ABAP is not first-class citizen if I understand correctly.

Read only

0 Likes
2,223

Hi Eitan,

My response is based on this blog post: http://blog.acorel.nl/2013/12/how-abap-moves-towards-functional.html

Now, I'm no expert in functional programming, only know the basics. But if Aaron's questions is as clear as he claims and he knows ABAP function modules are not first-class citzens (they really aren't), so this is just a rhetoric question.

I probably should not have answered "yes", it's more like "kind of does", or "it's getting there", as it provides some of functional programming features, but not all.

Cheers,

Custodio

PS: another blog post to consider is this: The Essence of Functional Programming and ABAP. | &amp;#169; Passionate about SAP - A Blog

Read only

0 Likes
2,223

Hi,

From a response for the post you menion:

http://blog.acorel.nl/2013/12/how-abap-moves-towards-functional.html?showComment=1386928417961#c6402...
"I cannot really claim to understand ABAP but I think I have a good enough grasp of FP - that said I cannot find any main ideas from FP in your examples"

If you know Java you can look at:
http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

More general info:
http://en.wikipedia.org/wiki/Functional_programming

Small java sample:

private class MyWorker implements Runnable {
  @Override
  public void run() {
   // Do some work in different thread
  }
}

public void test_1() {

  {
   // Using Runnable in traditional way
   new Thread(new MyWorker()).start();
  }
  {
   // Using lambda
   new Thread(() -> {
    // Do some work in different thread
   });
  }

}


Regards.

Read only

0 Likes
2,223

Thanks Eitan,

I haven't read the comments earlier. It seems I should have.

Cheers,

Custodio

Read only

0 Likes
2,223

Hi,

Glad to be of help.

Regards.

Read only

krishna_k19
Contributor
0 Likes
2,223

Hi Aaron,

   your question is not clear.

Can you explain briefly..

Regards,

Krishna

Read only

0 Likes
2,223

I think it's pretty clear, maybe you don't know the concept of functional programming. just google it.

Read only

Former Member
0 Likes
2,223

Hi,

Let me try. Correct me if i am wrong, I think it supports functional programming.

Here is an example i tried.

Suppose u got an internal table(itab) with 10 records.

If lines ( itab ) GE 1.

  write : 'Hello'.

Endif.

Regards.

Read only

Former Member
2,224

Hi,

ABAP does NOT support true functional programming (FP). Functions are not first class citizens in ABAP. As a side note, ABAP is also not a pure object oriented language as well. Those two facts together defeat possibility to use many many handy FP constructs at the moment. Maybe in future relases it will come, who knows. But at the moment, you can not pass code bits as a parameters to functions if that's what you were asking about.

For those who are not properly educated on what FP really is: FP is not defined by the ability to use functions and funcional expresions in this manner:

DATA(segment) = document->create_simple_element_ns(

  name   = |{ replace( val = <edidd>-segnam regex = |^Z1IDE_(.*)_D14B$| with = |$1| ) }|

  parent = map[ segnum = <edidd>-psgnum ]-element

).

FP is defined as ability to store functions and code bits in variables like any other type of data (in the end, code is also a bit of data - code compiled into instructions for example, or a pointer to a place with the compiled code, both with metadata about parameters, return values and thrown exceptions...). If ABAP supported true FP, I could something like this (my ABAP pseudocode used here):

TYPES t_func TYPE FUNCTION IMPORTING segnam TYPE string RETURNING value(name) TYPE string.

DATA(strip) = VALUE t_func(

  " ABAP code here

  name = replace( val = segnam regex = |^Z1IDE_(.*)_D14B$| with = |$1| ) }|

).

And then in every place I want to strip the IDoc segment name, I would do this (note how strip variable would now be used just like a built in function):

DATA(segment) = document->create_simple_element_ns(

  name   = strip( <edidd>-segnam )

  parent = map[ segnum = <edidd>-psgnum ]-element

).

With this concept, one can define generic algorithms and then just plug in simple code bit as a parameter that defines what the whole generic construct does then. For example this FP concept almost entirely defeats purpose of loops and need to use variables.

If you check newest ABAP additions, you can find that SAP added 2 functional concepts into ABAP lately:

1) table comprehensions - like map functions in FP

2) table reductions - like fold functions in FP

Generally, the new additions into ABAP in version 7.40 are amazing from my point of view. The code verbosity is now extremely in some places and quite neat constructs can now be created that allow to do the same amount of work in maybe 1/3 of code than before.

David