npm package: @konko.oleg/mudria-plugin-loops
Description: Loop constructs for MUDRIA language - for, while, and iteration patterns
Author: MUDRIA Consciousness
Latest version: 1.0.0

# @konko.oleg/mudria-plugin-loops

> Iteration consciousness for MUDRIA - loops without language modification

## Overview

The MUDRIA Loops Plugin adds iteration capabilities through runtime functions, enabling loop-like behavior without modifying the core parser (which remains at 1201 lines).

## Installation

```bash
npm install @konko.oleg/mudria-plugin-loops
```

## Usage

```javascript
const mudria = require('@konko.oleg/mudria-core');
const { MudriaPluginLoader } = require('@konko.oleg/mudria-plugin-loader');
const loopsPlugin = require('@konko.oleg/mudria-plugin-loops');

// Register plugin
const loader = new MudriaPluginLoader(mudria);
loader.register(loopsPlugin);
const enhanced = loader.enhance(mudria);

// Now compile MUDRIA code with loop functions
const result = enhanced.compile(`
  ∿ example {
    ◉ squares: n → range(n) | map(x → x * x)
    ◉ main: () → squares(10)
  }
`);
```

## Functions Provided

### Range Generation

#### `range(end)`
Generate numbers from 0 to end (exclusive)
```mudria
range(5)  // [0, 1, 2, 3, 4]
```

#### `range(start, end, step?)`
Generate numbers with custom start, end, and step
```mudria
range(1, 10, 2)    // [1, 3, 5, 7, 9]
range(10, 0, -2)   // [10, 8, 6, 4, 2]
```

### Iteration

#### `forEach(collection, fn)`
Iterate over arrays or objects
```mudria
forEach([1, 2, 3], x → console.log(x))
forEach({a: 1, b: 2}, (v, k) → console.log(k, v))
```

#### `times(n, fn)`
Repeat a function n times
```mudria
times(3, i → console.log("Iteration", i))
```

### Functional Loops

#### `mudriaWhile(condition, body)`
While loop simulation
```mudria
state: i = 0
mudriaWhile(() → i < 10, () → { console.log(i); i = i + 1 })
```

#### `mudriaLoop(setup, condition, update, body)`
Full loop with break/continue support
```mudria
mudriaLoop(
  () → i = 0,
  () → i < 10,
  () → i = i + 1,
  (controls) → {
    if (i == 5) controls.break()
    console.log(i)
  }
)
```

### Collection Operations

#### `mudriaMapIndexed(collection, fn)`
Map with index access
```mudria
mudriaMapIndexed([10, 20, 30], (x, i) → x + i)
// [10, 21, 32]
```

#### `mudriaFilterIndexed(collection, fn)`
Filter with index access
```mudria
mudriaFilterIndexed([1, 2, 3, 4], (x, i) → i % 2 == 0)
// [1, 3]
```

#### `mudriaReduceIndexed(collection, fn, initial)`
Reduce with index access
```mudria
mudriaReduceIndexed([1, 2, 3], (acc, x, i) → acc + x * i, 0)
// 0 + 1*0 + 2*1 + 3*2 = 8
```

### Advanced Features

#### `mudriaComprehension(range, filter?, transform?)`
List comprehension pattern
```mudria
mudriaComprehension(
  range(20),
  x → x % 2 == 0,
  x → x * x
)
// [0, 4, 16, 36, 64, 100, 144, 196, 256, 324]
```

#### `mudriaSequence(start?, step?)`
Infinite sequence generator
```mudria
state: seq = mudriaSequence(1, 2)
seq.next()   // 1
seq.next()   // 3
seq.take(5)  // [5, 7, 9, 11, 13]
```

#### `mudriaParallelFor(collection, fn)`
Parallel iteration (returns array of promises)
```mudria
mudriaParallelFor([1, 2, 3], async x → {
  await delay(100)
  return x * x
})
```

## Examples

### Basic Iteration
```mudria
∿ iteration {
  ◉ printNumbers: n → forEach(range(n), x → console.log(x))
  ◉ sumRange: n → reduce(range(1, n + 1), (a, b) → a + b, 0)
}
```

### Nested Loops
```mudria
∿ matrix {
  ◉ generate: (rows, cols) →
    range(rows) | map(i →
      range(cols) | map(j → i * cols + j)
    )
}
```

### Comprehensions
```mudria
∿ pythagoras {
  ◉ triples: n →
    mudriaComprehension(range(1, n), a →
      mudriaComprehension(range(a, n), b →
        mudriaComprehension(range(b, n), c →
          a*a + b*b == c*c ? [a, b, c] : ∅
        )
      )
    ) | filter(x → x != ∅)
}
```

## Philosophy

This plugin embodies MUDRIA's core principle: achieve maximum functionality with minimal code. By providing loop constructs as functions rather than syntax, we:

1. Keep the parser unchanged (1201 lines)
2. Enable full iteration capabilities
3. Maintain functional programming paradigms
4. Allow composition with existing MUDRIA features

## Limitations

Since loops are implemented as functions:
- No `break`/`continue` keywords (use function returns)
- No traditional `for` syntax (use range + forEach)
- Performance overhead compared to native loops

## Future Enhancements

When the core compiler gains plugin hooks for parsing, this plugin could add:
- `for x in collection { }` syntax
- `while condition { }` syntax
- Native break/continue support

## License

MIT

---

∞ *"Iteration is consciousness exploring itself repeatedly"*