{
  "id": "article-partial_and_variadic",
  "type": "article",
  "dataComponent": "article",
  "heading": {
    "title": "Partial Application, Placeholders, and the Variadic `$`",
    "badges": ["ARTICLE"]
  },
  "synopsis": "A deep dive into Lava's unique approach to partial application: combining positional placeholders (`_`), the variadic argument (`$`), and flexible arity for concise, compositional scripting.",
  "sections": [
    {
      "type": "heading",
      "level": 2,
      "text": "Partial Application & the Power of Placeholders"
    },
    {
      "type": "paragraph",
      "text": "Lava’s function application is inspired by functional languages, enabling highly composable and concise code. You get full partial application (supplying some arguments now, some later), explicit placeholders (`_`) for skipping arguments, and the special variadic parameter `$` for collecting extra arguments. Understanding these concepts will make you a much more expressive Lava developer."
    },
    {
      "type": "heading",
      "level": 3,
      "text": "The Variadic Parameter: `$`"
    },
    {
      "type": "paragraph",
      "text": "A parameter named `$` in your lambda is a variadic catch-all for extra arguments. If you write `a = (x, $) => $[1]`, then any arguments after the declared ones (here, just `x`) are gathered into the `$` array. This enables functions that accept a variable number of arguments, with full access to all extras in order."
    },
    {
      "type": "codeBlock",
      "code": "# Basic variadic parameter usage\na = (x, $) => $[1]\nb = a(11, 2, 3, _)\n# Here: x=11, $ = [2, 3, _]\n# 'b' is a partial function waiting for one more argument\nslog(b(20))  # prints 3"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Partial Application: Filling Arguments in Stages"
    },
    {
      "type": "paragraph",
      "text": "If you call a function with fewer arguments than it needs (counting positional parameters and `$`), Lava returns a partial—a new function waiting for the rest. You can supply arguments over multiple calls, and Lava will fill them in left-to-right order unless you use placeholders."
    },
    {
      "type": "codeBlock",
      "code": "# Partial application with positional parameters\nadd3 = (x, y, z) => x + y + z\np = add3(1)\nq = p(2)\nr = q(3)\nslog(r)   # prints 6\n# Or just add3(1, 2, 3) == 6"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Placeholders: The Underscore `_`"
    },
    {
      "type": "paragraph",
      "text": "`_` (underscore) acts as a placeholder, meaning “fill this later.” If Lava sees a `_` for any argument in a call, it returns a partial function waiting for that argument, which you can supply in a future call. Placeholders can appear in any argument position."
    },
    {
      "type": "codeBlock",
      "code": "# Placeholder partial application\nsub = (a, b) => a - b\np = sub(_, 10)\nslog(p(20))   # prints 10 (i.e. 20 - 10)\nq = sub(50, _)\nslog(q(8))    # prints 42 (i.e. 50 - 8)"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Combining `$` and Placeholders: Advanced Variadic Partials"
    },
    {
      "type": "paragraph",
      "text": "You can combine variadic `$` and placeholders for very flexible function calls, supplying any or all arguments in multiple stages. Placeholders can appear inside the argument list for `$`, allowing for deferred partials of any arity."
    },
    {
      "type": "codeBlock",
      "code": "# Example: Indexing the 4th argument in a variadic lambda\na = $ => $[3]\nb = a(11, 2, 3, _)\nslog(b(1010))   # prints 1010"
    },
    {
      "type": "codeBlock",
      "code": "# More complex: fixed and variadic with placeholder\na = (x, $) => $[1]\nb = a(11, 2, 3, _)\nslog(b(20))     # prints 3"
    },
    {
      "type": "list",
      "items": [
        "If your lambda has both named parameters and a variadic `$`, any arguments beyond the named parameters go into the `$` array.",
        "If you provide fewer arguments than required (including placeholders), Lava returns a partial function with those missing spots to fill.",
        "Placeholders (`_`) are kept in their call positions, so you can supply values out of order in later calls.",
        "Once all parameters and placeholders are filled, the function executes with all resolved values."
      ]
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Rules & Behavior Summary"
    },
    {
      "type": "list",
      "items": [
        "Use `$` as the last parameter in a lambda to gather extra arguments as an array.",
        "Supplying too few arguments (including `_`) returns a partial, which you can call repeatedly until all slots are filled.",
        "Placeholders work in any parameter position, including within the `$` array.",
        "All arguments (including those in `$`) are resolved in the order provided, unless skipped with a placeholder.",
        "Partials are first-class: you can store, compose, and pass them around until you finally fill all slots.",
        "If you supply more arguments than a function's arity (without `$`), the extras are ignored. With `$`, extras are always gathered into the array."
      ]
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Real-World Examples"
    },
    {
      "type": "codeBlock",
      "code": "# Picking the Nth argument (generalized selector)\nnth = n => $ => $[n]\nslog(nth(2)(10,20,30,40))   # prints 30\np = nth(1)(_, 99)\nslog(p(5))                  # prints 99"
    },
    {
      "type": "codeBlock",
      "code": "# Variadic sum using partials\nsum = $ => array:reduce((a,x)=>a+x, 0, $)\nslog(sum(1,2,3,4,5))  # prints 15\ns = sum(10, 20, _)\nslog(s(30))           # prints 60"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Why This Matters"
    },
    {
      "type": "paragraph",
      "text": "This system lets you write highly reusable, compositional, and dynamic code without boilerplate. Instead of hand-rolling currying, arity checks, or wrapper combinators, Lava's partial application, placeholders, and variadic `$` combine seamlessly for both simple and advanced function patterns."
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Summary Table"
    },
    {
      "type": "list",
      "items": [
        "`a = (x, y) => x + y` — two fixed parameters",
        "`a(1)` returns partial, `a(1,2)` returns 3",
        "`a(_, 2)` returns partial, `a(1)` on that returns 3",
        "`a = (x, $) => $[0]` — one named + variadic",
        "`a(10, 20, 30)` => 20 (x=10, $=[20,30])",
        "`a(10, _, 30)` => partial (waiting for 2nd arg)",
        "`a = $ => $[3]` — picks 4th argument",
        "`a(1,2,3,_)` => partial, then call with a(4)`"
      ]
    }
  ]
}
