Hi,

Have you taken a look at the Nim manual ? It has a chapter on Closure 
<https://nim-lang.org/docs/manual.html#procedures-closures>.

Personnaly, my favorite "most idiomatic" way of dealing with closure in Nim is 
through the sugar module : <https://nim-lang.org/docs/sugar.html>

This allows you to write closure using `=>` : For example (taken shamelessly 
from the documentation) :
    
    
    proc passTwoAndTwo(f: (int, int) -> int): int =
      f(2, 2)
    
    passTwoAndTwo((x, y) => x + y)
    
    
    Run

I find it especially useful when working with `seq`, you can do stuff like this 
: 
    
    
    import sugar
    import sequtils
    
    var myseq = @[1, 2, 3, 4, 5, 6]
    myseq.apply(x => x+1)
    echo myseq
    # Dividing a seq[int]will only give us @[0, 0, 0, ...]
    # Using map(x => ...) instead of appyl create another seq
    var otherseq = myseq.map(x => x/12)
    echo otherseq
    echo type(myseq)
    echo type(otherseq)
    
    
    Run

Reply via email to