1

Normal reduce call:

[1,2,3].reduce(0, { cur, val in
  return val
})

Attempt at calling reduce from an EnumeratedSequence<Array<Element>>:

    [1,2,3].enumerated().reduce(0, { cur, (index, element) in
      return element
    })
  // Error: consecutive statements on a line must be separated by ';'" (at initial reduce closure)
RobertJoseph
  • 7,968
  • 12
  • 68
  • 113

1 Answers1

5

You can access the element of the tuple with val.element and the index with val.offset:

let result = [1,2,3].enumerated().reduce(0, { cur, val in
    return val.element
})

Alternatively, you can use assignment to access the values in the tuple:

let result = [1,2,3].enumerated().reduce(0, { cur, val in
    let (index, element) = val
    return element
})
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • 2
    It's a slight restriction on Swift that you can't "expand" tuples in a closure's parameter list, which is the underlying problem with the code as per the question – JeremyP Apr 03 '17 at 16:15