What are the `@` characters for? Are they what makes it feel like Perl?
Because other than them I don’t think the equivalent Python code would look much different. Maybe more concise, e.g. you could replace the second section with something like `sorted = entries.sorted(key=lambda entry: entry.timestamp)`.
There are shorter options in Nim too, depending on your stylistic preferences
let sorted = entries.sorted(proc (a, b: Entry): int = cmp(a.timestamp, b.timestamp))
let sorted = entries.sorted((a, b) => cmp(a.timestamp, b.timestamp))
let sorted = entries.sortedByIt(it.timestamp)
I suppose you could change the whole proc to something like
proc groupIntoThreads(entries: seq[Entry], threshold: int): seq[seq[Entry]] =
let sorted = entries.sortedByIt(it.timestamp)
for i, entry in sorted:
if i == 0 or entry.timestamp - sorted[i - 1].timestamp > threshold:
result.add(@[sorted[i]])
else:
result[^1].add(sorted[i])
@[] is syntax for a "seq", which is similar to a C++ vector, ArrayList in Java/C#, or a python list. It's a heap-allocated array that automatically resizes.
In contrast with [], which is mostly identical to a C array: fixed size and lives on the stack.
Comments
What are the `@` characters for? Are they what makes it feel like Perl?
Because other than them I don’t think the equivalent Python code would look much different. Maybe more concise, e.g. you could replace the second section with something like `sorted = entries.sorted(key=lambda entry: entry.timestamp)`.
There are shorter options in Nim too, depending on your stylistic preferences
I suppose you could change the whole proc to something like`@` makes the array (stack allocated) into a sequence (heap allocated).
Edit: Just read the second half of your post—
He could be leveraging [std/sugar](https://nim-lang.org/docs/sugar.html) to make this look cleaner.
@[] is syntax for a "seq", which is similar to a C++ vector, ArrayList in Java/C#, or a python list. It's a heap-allocated array that automatically resizes.
In contrast with [], which is mostly identical to a C array: fixed size and lives on the stack.
Yeah, the code doesn't seem very Perl-ish to me.