Skip to content

Commit

Permalink
foldl: support openArray
Browse files Browse the repository at this point in the history
  • Loading branch information
foldl authored Jan 20, 2025
1 parent 6481482 commit a3b256d
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ template foldl*(sequence, operation: untyped): untyped =
multiplication = foldl(numbers, a * b)
words = @["nim", "is", "cool"]
concatenation = foldl(words, a & b)
procs = @["proc", "Is", "Also", "Fine"]
procs = ["proc", "Is", "Also", "Fine"]


func foo(acc, cur: string): string =
Expand All @@ -920,14 +920,13 @@ template foldl*(sequence, operation: untyped): untyped =
assert concatenation == "nimiscool"
assert foldl(procs, foo(a, b)) == "procIsAlsoFine"

let s = sequence
assert s.len > 0, "Can't fold empty sequences"
var result: typeof(s[0])
result = s[0]
for i in 1..<s.len:
assert sequence.len > 0, "Can't fold empty sequences"
var result: typeof(sequence[0])
result = sequence[0]
for i in 1..<sequence.len:
let
a {.inject.} = result
b {.inject.} = s[i]
b {.inject.} = sequence[i]
result = operation
result

Expand Down

0 comments on commit a3b256d

Please sign in to comment.