diff --git a/stdlib/public/Differentiation/ArrayDifferentiation.swift b/stdlib/public/Differentiation/ArrayDifferentiation.swift index 42664b81eb3ea..2b98328caf3a7 100644 --- a/stdlib/public/Differentiation/ArrayDifferentiation.swift +++ b/stdlib/public/Differentiation/ArrayDifferentiation.swift @@ -169,6 +169,48 @@ where Element: AdditiveArithmetic & Differentiable { } } +extension Array.DifferentiableView: + BidirectionalCollection, + Collection, + MutableCollection, + RandomAccessCollection, + RangeReplaceableCollection, + Sequence +where Element: Differentiable { + public typealias Element = Array.Element + public typealias Index = Array.Index + public typealias Indices = Array.Indices + public typealias SubSequence = Array.SubSequence + + @inlinable + public subscript(position: Array.Index) -> Element { + _read { yield base[position] } + set { base[position] = newValue } + } + + @inlinable + public subscript(bounds: Range.Index>) -> Self.SubSequence { + _read { yield base[bounds] } + set { base[bounds] = newValue } + } + + @inlinable + public mutating func replaceSubrange( + _ subrange: Range, with newElements: C + ) where C : Collection, Self.Element == C.Element { + self[subrange] = Self.SubSequence(newElements) + } + + @inlinable + public var startIndex: Index { base.startIndex } + + @inlinable + public var endIndex: Index { base.endIndex } + + @inlinable + public init() { self.init(.init()) } +} + /// Makes `Array` differentiable as the product manifold of `Element` /// multiplied with itself `count` times. extension Array: Differentiable where Element: Differentiable {