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)
3

1 Answer 1

6

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
})
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.