Well, initially it is not JS. I'm working on our internal compiler's Nim 
backend.

Original function source (ML-like language): 
    
    
    applyAllSync(funcs: [(() -> void) -> void], onAllDone: () -> void) -> void {
            fold(funcs, \onDoneNothing -> onDoneNothing,
                    \doBefore : (() -> void) -> () -> void, func : (() -> void) 
-> void -> {
                            \onDone : () -> void-> {
                                    doBefore(\ -> func(onDone))
                            }
                    }
            )(onAllDone)();
    }
    

We have first-class citizen functions, so we can treat them as usual objects, 
which can be returned and used. The syntax here is simple: 
    
    
    () -> void

is like 
    
    
    proc(): void

in parameters 
    
    
     \name -> void

is like 
    
    
    proc(name: T): void

, used as lambda (T might be changed to any, depends on context), 
    
    
     [] 

is an array.

Direct conversion with types gives next Nim code: 
    
    
    proc applyAllSync(funcs: seq[proc(t0: proc(): void): void], onAllDone: 
proc(): void): void =.
      fold(funcs, proc(onDoneNothing: any) =
          onDoneNothing, proc(doBefore: (proc(t0: proc(): void): proc(): void), 
func: (proc(t0: proc(): void): void)) =
          proc(onDone: (proc(): void)) =
              return doBefore(proc() =
                  func(onDone)))(onAllDone)()
    

It doesn't compile, error is 
    
    
    test.nim(3, 77) Error: ')' expected
    

Position 77 is a letter f in word func at line 3.

I showed Javascript example previously, because it works. I understand JS type 
system and it was just to show syntax.

Again, my question is 'How syntactically nested lambdas and/or closures can be 
written?'

Reply via email to