title: Swift - Cleaner Syntax for Reducing an Array inside Guard
body: <p>I have a UIView driven by two arrays: <code>[Float]</code> "data" and <code>[UIColor]</code> "colors". If the view isn't given proper data I am displaying an empty state, controlled by the guard statement you see here:</p>
  
  <pre><code>private func unwrap() -&gt; ([Float], [UIColor]) {
      guard
          let data = data, !data.isEmpty,
          let colors = colors, data.count == colors.count
          else { return ([1], [UIColor.lightGray]) } // empty state
  
      let total = data.reduce(0) { $0 + $1 }
      if total == 0 { return ([1], [UIColor.lightGray]) }
  
      return (data, colors)
  }
  </code></pre>
  
  <p>I don't like the repetition of the empty state <code>return ([1], [UIColor.lightGray])</code> being used twice. I've tried to add the data.reduce call inside the guard statement, something like: </p>
  
  <pre><code>private func unwrap() -&gt; ([Float], [UIColor]) {
      guard
          let data = data, !data.isEmpty,
          let colors = colors, data.count == colors.count,
          data.reduce(0) { $0 + $1 } != 0
          else { return ([1], [UIColor.lightGray]) } // empty state
  
      return (data, colors)
  }
  </code></pre>
  
  <p>...the compiler doesn't understand the syntax </p>
  
  <pre><code>private func unwrap() -&gt; ([Float], [UIColor]) {
      guard
          let data = data, !data.isEmpty,
          let colors = colors, data.count == colors.count,
          let total = data.reduce(0) { $0 + $1 }, total != 0
          else { return ([1], [UIColor.lightGray]) } // empty state
  
      return (data, colors)
  }
  </code></pre>
  
  <p>...the result of the closure <code>data.reduce(0) { $0 + $1 }</code> isn't optional so can't be included in the guard declaration.</p>
  
  <p>Is there a cleaner way to do this?</p>