Skip to main content

Lambda expressions

Lambda expression n => n allow you to take a set of collections and apply a method to every member of the collection

This is why it is used in filter() and map() expressions

n => n is broadly equivalent to function(n) {return n} but has an implicit return value and is more compact to type

Generic formโ€‹

image

Using Lambda with mapโ€‹

Use this if you want to perform data manipulation on or extract the values for a particular property

The advantage of map() is that it allows you to handle multiple nodes at once, performing the same defined function for each one โ€“ eliminating the need to create an intermediary property

Here a Lambda expression is used with .map() to calculate the difference between the start date and date of birth for each node in years and then providing the maximum of all nodes to identify the age of the oldest starter in the organization

nodes().map(n => n.startDate.dateDiff(node.dateOfBirth, 'y')).max

image

Without the use of the Map() and n => n this calculation would have required an additional property to have been created to first calculate the age of each employee when they joined the organization and then use an expression to calculate the maximum