DevLog 20250510 Dealing with Lambda

The essence of lambda calculus is with captures - be it simple values or object references, at the site of anonymous function declaration, it's being captured and (the reference) is saved inside the lambda until being invoked. // C# – capturing a local variable in a LINQ query int factor = 3; int[] numbers = { 1, 2, 3, 4, 5 }; var scaled = numbers .Select(n => n * factor) // ‘factor’ is captured from the enclosing scope .ToArray(); Console.WriteLine(string.Join(", ", scaled)); // 3, 6, 9, 12, 15 In C++ this is more explicit and the capturing process is more obvious: // C++20 – capturing a local variable in a ranges pipeline #include #include #include int main() { int factor = 3; std::vector numbers = { 1, 2, 3, 4, 5 }; // capture ‘factor’ by value in the lambda auto scaled = numbers | std::views::transform([factor](int n) { return n * factor; }); for (int x : scaled) std::cout Int -> Int add x y = x + y -- Partially apply 'add' to “capture” the first argument addFive :: Int -> Int addFive = add 5 main :: IO () main = do print (addFive 10) -- 15 print (map (add 3) [1,2,3]) -- [4,5,6]

May 10, 2025 - 16:43
 0
DevLog 20250510 Dealing with Lambda

The essence of lambda calculus is with captures - be it simple values or object references, at the site of anonymous function declaration, it's being captured and (the reference) is saved inside the lambda until being invoked.

// C# – capturing a local variable in a LINQ query
int factor = 3;
int[] numbers = { 1, 2, 3, 4, 5 };
var scaled = numbers
    .Select(n => n * factor)   // ‘factor’ is captured from the enclosing scope
    .ToArray();
Console.WriteLine(string.Join(", ", scaled));  // 3, 6, 9, 12, 15

In C++ this is more explicit and the capturing process is more obvious:

// C++20 – capturing a local variable in a ranges pipeline
#include 
#include 
#include 

int main() {
    int factor = 3;
    std::vector<int> numbers = { 1, 2, 3, 4, 5 };

    // capture ‘factor’ by value in the lambda
    auto scaled = numbers 
        | std::views::transform([factor](int n) { return n * factor; });

    for (int x : scaled)
        std::cout << x << " ";   // 3 6 9 12 15
}

What happens when an object reference is disposed, as in the case of IDisposable? It will simply throw an error.

using System;
using System.IO;

class Program
{
    static void Main()
    {
        // Create and use a MemoryStream
        var ms = new MemoryStream();
        ms.WriteByte(0x42);                // OK: writes a byte

        // Dispose the stream
        ms.Dispose();                      // Unmanaged buffer released, internal flag set

        try
        {
            // Any further operation is invalid
            ms.WriteByte(0x24);            // <-- throws ObjectDisposedException
        }
        catch (ObjectDisposedException ex)
        {
            Console.WriteLine($"Cannot use disposed object: {ex.GetType().Name}");
        }
    }
}

IDisposable capture runtime exception

An important distinction is events or plain callbacks that requires no return value, which can be implemented quite plainly.

Divooka procedural context custom event

To achieve the same thing in dataflow context, some kind of GUI (or "graph-native") support is needed, and to the taker/caller, it's evident (in the language of C#) it's taking a delegate as argument.

Example of

public static System.Collections.Generic.IEnumerable<TResult> Select<TSource,TResult>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,TResult> selector);

To implement capturing, the most natural way is to make sure it happens "in-place" - directly on the graph. With this handy, we do not need to implement specialized nodes for all kinds of functions and instead can rely on existing language construct to do the rest.

Final implementation

Demo Function

The last bit is actually inspired by Haskel, where functions are first class and we can do composition on functions:

-- A simple two‑argument function
add :: Int -> Int -> Int
add x y = x + y

-- Partially apply 'add' to “capture” the first argument
addFive :: Int -> Int
addFive = add 5

main :: IO ()
main = do
  print (addFive 10)         -- 15
  print (map (add 3) [1,2,3]) -- [4,5,6]