Advanced Programming Techniques - Lambda Expressions Explained (Gizmo)

Lambda expression allows you to take in 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 equivalent to ‘function(n){n}’ but faster to type and able to be nested within other functions. It also decreases the evaluation time of expressions and minimises the performance burden on your dataset.

Generic Format

[input].[method](item=>[rule for item])

Note the symbol can be anything, but it is good practice to represent the type of input: e.g. “n” for nodes, “p” for properties

e.g. view filtered list containing only non-generated properties

view.properties.filter(p=>!p.isgenerated)

[input] is any collection. Typically, the collection you are working with is nodes() or allnodes() (the unfiltered list of nodes). But another collection would be view.properties (this list of properties within this dataset). You can also create new collections - or example array(1,2,3,4,5) is collection of numbers from 1 to 5.

[method] is the operation you want to perform on the collection. It is more properly called a function, not method. There are several types of function - for example: map, filter, sort, groupBy take arguments.

When a function takes arguments, these are within the parenthesis (brackets). What arguments are required is dependent of course on the function being called, but commonly there will be a single argument which is a lambda function.

A lambda function is an anonymous function (a function with no name) - it performs some transformation on the data passed to it. The function is applied to each member of the collection in turn and the returned values are passed back in a new collection.

You can think of this as passing each item through a pipe, and within that pipe the item passed has been turned into something else by the time it exits the pipe.   In the case of map, the same number of items passed into the pipe come out the other side of the pipe, but in the case of filter some of the items are swallowed up in the pipe whilst others make it through.

nodes().map(n=>n.department)

Each node in the collection of nodes is passed through map, and map applies the anonymous function n=>n.department to the node and passes it into a new collection.

A lambda function is just a shortcut for creating an anonymous function in a less verbose way:

n=>n.department

is the same as

function(n) { return n.department }

Of course, returning the list of departments for all nodes is such a common case that Gizmo provides more convenient ways of doing it:

nodes().department

is a shortcut to the map form above.

However, what if I want to run some condition dependent upon department? Then I need the map form:

nodes().department.map( n => n.department == "Sales" ? "Sales" : "Not Sales"  )

returns a collection of strings Sales or Not Sales

Sometimes we want to provide a function that reduces the collection to a single value. This is called a reducer. The most obvious example is count:

nodes().count

returns a single integer value - the count of nodes

nodes().birthday.age().avg

is a more complex example:

  • nodes().birthday returns a collection of dates
  • nodes().birthday.age() applies the age() function to each date in turn, returning a collection of numbers. age() has the brackets after it as you can ask for the age in different units, which by default are 'y' or year.
  • nodes().birthday.age().avg takes this collection of numbers and reduces it to a single number - the average of the ages. avg is formally known as a reduce function. It takes no parameters, and therefore we give it no brackets.

Further information on Gizmo expressions can be found in the Gizmo Cookbook

This article was authored by Chris Barrett, Head of Product Design at Concentra

 

Have more questions? Submit a request

Comments