Lambda Functions

reviewed: 23 February 2025

Introduction

The best way to describe a lambda function is to look at it as a special type of object that represents a function.

Lambda function, by definition, do not have a name and only become useful when you assign a lambda function to a variable or parameter.

The beauty of lambda functions is that it allow you to treat functions just like any other variable or parameter value.

This probably still sounds abstract and it is all best explained by some examples.

Sort a Property Bag

The pb.sort() function can be used to sort the entries of a property bag. It is a highly generic function and obviously does not want to prescribe what order means.

It takes as the 2nd parameter a lambda function where you specify how two items from the property bag should be compared.

This is demonstrated in the following example:

function doTest()

    set(
        'pb',
        <
            'Paris'
            'Amsterdam'
            'Rome'
            'Brussels'
            'Berlin'
            'Kopenhagen'
        >
    )

    response.write( 'Before<br>')
    response.write( pb.dump( [pb] ) )
    response.write( '<br>')

    pb.sort(
        [pb],
        lambda.create(
            function( a, b )
                if eq( [a], [b] )
                    return 0
                else-if lt( [a], [b] )
                    return -1
                else
                    return 1
                end-if
            end-function
        )
    )

    response.write( 'After<br>')
    response.write( pb.dump( [pb] ) )
    response.write( '<br>')

    // Output
    // Before
    // <"Paris", "Amsterdam", "Rome", "Brussels", "Berlin", "Kopenhagen">
    // After
    // <"Amsterdam", "Berlin", "Brussels", "Kopenhagen", "Paris", "Rome">

end-function

Note the 2nd parameter to the pb.sort function where we use the lambda.create function to create a lambda function.

We know that the lambda function for the pb.sort function takes two parameters.; two items that need to be compared with one another.

The function should return -1, 0 or 1:

  • -1 When the first parameter is smaller than the second
  • 1 When the first parameter is greater than the second
  • 0 When the first and second parameter are equal

Note that we could also have assigned the lambda function to a variable and pass that to pb.sort.

    set( 
        'lambda',
        lambda.create(
            function( a, b )
                if eq( [a], [b] )
                    return 0
                else-if lt( [a], [b] )
                    return -1
                else
                    return 1
                end-if
            end-function
        )
    )
    pb.sort( [pb], [lambda] )

Lambda invoke

The pb.sort function will execute the lambda function several times until the property bag is sorted.

This is done using the lambda.invoke function that takes a lambda function as first parameter and as many parameters as expected by the lambda function.

This is demonstrated in the following example:

function doTest()

    set( 'lambda', lambda.create( function( s ) return ucase( [s] ) end-function ) )

    response.write( concat( page.call( './doSomething', 'hello world', [lambda] ), '<p>' ) )
    response.write( concat( page.call( './doSomething', 'hello moon', [lambda] ), '<p>' ) )

    // Output
    // HELLO WORLD
    // HELLO MOON
end-function

function doSomething( someValue, someLambda )
    return lambda.invoke( [someLambda], [someValue] )
end-function

<End of document>