loopfusion

Loop efficiently over a variadic number of containers


Keywords
loop, iterator, zip, forEach, variadic
Licenses
MIT/Apache-2.0
Install
nimble install loopfusion

Documentation

Loop Fusion

License: Apache License: MIT Stability: experimental

Iterate efficiently over a variadic number of containers.

  • The loop structure is generated inline at compile-time.
  • There are no temporary allocation.

Status

The containers can be seq of any type. In the future this will be generalized to openarray or even an Iterable concept.

Known limitations

At the moment:

  • all the seqs must contain the same element type.
  • the iteration values cannot be assigned to.

Usage

import loopfusion

let a = @[1, 2, 3]
let b = @[11, 12, 13, 10]
let c = @[10, 10, 10]

forEach [x, y, z], [a, b, c]:
  echo (x + y) * z

forEachIndexed j, [x, y, z], [a, b, c]:
  echo "index: " & $j & ", " & $((x + y) * z)
120
140
160
index: 0, 120
index: 1, 140
index: 2, 160
import loopfusion

let a = @[false, true, false, true, false]
let b = @[1, 2, 3, 4, 5]
let c = @["a: ", "b: ", "c: ", "d: ", "e: "]
var d: seq[int] = @[]

forEachIndexed j, [x, y, z], [a, b, c]:
  if x:
    d.add $(y*y)
  else:
    d.add $y

echo d
@[0, 140, 320]
import loopfusion

let a = @[1, 2, 3]
let b = @[11, 12, 13]
let c = @[10, 10, 10]

let d = @[5, 6, 7]

loopFusion(d,a,b,c):
  let z = b + c
  echo d + a * z
26
50
76