Hi,
I've spent the last couple of weeks creating an iterator library. Please take a look and give feedback:
https://github.com/petermora/nimLazy
Thanks, Peter
I completely missed Python's itertools, thanks for mentioning it. I checked Clojure's functions and Rust's iterator library before staring Lazy.
I would like to continue the work on this library. Obviously better documentation is required. Also some functions might need renaming, any advise is appreciated. I really miss a cached copy feature (python calles it "tee()"), the main building block for this is still broken (see https://github.com/Araq/Nim/issues/2766 ). This was the only thing so far I can't solve by myself. I couldn't use a function with variable number of iterators (like varargs[iterator(): T]), I'll report that issue as well (the error is: illegal capture). There is something broken with the iterators generated by templates (for example deepCopy is not deep enough), but I could avoid that pattern with a different approach. I tried generating a function in a template once, of course with {.gensym.}, but it failed somehow at the second call of the template from the same scope. I get warning messages that myFunction.:anonymous() is declared but not used, which is strange, because I have unit tests for these functions. I had some problems matching the "(k,v)" pattern to the iterator's return type. I did all kind of tricks, nothing worked, so I built a macro. When I built the macro then it was strange that I didn't have to add {.inject.}, while {.inject.} would have been necessary in a template. So I'm not sure about the generated code's visibility. I've seen many times C error messages, which shouldn't have happened, I guess.
Apart from these problems, this was an amazing experiment. The language feels just right. Having the "result" variable is an excellent idea! Templates with the injected variables are so convenient. I used type() a lot, and in some cases I needed the type of a several commands' last expression (for example I had to inject the variable first), but with an extra parenthesis it worked. The partition(iter, 2) command returns a tuple of type (T,T), which type is created at compile time with a macro. Writing macro is not as difficult as it sounds. However, it would be nice to see the intermediate code easily (I used getAst a lot), especially that metaprogramming is one of the strongest features of Nim.
Is it possible to have a nicer print of a tuple, for example: (1,"a") instead of (Field0: 1, Field1: "a")?
Thanks, Peter
Is it possible to have a nicer print of a tuple, for example: (1,"a") instead of (Field0: 1, Field1: "a")?
Something like this?
proc `$`*(xs: tuple): string =
result = "("
for x in xs.fields:
if result.len > 1:
result.add(", ")
result.add($x)
result.add(")")
echo((1, "a"))