• Kipper

    v0.12.1
  • Downloads

  • Docs

  • Playground

  • Changelog

  • Kipper

    v0.12.1
    • Downloads

    • Docs

    • Playground

    • Changelog

      • next /
      • Expressions /
      • Lambda Expression
    Kipper Docs
    • next
    • latest
    • 0.11.0
    • 0.10.4
    • 0.9.2
    • Overview

    • Quickstart

    • Goals for Kipper

    • Supported platforms

    • Usage Examples

      • Overview

    • Variables

      • Overview

      • String Type

      • Number Type

      • Boolean Type

      • Void Type

      • Null Type

      • Undefined Type

      • Array Type

      • Overview

      • Tangled Expression

      • Arithmetic Expression

      • Assignment Expression

      • Conditional Expressions

      • Logical Expressions

      • Bitwise Expression

      • Relational Expressions

      • Convert Or Cast Expression

      • F-String Expression

      • Member Access Expression

      • Function Call Expression

      • Lambda Expression

      • Matches Expression

      • Overview

      • Expression Statement

      • If Statement

      • While Loop

      • Do-While Loop

      • For Loop

      • Compound Statement

      • Jump Statement

    • Functions

    • Interfaces

    • Classes

    • Comments

    • Built-in Functions

      • Overview

      • Compiler Setup

        • Overview

          • index

          • compiler

          • errors

          • logger

          • utils

          • config

          • index

          • index

        • Overview

        • new

        • run

        • compile

        • help

        • version

      • next /
      • Expressions /
      • Lambda Expression
    • next /
    • Expressions /
    • Lambda Expression

    Edit page

    Lambda Expression

    A lambda expression is an expression creating an anonymous function in memory that can be used as a value. It is a way to define a function without giving it a name, and it can be used in the same way as a named function.

    When creating a lambda expression it will be of type Func<ARGS..., RET>, where ARGS... is a list of arguments the function takes and RET is the return type of the function.

    Syntax

    Expression Lambda Expression

    (ARG1: T1, ...): RET -> EXP

    Compound Lambda Expression

    (ARG1: T1, ...): RET -> {
        // Content
        return EXP;
    }

    Examples

    Assigning an anonymous lambda function to a variable

    var add_func: Func<num, num, num> = (a: num, b: num): num -> {
            return a + b;
    };
    
    var result: num = add_func(2, 3);
    print(result); // -> 5

    Assigning an anonymous lambda function with no arguments to a variable

    var hello_func: Func<void> = (): void -> {
            print("Hello, World!");
    };
    
    hello_func(); // -> "Hello, World!"

    Passing on a lambda function as a function argument

    def forEach(values: Array<num>, func: Func<num, void>) -> void {
        for (var i: num = 0; i < len(values); i++) {
            func(values[i]);
        }
    }
    
    var values: Array<num> = [1, 2, 3, 4, 5];
    forEach(values, (value: num): void -> {
            print(value);
    });

    Using a lambda function in a higher-order function

    def map(values: Array<num>, func: Func<num, num>) -> Array<num> {
        var result: Array<num> = [];
        for (var i: num = 0; i < len(values); i++) {
            result[i] = func(values[i]);
        }
        return result;
    }
    
    var values: Array<num> = [1, 2, 3, 4, 5];
    var squared_values: Array<num> = map(values, (value: num): num -> {
            return value ** 2;
    });
    print(squared_values); // -> [1, 4, 9, 16, 25]
  • Developed at:

    • Releases
    • GitHub
    • Security
    • Issue Tracker
    • License
  • Copyright © 2021-2025 Luna Klatzer, 2024-2025 Lorenz Holzbauer & Fabian Baitura.
    Kipper is licensed under the GPL-3.0-or-later license.