Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
hardyp180
Active Contributor
Dear Christmas Fans,

You are no doubt familiar with the concept of an "advent calendar" where you open a door each day in December leading up to Christmas and eat a chocolate of some such.

This December I have been following a series of posts on the following site:-

https://functional.christmas/

Each day there was an article trying to explain the basics of Functional Programming. As background Java and ABAP are "imperative" languages, JavaScript is supposed to be a "functional" language and there are other languages which are "pure" functional languages.

What's the difference? I had no idea. I did know that in recent releases of ABAP new "functional" keywords like REDUCE and FILTER were introduced which supposedly do the same thing as their functional equivalents (do they? someone can answer that question if they so desire).

Why am I interested in functional programming? Two main reasons - firstly these days a working knowledge of JavaScript is becoming more and more important due to UI5 but secondly and probably the killer argument is that D.J. Adams says that writing programs in a functional language is fun and I like fun things.

The articles in the functional Christmas series mostly went over my head, and it did not help they used examples like A, B, C, X, Y, Z as variable names rather than proper business specific domain names like Monsters which anyone can relate to.

Academic articles always do that as well. It is so difficult to get your head round something like f(a) = b ~ c (d) * (e) / (f) as opposed to DATA(monsters_under_bed) = bed->monster_count( ).

Nonetheless I encourage everyone to have a read, one a day like I did, so your head does not explode.

One concept I finally got, it was so strange to me, was the idea of a function returning another function as a result. I just could not understand that one.

Now, in the real life example I just gave the variable MONSTERS_UNDER_BED is clearly the result of a call to a method. In such constructs the result is on the left hand side.

Now let us say, in a functional language we have a function that takes two arguments, colour and height and returns some important monster related information based on those two inputs.

As it turns out you can create a new function based on the old function by passing less arguments than it expects. It looks something like this:-

LET blue_monster_function = all_monsters_function BLUE.

The result is on the left hand side. You have a new function called BLUE_MONSTER_FUNCTION where you only pass in the height and the BLUE parameter is hard coded.

This, apparently, is called a "curried egg" after Professor Curried Egg who invented the concept and has fifty four functional languages named after him. Some historians argue however that the Russian Academic and Logician Professor Egginov Currinovlesky got there first a hundred years earlier.

The other interesting thing is something called "tail recursion". In ABAP we usually process data using a LOOP over an internal table. In functional programming you generally use recursion instead and indeed in some pure functional languages that is the only means of looping possible.

In one article the author said such recursion can sometimes cause a "stack overflow" (hence the name of the website) so you have to mess with the code so that it looks like it is doing recursion but in the background the compiler is REALLY doing a loop just like in ABAP.

You can of course use recursion in ABAP, and I often have. You have to be careful not to cause an endless loop as you do not get a "stack overflow" error it just sorts of hangs there forever until you get a TIME OUT.

Lastly what might make ABAP programmers heads spin is the concept of immutability. In pure functional languages once you have given a variable a value it cannot be changed. That seems crazy at first glance. If I have an application to call up a sales order, or a monster master record, and change something then presumably I need to have a variable which has the initial value and then the new changed one based on user input.

To quote Robert Martin (Uncle Bob) rather than changing the existing variable you have to create an "entire new universe" which is exactly the same as the last one with just one small aspect changed. He did a series of blogs detailing exactly how to do this based on a Star Trek Captain Kirk vs the Klingon's game he wrote using a functional language.

https://blog.cleancoder.com/uncle-bob/2018/12/17/FPvsOO-List-processing.html

What do the panel think about this? Do we need more functional programming constructs in ABAP? Do the ones already there work properly? Have you used functional programming in JavaScript? is it fun?

Cheersy Cheers

Paul

 

 

 
4 Comments