Lamba functions in Lucid allow you to apply a specific function to a list or array of information, effectively looping the function. This video demonstrates two examples using Lucid's "filter" formula in this way - filtering a list of numbers to identify which ones are even, and filtering employees in an org chart to count those who are based in a certain region.
Full transcript here 👇
formula system which if you come in and check out our documentation you can take a look at. Um one thing that we sometimes have uh that people have questions on are lambdas which are you know some of our functions filters map for example that kind of thing. Um we actually do have this thing called lambdas which can be used to actually help people basically loop through you know um often a list um or to better use a function like filter or map which can be otherwise kind of tricky.
So let's talk about lambdas. What are they? If you don't like my gif joke here then sorry you don't have to watch this video. Um okay so what are lambdas? Well, um, for those who aren't familiar, they they come from something, um, like JavaScript, a coding language, where basically they become this easier way to loop through a list and and to talk about functions and to tell the formula what you want it to do. So, let's say I have a list 1 2 3 4 5 6 7 8 and I want to filter my list. And specifically, I want to return only numbers. Well, I would write it something like this in Lucid formula syntax. And this is my lambda where I'm saying for my filter take in this list of things or array of things and for every item in the list we're just going to call it x uh check is it even uh and so take this is even function run it on x and it should return true or false. The little arrow is basically our indicator of the the function or the the little formula that we're going to run on each item x. And what we would get back is 2, four, six, eight. Yeah. Okay. Just double checking that before I say something dumb about even numbers. Um, so basically x is each item. As we loop through this list or array, we check is it even? And if so, we're going to keep it. Otherwise, we'll toss it and we'll end up with only our final list of numbers.
So let's actually just kind of run through this exercise here uh in a more visual way so that you can see this. If this is our list, we're running our function here, the filter x, returning only if it is even. Well, let's start with one, which is our first x. And we're going to check is x even. Nope. So, we're going to ignore it. Two, uh, is now x. Is it even? Yep. So, we are going to keep it. Three, is it even? Nope. So, we are going to toss it. And so on so forth. Four. Even? Yes. Five. Uh, as our next x, not even. Toss it. Six. Absolutely even. Let's keep it. Seven. Nope. Go away. Eight. Uh, is even? Yes. So, here we end up with our final list of things to keep as a result of our filter. Uh, which is 2 4 6 8. So, that's effectively what's happening. um in a more visual way for anyone confused about what this little lambda does. Um there is a lot more on the syntax in here including how you might choose to filter um or or nest the lambdas I should say. Um but the basic syntax is always you're going to provide something. I typically use x um as the documentation states. This could really be any sort of like little um you know variable name. You could use X, you could use Y, Z, you could use um, you know, something more significant like numbum. And then it would look like um, you know, numbum is even numbum. And you're just going to run it on whatever you've used to alias for whatever item we are currently on in the list. As we run through it, this is the test to run on it.
So, let's take a quick look at that in action. Let's say I had a data field here in Lucid Chart. Um, I'll just call it list. And we'll do two or sorry, one, two, three, four, five, six, 7, 8. There's my list. Um, and actually, I'm going to turn it into a little list. Um, specifically by going ahead and putting it in to the brackets so that it's truly this listed array. Um, and I'm actually going to put my same function in here. So I'm going to say filter going to say at list and I'm going to put in my lambda. So for every single x I want to check is even which I need to spell right uh x. And when I close that formula you'll notice that I get my list of evens. We could do a similar one if I wanted maybe odds. I could do the same thing. I could say it's, you know, filter at list and I want my test to be for every item, uh, I want to return is odd x. Uh, and that's going to give me 1 357. So that's how that would work.
So what would we actually use this for? Like what what's something that I would care about in lucid chart? Well, I'm going to do an example here with an org chart. Now every single employee here um actually has a data field called location in this case. So maybe something that I want to do is I want to pull a list of how many um employees there are in each location. So uh Emia uh if I want to do that I'm going to say in this case uh descendants of page that's a specific formula we use to go get every single shape on the page even if it's inside something like an org chart. Um, and maybe I want to specifically pull these these people. So, I'm going to filter them. Uh, I'm going to pass in the descendants. That's the equivalent of my list. And I'm going to say only return them if their location is equal to Emia. So, X for every single descendant of the page. Uh, I want to return it if x dot location equals Emia. And what I'm going to get is this list of people who are all just named new employee as it happens. Uh, let's give them real names. So, this will be Ron, uh, Hermione, and Harry. And we'll say that, uh, maybe Neville and, uh, Malfoy work in Apac.
So now if I come back here, you'll notice that it's actually saying, you know, Harry, Hermione, Ron. Um, it is saying AMIA after them because it's actually displaying the text. That's what's being displayed here. Um, but if I wanted to, maybe I maybe what I really want is the count. So I could actually then wrap this further in a count and I get that there are three AMIA employees. Let's do the same thing for APEC real quick. Um, I would do this exact same function. I'm actually just going to copy paste it, but I'm going to say instead I want the location to equal APAC. That's going to give me two. Um, and maybe I want to do the USA as well. Going to come in and do the same thing. And I just happen to call it US. We'll get a three.
Um, and technically I could even then maybe display this on the page. So if I wanted to, uh, I could actually have this, uh, choose to, uh, display this on a in a text box. I could either add data fields where I actually collect and write down um you know pageia page us since that's where I was writing all of this or a nice easy way to do it is with interpolated text. So I might say equals page domeia. What this is going to do is it's going to insert that formula for me and I could then add a label employees three. Uh, I could do the exact same thing for my other fields here. So, I had APEC. Uh, and since we're US based company, maybe we'll put US at the top. Uh, and I can then just edit those little formulas in the interpolated text. And I I called them, you know, USA was the name of my formula and um, APAC was the name of my formula. Um, so I can actually choose. You got to hit enter actually. So, none of those changed. There we go. Um, same with the US one. There we go. Uh, so let's say I now start changing my org chart. Uh, let's add someone else who's in the USA. Uh, we'll say it's double door. Um, and what you'll notice is, uh, oh wait, I've got to say US, not USA. It is sensitive to that. Um, USA employees is now ticking up to four. And if I made further changes to my org chart, this would be displayed.
So that's an example of a time where I might actually want to use a filter and a lambda in order to create sort of a display here for myself. Um it would probably look a lot nicer um to be able to show something like USA AMIA and APAC employee. Just to finish this up, here's an actually kind of prettier example of that. Uh, I'm actually displaying, you know, location Seattle vers San Francisco using a dynamic shape on an org chart here. If I were to shift people around, this would change because our filter, which has a lambda inside, actually doing the checking, um, would actually give us that nice little breakdown that we can then use to show, hey, here's where our breakdown is by location on an orth chart.