{
 "meta": {
  "title": "Lambdas & the Pipeline Operator — The Crush Language Guide",
  "description": "Generated from examples/lambdas.md of The Crush Language Guide (https://docs.crushlang.org/examples/lambdas.html). Edit the guide, not this file: it is regenerated on every build.",
  "frontends": [
   "crush"
  ],
  "author": "The Crush Language Guide",
  "tags": {
   "source": "examples/lambdas.md",
   "guide_url": "https://docs.crushlang.org/examples/lambdas.html",
   "generator": "scripts/build-notebooks.py"
  }
 },
 "cells": [
  {
   "id": "examples-lambdas-md-1",
   "kind": {
    "type": "markdown"
   },
   "source": "# Lambdas & the Pipeline Operator\n\n> Verified against crush-ast `v0.13.0` (`dc0d0278`, 2026-12-03).\n\n## The pipeline operator\n\n`x |> f` calls `f(x)`; `x |> f(y)` calls `f(x, y)`. Chains read left to right and\nbind more loosely than any other operator. The right-hand side must be a function\nname, or a variable holding a function value.\n",
   "state": {
    "status": "done"
   },
   "meta": {},
   "outputs": []
  },
  {
   "id": "examples-lambdas-1",
   "kind": {
    "type": "crush"
   },
   "source": "fn add(a, b) {\n    return a + b\n}\nfn mul(a, b) {\n    return a * b\n}\nfn square(x) {\n    return x * x\n}\n\nprint(add(1, 2) |> mul(3))\nprint(2 |> square |> square)\nlet result = 3 |> add(4) |> mul(10)\nprint(result)\n",
   "state": {
    "status": "pending"
   },
   "meta": {
    "tags": [
     "guide",
     "runnable"
    ],
    "extra": {
     "guide": {
      "src": "examples/lambdas.md:11",
      "browser": "run",
      "expect_output": "9\n16\n70\n"
     }
    }
   },
   "outputs": []
  },
  {
   "id": "examples-lambdas-md-3",
   "kind": {
    "type": "markdown"
   },
   "source": "`add(1, 2) |> mul(3)` is `mul(add(1, 2), 3)`, i.e. `mul(3, 3)` = 9.\n\n## Lambdas\n\nA lambda is `|params| expression` or `|params| { block }`:\n",
   "state": {
    "status": "done"
   },
   "meta": {},
   "outputs": []
  },
  {
   "id": "examples-lambdas-2",
   "kind": {
    "type": "crush"
   },
   "source": "fn main() {\n    let add = |a, b| {\n        return a + b\n    }\n    let mul = |x, y| x * y\n    print(add(1, 2))\n    print(mul(4, 5))\n    print(6 |> |n| n)\n}\n",
   "state": {
    "status": "pending"
   },
   "meta": {
    "tags": [
     "guide",
     "expected-failure"
    ],
    "extra": {
     "guide": {
      "src": "examples/lambdas.md:42",
      "browser": "fail",
      "why": "this example shows a compile error"
     }
    }
   },
   "outputs": []
  },
  {
   "id": "examples-lambdas-md-5",
   "kind": {
    "type": "markdown"
   },
   "source": "That program fails to compile because of its last line: an inline lambda on the\nright of `|>` is a parse error (`Pipeline right side must be function`). Bind it\nfirst:\n",
   "state": {
    "status": "done"
   },
   "meta": {},
   "outputs": []
  },
  {
   "id": "examples-lambdas-3",
   "kind": {
    "type": "crush"
   },
   "source": "let add = |a, b| {\n    return a + b\n}\nlet mul = |x, y| x * y\nlet ident = |n| n\nprint(add(1, 2))\nprint(mul(4, 5))\nprint(6 |> ident)\n",
   "state": {
    "status": "pending"
   },
   "meta": {
    "tags": [
     "guide",
     "runnable"
    ],
    "extra": {
     "guide": {
      "src": "examples/lambdas.md:56",
      "browser": "run",
      "expect_output": "3\n20\n6\n"
     }
    }
   },
   "outputs": []
  },
  {
   "id": "examples-lambdas-md-7",
   "kind": {
    "type": "markdown"
   },
   "source": "## Functions as values, and closures\n\nA function name is a value, and a lambda captures the variables around it:\n",
   "state": {
    "status": "done"
   },
   "meta": {},
   "outputs": []
  },
  {
   "id": "examples-lambdas-4",
   "kind": {
    "type": "crush"
   },
   "source": "fn compose(f, g) {\n    return |x| g(f(x))\n}\nfn make_scaler(k) {\n    return |x| x * k\n}\nlet triple = make_scaler(3)\nlet inc = |x| x + 1\nlet triple_then_inc = compose(triple, inc)\nprint(triple_then_inc(5))\nprint(parallel_map([1, 2, 3], triple))\n",
   "state": {
    "status": "pending"
   },
   "meta": {
    "tags": [
     "guide",
     "runnable"
    ],
    "extra": {
     "guide": {
      "src": "examples/lambdas.md:78",
      "browser": "run",
      "expect_output": "16\n[3, 6, 9]\n"
     }
    }
   },
   "outputs": []
  },
  {
   "id": "examples-lambdas-md-9",
   "kind": {
    "type": "markdown"
   },
   "source": "More in [Closures and Function Values](https://docs.crushlang.org/crush/closures.html).\n",
   "state": {
    "status": "done"
   },
   "meta": {},
   "outputs": []
  }
 ]
}
