title: Swift Combine transform an array of publishers
body: <p>I'm getting my way through Combine bit I don't seem to wrap my head around some trivial operations. I'm building a Combine based to do some HealthKit operations. Mainly I have two operations:</p>
  <ol>
  <li><code>func workouts(_ limit: Int) -&gt; AnyPublisher&lt;[HKWorkout], Error&gt;</code></li>
  <li><code>func workoutDetails(_ workout: HKWorkout) -&gt; AnyPublisher&lt;WorkoutDetails, Error&gt;</code></li>
  </ol>
  <p>First off I will use the first one to query some workouts, that's easy.</p>
  <pre class="lang-swift prettyprint-override"><code>HKHealthStore().workouts(HKObjectQueryNoLimit)
      .sink(receiveCompletion: { subject in
          switch subject {
          
          case .finished:
              break
          case .failure(let error):
              dump(error)
          }
      }, receiveValue: { workouts in
          dump(workouts)
          // What now? 
      }).store(in: &amp;bag)  
  </code></pre>
  <p>Now I'd like to query the second function. It takes a workout as an argument and returns more information about the <code>HKWorkout</code>. How can I query the second function with the returned <code>[HKWorkout]</code> but don't know how to approach this.</p>
  <ul>
  <li>Should I do the query in a nested way in the sink part of the first method?</li>
  <li>Is there a way to nest operations for each of the workout in the <code>[HKWorkout]</code> and call <code>workoutDetails(_ workout: HKWorkout)</code> for each one so I get as a result <code>[WorkoutDetails]</code>?</li>
  </ul>
  <p>What I have attempted so far it's not working.</p>
  <pre class="lang-swift prettyprint-override"><code>HKHealthStore().workouts(HKObjectQueryNoLimit)
      .map({ workouts -&gt; AnyPublisher&lt;[WorkoutDetails], Never&gt; in
          
          return workouts.publisher.flatMap({ workout in
              return Just(workout as! [WorkoutDetails]).eraseToAnyPublisher()
                  .eraseToAnyPublisher()
          })
          .eraseToAnyPublisher()
          
      }).sink(receiveCompletion: { subs in
          switch subs {
          
          }
      }, receiveValue: { val in
          /// Cannot convert value of type 'AnyPublisher&lt;[WorkoutDetails], Never&gt;' to expected argument type 'Void'
          /// I am not able to get [WorkoutDetails] here as I'd like
          dump(val)
      })
      .store(in: &amp;bag)
  </code></pre>
  <hr />
  <p>Edit: Solution I've finally used. There are two options that I've finally managed to make it work and understand it.</p>
  <p>One is @New Dev's answer to finally receive in the <code>sink</code> the combined array and another one is to receive on the <code>sink</code> individual elements to save on an array.</p>
  <pre class="lang-swift prettyprint-override"><code>HKHealthStore().workouts(HKObjectQueryNoLimit)
      .flatMap({
          $0.publisher.eraseToAnyPublisher()
      }).flatMap({
          $0.workoutWithDetails
      }).sink(receiveCompletion: { comp in
          switch comp {
  
          case .finished:
              break
          case .failure(let er):
              dump(er)
          }
      }, receiveValue: { details in
          /// Add the individual `WorkoutDetails` elements to an array 
          results.append(details)
      }).store(in: &amp;SGHealthManager.bag)
  
  </code></pre>