Technical Articles
The ABAP REDUCE Operator | A Tutorial for 2020
On one of my more recent projects I went through a code review and afterwards my team lead pulled me aside and asked me to explain something. As it turns out he was referring to my use of the REDUCE operator in one the class methods. Much to my surprise this particular operator is not as widely used as I thought and the reason doesn’t seem to be that it’s actually hard to use but rather it just “looks” difficult to begin with.
It’s a great way to quickly and efficiently sum up or collect strings in a way that lets you summarize large tables of information. It’s also, in my humble opinion, a very clean and tidy way to collect summarised data making the logic easy to read for other developers.
So in this article I really want to encourage you, if you aren’t already doing so, to give the REDUCE operator a try 🙂
The SAP documentation on this particular operator is really well written and so I made a short video simply following 3 examples directly from the online documentation.
These are the three examples I run through:
REDUCE – Reduction Operator – https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-us/abenconstructor_expression_reduce.htm
Creating Values with FOR and REDUCE – https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-us/abencond_iteration_reduce_abexa.htm
Table Reductions, Summation of an Array – https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-us/abenreduce_simple_abexa.htm
There’s also a great blog post by Horst Keller on the subject – https://blogs.sap.com/2016/08/25/i-dont-like-reduce-i-love-it/
The example can be found in this GitHub Repo –Â https://github.com/brandoncaulfield/youtube-abap/blob/master/z_reduce_example.abap
If you have any questions or anything you’d like to add, please let me know in a comment and I’ll help where I can.
Thanks for reading!
Great stuff Brendan, thanks for this.
I've been a "casual undercover" abap developer for about 10 years, but only recently starting to get into the more functional programming in abap. So it's to get these types of examples.
Keep going!
Thanks Jakob Marius Kjær! Im glad it was of some help. I’m busy with an Advanced ABAP Series on YouTube so if you have any concepts you’d like me to add just let me know!
I loved REDUCE so much that when I was working on a 7.31 system, I implemented it using classes/interfaces.
That’s brilliant Matthew Billingham ! How did you manage that? Might be nice for anyone reading still on 7.31 or below 🙂
I'll see if I can dig it out...
It's a limited reduce, but it suited the functionality I needed at the time! So I have an interface ZIF_CONVERTOR
And I have a static method in a utility class
And you use it like this: