Skip to content

Commit

Permalink
fix nim-lang#14404 foldr had the classic multiple evaluation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed May 21, 2020
1 parent e600ddc commit e026bc9
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,7 @@ template foldl*(sequence, operation, first): untyped =
digits = foldl(numbers, a & (chr(b + ord('0'))), "")
assert digits == "0815"

var result: typeof(first)
result = first
var result: typeof(first) = first
for x in items(sequence):
let
a {.inject.} = result
Expand Down Expand Up @@ -883,11 +882,11 @@ template foldr*(sequence, operation: untyped): untyped =
assert multiplication == 495, "Multiplication is (5*(9*(11)))"
assert concatenation == "nimiscool"

let s = sequence
assert s.len > 0, "Can't fold empty sequences"
var result: typeof(s[0])
result = sequence[s.len - 1]
for i in countdown(s.len - 2, 0):
let s = sequence # xxx inefficient, use {.evalonce.} pending #13750
let n = s.len
assert n > 0, "Can't fold empty sequences"
var result = s[n - 1]
for i in countdown(n - 2, 0):
let
a {.inject.} = s[i]
b {.inject.} = result
Expand Down Expand Up @@ -1436,6 +1435,7 @@ when isMainModule:
assert subtraction == 7, "Subtraction is (5-(9-(11)))"
assert multiplication == 495, "Multiplication is (5*(9*(11)))"
assert concatenation == "nimiscool"
doAssert toSeq(1..3).foldr(a + b) == 6 # issue #14404

block: # mapIt + applyIt test
counter = 0
Expand Down

0 comments on commit e026bc9

Please sign in to comment.