I couldn't locate a built in method to reverse a sequence, only other Array types.
Did I miss it? If so please point me in the right direction if not...
How would you handle it? Your thoughts on the following proc...
proc seqReverse[T](sqnce: seq[T]): seq[T] = result = @[] for r in countdown(sqnce.len - 1, 0): result.add sqnce[r]
Since you know the length of the sequence you can optimize this by pre-allocating a new sequence of the correct length. It doesn't hurt to use openarray here to accept seqs as well as arrays:
proc reverse[T](xs: openarray[T]): seq[T] =
result = newSeq[T](xs.len)
for i, x in xs:
result[^i-1] = x # or: result[xs.high-i] = x
Sometimes it makes more sense to reverse inplace. You can check the existing implementation for inplace and generating a new seq in the algorithm module: http://nim-lang.org/docs/algorithm.html
To search for stuff in the standard library use the index and ctrl-f: http://nim-lang.org/docs/theindex.html
@def: Thanks! So I did see the openarray version of reverse in the algorithm module but I was unable to get it to work... obviously I was doing something wrong since changing mine to openarray works with both array types with no issues. I'll revisit using the proc from the module to see where I'm off.
I understand the xs.high-i but the ^i-1 has me puzzled, I'll investigate some more. Thinking my start point is that ^ is an overloaded operator for array types ?