After diving deep in the wonderful world of metaprogramming today, I'm happy to 
share the [loopfusion](https://github.com/numforge/loopfusion) package.

For now you need to install it through `nimble install 
https://github.com/numforge/loopfusion`.

This will give you 2 macros forEach and forEachIndexed that will allow you to 
iterate and operate over any number of sequences of any same type.

Examples:
    
    
    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]
    

Reply via email to