0

Sugar works with map

import sugar 

proc map2*[T, R](list: openArray[T]; cb: proc (x: T): R): seq[R] =
  for v in list: result.add(cb(v))
  result
  
echo @[1, 2].map2((v) => v*v)

But not with each, seems like it somehow related to void, is there a way to make it work with void too?

import sugar 

proc each2*[T](list: openArray[T]; cb: proc (x: T): void): void =
  for v in list: cb(v)

@[1, 2].each2((v) => echo v)
Alex Craft
  • 13,598
  • 11
  • 69
  • 133

1 Answers1

1

I haven't had much luck with sugar.=> personally. This will do what you want, and isn't much more verbose:

proc each2*[T](list: openArray[T]; cb: proc (x: T): void): void =
  for v in list: cb(v)

@[1, 2].each2(proc(v: auto) = echo v)

This RFC is proposing proper lambda support, which should give us a succinct way to write anonymous procs that work everywhere.

dsrw
  • 171
  • 2