Is there a better way to do this:
var input = "1, 2, 3, 4, 5"
var lineNums = input.split(",").map(proc (x:string): string = x.strip()).map(proc (x:string): int = parseInt(x))
I had expected to be able to do something more like this:
var lineNums = input.split(",").map(strip).map(parseInt)
Thanks. That was clear and pointed me to read up on procvar. I suppose, somewhat naively because I haven't investigated the Nim macro system, I could write a macro to wrapper a function with a procvar'd function to do what I want to do. Something that would look like this in use:
var lineNums = input.split(",").map(#strip).map(#parseInt)
where the # symbol is my macro. Thanks again for the quick answer.
This has nothing to do with procvar vs. closure; map expects a closure to begin with and procvars can always be converted to closures, anyway. The problem is that strip has two optional parameters, so it's being treated as a proc(string, bool, bool): string {.closure.} type and map expects a proc(string): string {.closure.} type.
The following works:
import strutils
proc mystrip(x: string): string =
x.strip
var input = "1, 2, 3, 4, 5"
var lineNums = input.split(",").map(mystrip).map(parseInt)
echo lineNums
Note how everything works out of the box for parseInt.
If you add a y: bool = false parameter to mystrip, it breaks.
Ideally, the type inference mechanism should be able to instantiate proc parameters with omitted defaults with a generated proc that instantiates the defaults.