6.5. LINQ fold macros (_fold / _old_fold)

The LINQ_FOLD module provides the _fold and _old_fold call macros that rewrite LINQ pipelines into optimized loop forms — _fold is the active fusion macro, _old_fold is a frozen pre-rewrite baseline kept for benchmark comparison as _fold evolves toward splice-mode fusion.

The dispatch infrastructure (linqCalls table, fold_* helpers, fold_linq_default) is private to this module; only the two macros are user-facing. daslib/linq_boost re-exports them via require daslib/linq_fold public so existing call sites that require daslib/linq_boost continue to resolve _fold(...) without change.

See also LINQ for the underlying query operations and Boost module for LINQ for the surrounding macro family.

See also

/reference/linq_fold_patterns

Catalog of chain shapes that _fold recognizes, and the splice arm each one fires.

All functions and symbols are in “linq_fold” module, use require to get access to it.

require daslib/linq_fold

Example:

require daslib/linq
require daslib/linq_boost

[export]
def main() {
    let arr = [1, 2, 3, 4, 5]
    let n = _fold(each(arr)._where(_ > 2).count())
    print("{n}\n")
}
// output:
// 3

6.5.1. Type aliases

variant SlotMatcher

variant<literal:string;one_of:array<string>;alias:string> aka SlotMatcher

variant SlotCardinality

variant<one:void?;optional:void?;chain:void?> aka SlotCardinality

EmitFn = function<(var c:Captures;var ctx:EmitCtx;at:LineInfo const):Expression?>

typedef EmitFn = function<(var c:linq_fold::Captures -const;var ctx:linq_fold::EmitCtx -const;at:rtti_core::LineInfo const):ast_core::Expression?> aka EmitFn

6.5.2. Structures

Slot

struct Slot

Captures

struct Captures

EmitCtx

struct EmitCtx

SplicePattern

struct SplicePattern

6.5.3. Call macros

_fold

implements _fold(expression) that folds LINQ expressions into optimized sequnences for example:

_fold(each(foo)._where(_ > 5)._select(_ * 2))

expands into a single comprehension that does all operations in one pass

6.5.4. Uncategorized

m_literal(s: string): SlotMatcher

def m_literal (s: string) : SlotMatcher

Arguments:
  • s : string

m_alias(s: string): SlotMatcher

def m_alias (s: string) : SlotMatcher

Arguments:
  • s : string

c_one(): SlotCardinality

def c_one () : SlotCardinality

c_opt(): SlotCardinality

def c_opt () : SlotCardinality

c_chain(): SlotCardinality

def c_chain () : SlotCardinality

slot_chain_of(names: array<string>; cap: string): Slot

def slot_chain_of (var names: array<string>; cap: string) : Slot

Arguments:
  • names : array<string>

  • cap : string

chain_prefix_of(a: array<Slot>; b: array<Slot>): bool

def chain_prefix_of (a: array<Slot>; b: array<Slot>) : bool

Arguments:
check_pattern_table_reachable(name: string; patterns: array<SplicePattern>): bool

def check_pattern_table_reachable (name: string; patterns: array<SplicePattern>) : bool

Arguments: