It would be useful if someone could add an example to the manual (or the tutorial) on using the add proc with Sequences. I ended up doing:
result = result & value
in the middle of a loop.
& is available, and is what I used, as per example.
I agree that &= should be available, but it isn't. What the documentation mentions is a "add" proc...but it gives no examples of it, and the intuitive use:
seq.add(item)
failed, though I don't remember the error message. Presumably what I was supposed to do was:
seq = seq.add(item)
but this is a guess based on the presumption that it's an alias for the "&" operator. I just feel it should be documented.
Ah, right. I forgot & worked. But seq.add(item) should work as well.. looking at your code, I assume you just forgot to initialize the result seq before trying to add to it, eg:
result = @[]
result.add(item)
# or..
result = @[item1, item2]
# or..
result = newSeq[T](SIZE)
result[0] = item1
result[1] = item2
There's no example in the docs, but you can use index in such cases, there you can find at least proc's signature (what it takes and what returns), in this case http://nim-lang.org/system.html#add,seq[T],T.
And IMO the proc's name (add) quite intuitively stands for &=, not & (for an imperative language), otherwise it would be named concat or smth.
Hmmm...I didn't save the code that didn't work, and when I substitue "add" into the current code it works as expected, so maybe I just didn't properly initialize the seq. (Or had some other error on the same line. I'm still having trouble understanding the error messages.)
I'd still like an example in the documentation where it talks about the "add" operation. For that matter, the documentation on operators seems generally poor. I'm still not sure whether an exponentiation operator exists, and if it does what variable types it is defined over. (I haven't needed to go searching for this, but I noticed while I was reading the tutorial that it didn't seem defined, and searching found that the word "exponent" only occurs in the context of literals within the manual, and not at all in the tutorials.)
There is pow for floats in the math module: http://nim-lang.org/math.html#pow,float,float
If you're on devel there is also ^ for integers now.
I wrote an article trying to cover all of the behavior for seqs although I missed a few things like concatenation via add:
http://goran.krampe.se/2014/12/03/nim-seq
...I should revisit it to make it more complete. And we should make an effort to flesh out sequtils. Really.
Thanks, http://goran.krampe.se/2014/12/03/nim-seq was a good read.
If I understand correctly string is a seq of bytes, so seq operators should (automatically?) work on string. But I'm coming from Python rather than Smalltalk, and my current search was for a way to do the equivalent of: result = "a" + ("b" * 4) + ("cn")
Based on my reading so far that's going to involve a loop over ("b" * 4). Well, it probably does internally, anyway, particularly as that 4 is a variable rather than a constant, but it sure makes my code look messier. And I've currently spent over a (contact) hour wondering whether the operation is built-in or not, which isn't good. (That "contact hour" is because most of the time I'm dong something else.)
import strutils
var result = "a" & repeatChar(4, 'b') & "cn"
Something like this?
import strutils
proc testStr(a, b: string, n: int): string =
result = a & repeatStr(n, b) & "de"
echo testStr("a", "bc", 4)
proc repeatSeq[T](n: int, q: seq[T]): seq =
result = newSeq[T](n * q.len)
var o = 0
for x in 1..n:
for e in q:
result[o] = e
inc o
proc testSeq(a, b: seq, n: int): seq =
result = a & repeatSeq(n, b) & @['d','e']
echo testSeq(@['a'], @['b', 'c'], 4)
Or didn't I understand what you want to do?