CollectionExtensions.swift 781 B

1234567891011121314151617181920212223242526
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // This source file is part of the Swift Argument Parser open source project
  4. //
  5. // Copyright (c) 2021 Apple Inc. and the Swift project authors
  6. // Licensed under Apache License v2.0 with Runtime Library Exception
  7. //
  8. // See https://swift.org/LICENSE.txt for license information
  9. //
  10. //===----------------------------------------------------------------------===//
  11. extension Collection {
  12. func mapEmpty(_ replacement: () -> Self) -> Self {
  13. isEmpty ? replacement() : self
  14. }
  15. }
  16. extension MutableCollection {
  17. mutating func withEach(_ body: (inout Element) throws -> Void) rethrows {
  18. var i = startIndex
  19. while i < endIndex {
  20. try body(&self[i])
  21. formIndex(after: &i)
  22. }
  23. }
  24. }