JSPM

@konko.oleg/mudria-plugin-loops

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q20823F
  • License MIT

Loop constructs for MUDRIA language - for, while, and iteration patterns

Package Exports

  • @konko.oleg/mudria-plugin-loops
  • @konko.oleg/mudria-plugin-loops/src/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@konko.oleg/mudria-plugin-loops) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@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

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

Usage

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)

range(5)  // [0, 1, 2, 3, 4]

range(start, end, step?)

Generate numbers with custom start, end, and step

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

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

times(3, i → console.log("Iteration", i))

Functional Loops

mudriaWhile(condition, body)

While loop simulation

state: i = 0
mudriaWhile(() → i < 10, () → { console.log(i); i = i + 1 })

mudriaLoop(setup, condition, update, body)

Full loop with break/continue support

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

mudriaMapIndexed([10, 20, 30], (x, i) → x + i)
// [10, 21, 32]

mudriaFilterIndexed(collection, fn)

Filter with index access

mudriaFilterIndexed([1, 2, 3, 4], (x, i) → i % 2 == 0)
// [1, 3]

mudriaReduceIndexed(collection, fn, initial)

Reduce with index access

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

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

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)

mudriaParallelFor([1, 2, 3], async x → {
  await delay(100)
  return x * x
})

Examples

Basic Iteration

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

Nested Loops

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

Comprehensions

∿ 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"