# How to pass a parameter pack of values to a parameter pack of closures?

**URL:** <https://forums.swift.org/t/how-to-pass-a-parameter-pack-of-values-to-a-parameter-pack-of-closures/73449>\
**Category:** Using Swift\
**Tags:** parameter-packs\
**Created:** [July 25, 2024, 8:06am UTC](https://forums.swift.org/t/how-to-pass-a-parameter-pack-of-values-to-a-parameter-pack-of-closures/73449 "2024-07-25T08:06:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanvoorden](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vanvoorden/32/33619_2.png) [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Post date:** [July 25, 2024, 8:06am UTC](https://forums.swift.org/t/how-to-pass-a-parameter-pack-of-values-to-a-parameter-pack-of-closures/73449/1 "2024-07-25T08:06:43Z")

</div>

Hi! I'm running into a situation with parameter packs where I have some working code… but I am looking for some ways to clean it up (if possible). I am hoping to build this all from production 5.10 Swift.

My situation is I have a parameter pack of some `Input` types. I then have a parameter pack of closures that produce an `Output` for every `Input`. A more advanced use-case might be supporting a unique `Output` type (another parameter pack). For now I can assume every `Input` type produces the same `Output` type.

I am looking for a pattern to "apply" my parameter pack of closures to my parameter pack of inputs. Here are some attempts:

```swift
func f1<each Input, Output>(
  input: repeat each Input,
  transform: repeat (each Input) -> Output
) {
  let _ = (repeat each transform(input))
  let _ = (repeat transform(each input))
  let _ = (transform(repeat each input))
}

```

Those all fail with different errors.

I am unblocked (for now) with this workaround that uses a local helper function:

```swift
func f2<each Input, Output>(
  input: repeat each Input,
  transform: repeat (each Input) -> Output
) {
  func _transform<T>(input: T, transform: (T) -> Output) -> Output {
    transform(input)
  }
  let _ = (repeat _transform(input: each input, transform: each transform))
}

```

This compiles… but I was wondering if there was any way to cut out that extra helper function and perform this work directly.

And then another question I still don't have a great answer for is how might I _return_ that tuple of `Output` values back?

```swift
func f3<each Input, Output>(
  input: repeat each Input,
  transform: repeat (each Input) -> Output
) -> Output {
  func _transform<T>(input: T, transform: (T) -> Output) -> Output {
    transform(input)
  }
  return (repeat _transform(input: each input, transform: each transform))
          `- error: cannot convert return expression of type '(/* shape: each Input */ repeat Output)' to return type 'Output'
}

```

I've looked through [SE-0393](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0393-parameter-packs.md#value-parameter-packs) and I'm not sure I see any way to specify that `each Input repeat Output` type. I am looking for a tuple of `Output` values that matches the dimension of the `Input` parameter pack. Is something like that even possible with 5.10? Any more advice about that? Thanks!

---

<div class="post-metadata">

**Author:** ![Quedlinbug](https://avatars.discourse-cdn.com/v4/letter/q/77aa72/32.png) [@Quedlinbug](https://forums.swift.org/u/Quedlinbug)\
**Post date:** [July 25, 2024, 11:07am UTC](https://forums.swift.org/t/how-to-pass-a-parameter-pack-of-values-to-a-parameter-pack-of-closures/73449/2 "2024-07-25T11:07:37Z")

</div>

[You can't directly represent a homogeneous pack.](https://github.com/swiftlang/swift/pull/70227#issuecomment-2187412271)

Either the output needs to be a pack as well…

```swift
func map<each Input, each Output>(
  input: repeat each Input,
  transform: repeat (each Input) -> each Output
) -> (repeat each Output) {
  (repeat (each transform)(each input))
}

```

Or you need this sort of thing.

```swift
typealias Unpack<Pack, Value> = Value

func map<each Input, Output>(
  input: repeat each Input,
  transform: repeat @escaping (each Input) -> Output
) -> (repeat Unpack<each Input, Output>) {
  (repeat (each transform)(each input))
}

```

I don't know if it's possible to avoid that `@escaping`.

---

<div class="post-metadata">

**Author:** ![vanvoorden](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vanvoorden/32/33619_2.png) [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Post date:** [July 25, 2024, 7:21pm UTC](https://forums.swift.org/t/how-to-pass-a-parameter-pack-of-values-to-a-parameter-pack-of-closures/73449/3 "2024-07-25T19:21:19Z")

</div>

> [@Quedlinbug](#):
>
> `(repeat (each transform)(each input))`

Ahh… I actually think this answers my original question:

```swift
func map<each Input, Output>(
  input: repeat each Input,
  transform: repeat (each Input) -> Output
) {
  let _ = (repeat (each transform)(each input))
}

```

I was looking for the syntax to pass each `input` to each `transform` without an extra local helper function. This one compiles with no errors. Thanks!

---

<div class="post-metadata">

**Author:** ![Quedlinbug](https://avatars.discourse-cdn.com/v4/letter/q/77aa72/32.png) [@Quedlinbug](https://forums.swift.org/u/Quedlinbug)\
**Post date:** [July 25, 2024, 11:37pm UTC](https://forums.swift.org/t/how-to-pass-a-parameter-pack-of-values-to-a-parameter-pack-of-closures/73449/4 "2024-07-25T23:37:34Z")

</div>

What are you going to do with it, though? If you can't access the output, it's not "`transform`"; it's "[`body`](https://developer.apple.com/documentation/swift/sequence/foreach(_:))".

You can easily [iterate over it in Swift 6.0](https://www.swift.org/blog/pack-iteration/), but you can't stuff the result into a tuple.

---

<div class="post-metadata">

**Author:** ![vanvoorden](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vanvoorden/32/33619_2.png) [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Post date:** [July 26, 2024, 7:38pm UTC](https://forums.swift.org/t/how-to-pass-a-parameter-pack-of-values-to-a-parameter-pack-of-closures/73449/5 "2024-07-26T19:38:10Z")

</div>

Ahh… I see. The code compiles but there is nothing I can do with that `each Input repeat Output` type.
