Repository: airbnb/lottie-ios
File: Sources/Private/Experimental/Layers/CALayer+setupLayerHierarchy.swift (Swift)

Code before:
      {
        let maskParentTransformLayer = makeParentTransformLayer(
          childLayerModel: maskLayerModel,
          childLayer: maskLayer,
          name: { parentLayerModel in
            "\(maskLayerModel.name) (mask of \(layerModel.name)) (parent, \(parentLayerModel.name))"
          })

        // Set up a parent container to host both the layer
        // and its mask in the same coordinate space
        let maskContainer = BaseAnimationLayer()
        maskContainer.name = "\(layerModel.name) (parent, masked)"
        maskContainer.addSublayer(parentTransformLayer)
        maskContainer.mask = maskParentTransformLayer
        addSublayer(maskContainer)
      }

      else {
        addSublayer(parentTransformLayer)
      }
    }
  }

}

extension Array where Element == LayerModel {
  /// Pairs each `LayerModel` within this array with
  /// a `LayerModel` to use as its mask, if applicable
  /// based on the layer's `MatteType` configuration.
  ///  - Assumes the layers are sorted in z-axis order.
  var layersAndMasks: [(layer: LayerModel, mask: LayerModel?)] {
    var layersAndMasks = [(layer: LayerModel, mask: LayerModel?)]()
    var unprocessedLayers = self

    while !unprocessedLayers.isEmpty {
      let layer = unprocessedLayers.removeFirst()

      /// If a layer has a `MatteType`, then the next layer will be used as its `mask`
      if
        let matteType = layer.matte,
        matteType != .none,
        !unprocessedLayers.isEmpty
      {
        LottieLogger.shared.assert(
          matteType == .add,
          "The Core Animation rendering engine currently only supports `MatteMode.add`.")

        let maskLayer = unprocessedLayers.removeFirst()
        layersAndMasks.append((layer: layer, mask: maskLayer))
      }


Review comment (suggestion):
If you want to avoid the `Array` conversion above on line 17 I believe you could make this a `Collection` extension
```suggestion
extension Collection where Element == LayerModel {
```

Code after:
      {
        let maskParentTransformLayer = makeParentTransformLayer(
          childLayerModel: maskLayerModel,
          childLayer: maskLayer,
          name: { parentLayerModel in
            "\(maskLayerModel.name) (mask of \(layerModel.name)) (parent, \(parentLayerModel.name))"
          })

        // Set up a parent container to host both the layer
        // and its mask in the same coordinate space
        let maskContainer = BaseAnimationLayer()
        maskContainer.name = "\(layerModel.name) (parent, masked)"
        maskContainer.addSublayer(parentTransformLayer)
        maskContainer.mask = maskParentTransformLayer
        addSublayer(maskContainer)
      }

      else {
        addSublayer(parentTransformLayer)
      }
    }
  }

}

extension Collection where Element == LayerModel {
  /// Pairs each `LayerModel` within this array with
  /// a `LayerModel` to use as its mask, if applicable
  /// based on the layer's `MatteType` configuration.
  ///  - Assumes the layers are sorted in z-axis order.
  fileprivate func pairedLayersAndMasks() -> [(layer: LayerModel, mask: LayerModel?)] {
    var layersAndMasks = [(layer: LayerModel, mask: LayerModel?)]()
    var unprocessedLayers = reversed()

    while let layer = unprocessedLayers.popLast() {
      /// If a layer has a `MatteType`, then the next layer will be used as its `mask`
      if
        let matteType = layer.matte,
        matteType != .none,
        let maskLayer = unprocessedLayers.popLast()
      {
        LottieLogger.shared.assert(
          matteType == .add,
          "The Core Animation rendering engine currently only supports `MatteMode.add`.")

        layersAndMasks.append((layer: layer, mask: maskLayer))
      }

      else {
        layersAndMasks.append((layer: layer, mask: nil))
      }