-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sequtils.toSeq produces the sequence from the iterator twice if compiles(iter.len) == true #7187
Comments
@skilchen
Or I haven't figured out what the problem is, sorry. |
@data-man yes it is still actual for the latest Nim. Maybe it becomes clearer what happens, if you compile the following example and then inspect the generated C code: import sequtils
proc test() =
var a = "a,b,c"
let s = toSeq(filter(a) do (x: char) -> bool: x != ',')
echo s
test() In the generated C code in nmcache you will then find an implementation of filter and test procedures both containing the same innermost loop. Maybe a less exotic fix to this issue would be something like this: template toSeq*(iter: untyped): untyped =
when compiles(iter.len):
var i = 0
let t = iter
var result = newSeq[type(iter)](t.len)
for x in t:
result[i] = x
inc i
result
else:
var result: seq[type(iter)] = @[]
for x in iter:
result.add(x)
result Important is the line |
sent out fix here: #8586 |
…rator twice (nim-lang#8586) * cleanups refs nim-lang#8584 * fixes nim-lang#7187
In the doc comment for sequtils.toSeq we find this example:
If you run this, the filtered sequence is produced twice, as can be seen here on the nim playground with my instrumented copies of
filter
andtoSeq
. I think its the same problem that was fixed eg. in mapIt by copying the iterable to a local variable and therefore forcing the iteration to take place.My proposed fix looks like this:
but, as usual, i am not sure if this fix is correct...
The text was updated successfully, but these errors were encountered: