Another Day, Another Blog Post 🙂 Darwin with PCSOFT did an excellent Webinar last year covering Map, Filter, and Reduce. I will be honest it broke this very old programmer’s brain a bit when he started using Lambda functions. So now a mere 16 months later I finally “understand” it, and will give my version of an explanation in hopes that it helps other old programmers!
Before we go any further, go watch Darwin’s Webinar. And if you are under 40, that might be all the further you need to go 🙂 His explanations and examples are excellent, just a bit tough for me to get my head around all the new concepts, so for the rest of us let’s continue on.
First, let’s create a structured array and populate some test data
strCart is Structure
Product is string
Quantity is int
isKit is boolean
END
recCart is strCart
arrCart is array of strCart
recCart.Product = "Product 1"
recCart.Quantity = 5
recCart.isKit = True
ArrayAdd(mgrCart.arrRec,recCart)
recCart.Product = "Product 2"
recCart.Quantity = 7
recCart.isKit = True
ArrayAdd(mgrCart.arrRec,recCart)
recCart.Product = "Product 3"
recCart.Quantity = 2
recCart.isKit = False
ArrayAdd(mgrCart.arrRec,recCart)
Now let’s take an easy example that even I could understand. If we need to know the total quantity of items in our cart, we can use the sum function, and tell it to give us a sum of the Quantity member, like so
TotalItems is int = arrCart.Sum("Quantity")
If we run that we would find out that TotalItems is 14. BTW, that is the new .dot syntax that PCSOFT has been introducing in the last couple of releases, I suspect to make developers from some other languages more comfortable with WL. I have started using more and more of it. I like how it makes my variable the first part of the statement, which is the most important part when I am scanning code trying to figure something out.
If you don’t like the .dot syntax you could write it like so and it would still work
TotalItems is int = Sum(arrCart,"Quantity")
I truly feel that the .dot syntax is clearer to read, and the rest of the examples will be in that syntax. If you are really old school, you could write it like this.
TotalItems is int
FOR EACH recCart OF arrCart
TotalItems += recCart.Quantity
END
I assure you that my code up to about 10 minutes ago certainly didn’t look like that. Do you think I am some kind of neanderthal? 🙂
Now say we need to know the quantity of just Products that are kits. Let me introduce you to the Filter function.
arrKits is array of strCart
arrKits = arrCart.Filter(tmpRec => tmpRec.isKit)
We just created a new array with just the records where isKit = True. I could explain that syntax to you, but I would probably be wrong 🙂 , I think it is Lambda, I ain’t even sure! But here’s the ideal. The Filter function loops through the array for each element and calls a function. We could declare an internal procedure and call it but instead, we are using a simple inline way of writing that procedure. Those that come from languages such as JavaScript, JAVA, etc. are probably already familar with this concept.
Filter is looping through each record in the array. tmpRec is the value of the current record of the loop. => is us passing that value to a “procedure”. And finally, tmpRec.isKit is the code of that procedure. Since isKit is boolean it is returning a true or false. This is what our Filter function wants, for each record it wants a true or false, and the trues, get added to our new array. So whatever code, internal procedure, etc. you use it just needs to return a true or false.
Clean as Mud? Good! So now we have an array with just kits, we can apply our previous knowledge to get the total
TotalItems is int = Sum(arrKits,"Quantity")
And sure enough, the answer is 12. Great, but this whole exercise was supposed to be about simplicity. Let me introduce you to sequence. We are able to sequence our functions (Filter, Map, Union, Intersect, Distinct) in one line so that the next function, applies to the previous function. So we can take our Filter statement which produces an array, and do a sum on that array all in one statement. Like so.
TotalItems is int = arrCart.Filter(tmpRec => tmpRec.isKit).Sum("Quantity")
Only one problem, that gives us a compile error.

What the heck I thought I was starting to understand this stuff? Being the expert at the Filter, Sum, and Lambda functions (of all of 10 minutes), I believe that statement should be valid. And I am going to submit to support to see if they can tell me why it isn’t. But never fear there is another function that will help us out.
That is the Map function. The map function again loops through every record of the array, but this time instead of returning an array of records that returned a true. It returns an array containing the result of the function applied to each record. So this statement
arrQuantity is array of int = arrKits.Map(tmpRec => tmpRec.Quantity)
Gives us an array of 2 ints 5 and 7. Now we can add the sum function to the end of that sequence like so
TotalItems is int = arrKits.Map(tmpRec => tmpRec.Quantity).Sum()
And we get our answer of 12. Now let’s put it all together.
TotalItems is int = arrCart.Filter(tmpRec => tmpRec.isKit).Map(tmpRec => tmpRec.Quantity).Sum()
We filter to only kits, reduce it down to just the Quantities, and finally get a Sum of those quantities. Pretty cool, right? Well, actually I am still undecided on that. Be honest with me and yourself, if you are looking at someone else’s code, which are you going to be able to understand quicker the above line or this code?
TotalItems is int
FOR EACH recCart OF arrCart
IF recCart.isKit THEN
TotalItems += recCart.Quantity
END
END
I definitely like the filter one by itself, and we have quite a few places where we loop through and build multiple filtered versions of the same array. I like Sum by itself and will be using it to get the total sum as shown at the start of this post. I might even be able to get behind the version where I just did Filter and Sum, had it worked. However, when you get to the 3rd or 4th level of sequenced statements I think you might be sacrificing readability for cleverness.
As mentioned I will be asking support about the line with just filter and sum and will let you know if I get any enlightenment from that.
=== Update 9/29/2023 ===
It appears support agrees that the version of my statement with just Filter and Sum with the Quantity parameter should be supported and has submitted it to development
Indeed, this syntax is not recognized. A suggestion to this effect has been forwarded to our development team. I sincerely hope that the necessary implementations can be planned quickly.
From Support

I completely agree with you. Lambdas haven’t quite convinced me either. I find the traditional coding approach to be more transparent and easier to grasp. It’s not that I don’t understand lambdas; I just prefer not to employ them unless the task is extremely straightforward, like a single-line IF or SWITCH statement. Only in those cases would I consider using them.
LikeLike