As an exercise I just ported the Firebase push id algorithm to Nim. As I just started writing code in Nim I would be happy if somebody with more experience would look over it so that the code is not to bad :)
Gist with my port: https://gist.github.com/oderwat/04b8c8f586132909e8f0
Related article: https://www.firebase.com/blog/2015-02-11-firebase-unique-identifiers.html (links to a gist with the original code and more ports)
If you want performance it's important to allocate the string once with newString(20) instead of adding to it all the time. Also, strutils.repeatChar is not a good way to initialize a string, and you can just use result instead of an intemediate variable. Apart from this I cleaned up some other things but that may be a matter of taste. Looks a bit cleaner to me and runs 4 times faster for me: https://gist.github.com/def-/e473d087445344c03629
Diff to see what I changed: https://gist.github.com/def-/e473d087445344c03629/revisions
Edit: I guess the proc-inside-proc is inspired by my brainfuck example. I only used it there because the inner proc calls itself recursively all the time and I didn't want to pass around references, but still allow multiple brainfucks to be run in separate threads. This is not necessary for this code, so I would just use a regular proc.
Edit2: If you want another 2x speedup, change the return type of generatePushID to a fixed size char array with array[21, char] (not as nice to use, \0 at end for easier echoing)
Thank you for the reply def!
You may laugh but I actually was looking for "creating a new string with a fixed length" and simply did not thought of newSting(x) :(
I also hat the problem that I tried to use something in the spirit of newSeqchar but could not find a way to return this as string. I wanted something which is similar to that and used the strings.
The inside proc is not inspired by the brainfuck example but because I wanted to keeping the state variables out of the global code and thought it would work without the global pragmas. I was going to create generator (returning a "proc") such that you could have multiple of the generators. Thinking of that I found that this is stupid and just added the static pragmas.
Then I tried to get the randomize() inside the proc. In the end I gave up and didn't notice that I can put those globals inside the inner proc, removing the outer altogether without loosing access limitations and encapsulation.
EDIT: I updated my code a bit (just picking up thoughts)
EDIT2:
The ".mitems" Iterator is pretty cool! I also forgot that the "for a, b" triggers the pairs automatically.
I do not like to spill the vars to the global scope. But if it would be a module that should be done. I am unsure about what is best for a "code snippet", I like my "encapsulation" there.
I would like it more if I could write something like "static var = .." instead of that pragma but I could not find that for Nim.
I do not like to return the char array for this code. It may make sense in a real application though.