150 Swift Productivity Methods To Use

Yasir
11 min readJun 6, 2023

It’s a collection of 150 swift functional methods with a short description about them.

  1. map: Transforms each element in a collection and returns an array containing the results.
  2. flatMap (or compactMap in Swift 5.0 and later): Transforms each element in a collection and flattens the results into a single-level array.
  3. filter: Returns a new array containing only the elements that satisfy a given condition.
  4. reduce: Combines all elements in a collection into a single value by applying a combining closure.
  5. forEach: Performs a given operation on each element in a collection.
  6. sort (or sorted): Returns a new array with the elements sorted according to a given closure.
  7. contains: Checks if a collection contains an element that satisfies a given condition.
  8. allSatisfy: Checks if all elements in a collection satisfy a given condition.
  9. anySatisfy: Checks if any element in a collection satisfies a given condition.
  10. first(where:): Returns the first element in a collection that satisfies a given condition.
  11. compactMap (or flatMap in Swift 4.2 and earlier): Transforms each element in a collection and removes any nil values from the results.
  12. split: Splits a collection into multiple subcollections based on a given condition.
  13. joined: Returns a string by concatenating the elements of a collection into a single string.
  14. prefix: Returns a subcollection containing the specified number of elements from the start of the collection.
  15. suffix: Returns a subcollection containing the specified number of elements from the end of the collection.
  16. drop: Returns a subcollection by omitting the specified number of elements from the start of the collection.
  17. compactMapValues: Transforms the values of a dictionary and removes any nil values from the results.
  18. Certainly! Here are a few more higher-order functions available in Swift for working with collections:
  19. drop(while:): Returns a subcollection by omitting elements from the start of the collection until a condition is no longer satisfied.
  20. prefix(while:): Returns a subcollection by taking elements from the start of the collection until a condition is no longer satisfied.
  21. enumerated: Returns a sequence of pairs (index, element) for each element in a collection.
  22. reversed: Returns a new collection with the elements in reverse order.
  23. min(by:): Returns the minimum element in a collection based on a given closure.
  24. max(by:): Returns the maximum element in a collection based on a given closure.
  25. shuffle: Returns a new collection with the elements shuffled randomly.
  26. partition(by:): Reorders the elements of a collection so that all elements satisfying a given condition are true to the left of those that don't.
  27. contains(where:): Checks if a collection contains an element that satisfies a given condition.
  28. firstIndex(where:): Returns the index of the first element that satisfies a given condition.
  29. lastIndex(where:): Returns the index of the last element that satisfies a given condition.
  30. reduce(into:_:): Combines all elements in a collection into a single value by applying a closure that can mutate an accumulator.
  31. forEachIndex: Performs a given operation on each element in a collection along with its corresponding index.
  32. zip: Creates a sequence of pairs by combining elements from two collections.
  33. compactMapUnique: Transforms elements in a collection and removes any duplicate values from the results.
  34. reduce(intoResult:_:): Combines all elements in a collection into a single result value by applying a closure that can mutate the result.
  35. indices: Returns a range of all the valid indices for a collection.
  36. first: Returns the first element in a collection, or nil if the collection is empty.
  37. last: Returns the last element in a collection, or nil if the collection is empty.
  38. first(where:): Returns the first element in a collection that satisfies a given condition.
  39. last(where:): Returns the last element in a collection that satisfies a given condition.
  40. single(where:): Returns the single element in a collection that satisfies a given condition, or nil if there are zero or more than one such element.
  41. dropLast: Returns a subcollection by omitting the specified number of elements from the end of the collection.
  42. prefix(upTo:): Returns a subcollection containing all the elements from the start of the collection up to, but not including, the specified index.
  43. suffix(from:): Returns a subcollection containing all the elements from the specified index to the end of the collection.
  44. reduce(into:combine:): Combines all elements in a collection into a single value using a closure that can mutate an accumulator.
  45. reduce(_:combine:): Combines all elements in a collection into a single value using a closure.
  46. forEach(_:do:): Performs a given operation on each element in a collection while passing in the index of each element.
  47. contains(_:equatable:): Checks if a collection contains a specific element based on equatability.
  48. allSatisfy(_:predicate:): Checks if all elements in a collection satisfy a given predicate.
  49. partition(_:isIncluded:): Reorders the elements of a collection based on a predicate, so that all elements satisfying the predicate come before the ones that don't.
  50. firstIndex(of:): Returns the index of the first occurrence of a given element in a collection, or nil if the element is not found.
  51. lastIndex(of:): Returns the index of the last occurrence of a given element in a collection, or nil if the element is not found.
  52. dropFirst: Returns a subcollection by omitting the specified number of elements from the start of the collection.
  53. prefix(through:): Returns a subcollection containing all the elements from the start of the collection up to and including the specified index.
  54. suffix(through:): Returns a subcollection containing all the elements from the specified index to the end of the collection, inclusive.
  55. partitioned(by:): Splits a collection into two subcollections based on a given predicate, with elements satisfying the predicate in one subcollection and the rest in the other.
  56. min(): Returns the minimum element in a collection of comparable elements.
  57. max(): Returns the maximum element in a collection of comparable elements.
  58. reversed(): Returns a new collection with the elements in reverse order.
  59. allSatisfy(_:contains:): Checks if all elements in a collection satisfy a given condition by using the contains function.
  60. forEach(_:index:do:): Performs a given operation on each element in a collection while passing in both the element and its index.
  61. firstIndex(of:where:): Returns the index of the first occurrence of a given element that satisfies a given condition in a collection, or nil if the element is not found.
  62. lastIndex(of:where:): Returns the index of the last occurrence of a given element that satisfies a given condition in a collection, or nil if the element is not found.
  63. split(separator:maxSplits:omittingEmptySubsequences:): Splits a collection into subcollections based on a separator element and a maximum number of splits, optionally omitting empty subsequences.
  64. joined(separator:): Returns a string by concatenating the elements of a collection into a single string, separated by a given separator.
  65. lazy: Creates a lazy sequence that defers the execution of transformations until they are actually needed.
  66. firstIndex(_:in:): Returns the index of the first occurrence of any element in a given set of elements within a collection, or nil if none of the elements are found.
  67. indices(where:): Returns a sequence of indices for elements in a collection that satisfy a given predicate.
  68. prefix(while:): Returns a subcollection containing elements from the start of the collection until a condition is no longer satisfied.
  69. suffix(while:): Returns a subcollection containing elements from the end of the collection until a condition is no longer satisfied.
  70. reduce(into:while:): Combines elements in a collection into a single value using a closure that can mutate an accumulator until a condition is no longer satisfied.
  71. forEachSlice(size:do:): Performs a given operation on each slice of elements in a collection with a specified size.
  72. compactMapUniqueValues: Transforms values in a dictionary and removes any duplicate values from the results.
  73. stride(from:to:by:): Returns a sequence of values starting from a given value, up to, but not including, a specified end value, with a specified step.
  74. stride(from:through:by:): Returns a sequence of values starting from a given value, up to and including a specified end value, with a specified step.
  75. forEachChunk(size:do:): Performs a given operation on each chunk of elements in a collection with a specified size.
  76. partitionAll(by:): Splits a collection into subcollections based on a given predicate, with each subcollection containing consecutive elements that satisfy the predicate.
  77. filtering(_:isIncluded:): Filters a collection by including only the elements that satisfy a given condition, and removes the rest.
  78. splitOnce(separator:): Splits a collection into two subcollections based on the first occurrence of a separator element.
  79. reduceRight(into:combine:): Combines elements in a collection into a single value from right to left using a closure that can mutate an accumulator.
  80. forEachIndexOffset(size:do:): Performs a given operation on each element in a collection along with its index and offset from the start.
  81. cycled(): Returns a new collection that cycles through the elements of the original collection indefinitely.
  82. grouped(by:): Groups elements in a collection into a dictionary based on a given closure, with the closure's result as the key and the elements as the values.
  83. scan(_:_:): Applies a closure to each element in a collection, accumulating the results, and returns an array of the intermediate values.
  84. zip(_: _: _: _:): Creates a sequence of tuples by combining elements from four collections.
  85. lazyMap(_:): Creates a lazy sequence by applying a transformation to each element in a collection.
  86. interleave(_: _: _: _:): Creates a sequence by interleaving elements from four collections.
  87. indexed(): Returns a sequence of tuples containing the index and element for each element in a collection.
  88. unzip(_:): Separates an array of tuples into separate arrays, each containing elements at a specific position.
  89. scan(into:_:): Applies a closure to each element in a collection, accumulating the results into an array.
  90. rotate(shiftingToStart:): Returns a new collection with the elements rotated by a specified number of positions.
  91. chunked(by:): Groups elements in a collection into subarrays based on a given closure, with the closure's result determining the grouping.
  92. noneSatisfy(_:predicate:): Checks if none of the elements in a collection satisfy a given predicate.
  93. indexedFilter(_:include:): Returns an array of elements from a collection at the specified indices.
  94. consecutiveGroups(_:areInIncreasingOrder:): Groups consecutive elements in a collection based on a given closure that determines the order.
  95. forEachOffset(_:do:): Performs a given operation on each element in a collection along with its offset from the start.
  96. indexedReduce(into:combine:): Combines elements in a collection into a single value using a closure that can mutate an accumulator, with access to the element's index.
  97. groupAdjacent(by:): Groups adjacent elements in a collection based on a given closure.
  98. compactMapFirst(where:): Transforms elements in a collection and returns the first non-nil result that satisfies a given condition.
  99. interleave(sequence:): Creates a sequence by interleaving elements from a collection with another sequence.
  100. recurrentReduce(_:_:): Applies a recurrent reduction to a collection, building up an intermediate result with a given closure.
  101. append(_:while:): Appends elements to a collection until a given condition is no longer satisfied.
  102. unzip3(_:): Separates an array of triples into three separate arrays, each containing elements at a specific position.
  103. index(of:in:): Returns the index of the first occurrence of a given element in a collection, or nil if the element is not found.
  104. containsAll(_:equatable:): Checks if a collection contains all elements from another collection based on equatability.
  105. firstDuplicate(): Returns the first duplicate element in a collection, or nil if there are no duplicates.
  106. lazyFilter(_:predicate:): Creates a lazy sequence by filtering elements in a collection based on a given predicate.
  107. windowed(by:size:step:): Returns a sequence of sliding windows over a collection with a specified size and step.
  108. take(_:repeating:): Returns a collection by repeating a given element a specified number of times.
  109. forEachReversed(_:do:): Performs a given operation on each element in a collection in reverse order.
  110. countOccurrences(of:in:): Returns the number of occurrences of a given element in a collection.
  111. cycled(by:): Returns a new collection that cycles through the elements of the original collection repeatedly, applying a given closure to each cycle.
  112. consecutiveReducing(_:initialResult:nextPartialResult:): Applies a reducing closure to consecutive pairs of elements in a collection, accumulating the results.
  113. forEachSliceIndices(size:do:): Performs a given operation on each slice of elements in a collection with a specified size, passing in both the slice and its indices.
  114. chunked(by:isEquivalent:): Groups elements in a collection into subarrays based on a given closure that determines the grouping, using a custom equivalence check.
  115. rotate(shiftingToEnd:): Returns a new collection with the elements rotated by a specified number of positions, in the opposite direction as rotate(shiftingToStart:).
  116. filterUnique(by:): Filters a collection to include only the unique elements based on a given closure that determines uniqueness.
  117. lazyEnumerated(): Creates a lazy sequence of tuples containing the index and element for each element in a collection.
  118. splitAt(index:): Splits a collection into two subcollections at the specified index.
  119. reduceRight(_:nextPartialResult:combine:): Combines elements in a collection into a single value from right to left using a reducing closure.
  120. enumerateGroups(_:areEquivalent:): Groups elements in a collection based on a given closure that determines equivalence.
  121. filtering(_:isIncluded:isExcluded:): Filters a collection by including only the elements that satisfy a given inclusion condition and excluding the elements that satisfy an exclusion condition.
  122. lastIndexOf(_:in:): Returns the index of the last occurrence of a given element in a collection, or nil if the element is not found.
  123. chunked(into:): Groups elements in a collection into subarrays with a specified maximum size.
  124. unwrap(): Returns an array by unwrapping optional elements in a collection and removing any nil values.
  125. repeatingElements(): Returns a new collection by repeating each element in the original collection a number of times equal to its value.
  126. forEachPair(_:do:): Performs a given operation on each pair of adjacent elements in a collection.
  127. chunked(by:isIncluded:): Groups elements in a collection into subarrays based on a given closure that determines the grouping, and includes only the elements that satisfy a condition.
  128. indexedScan(into:_:): Applies a closure to each element in a collection, accumulating the results into an array, and providing access to the element's index.
  129. slice(from:to:): Returns a subsequence of elements from a collection, starting from a given start index and ending before a given end index.
  130. reduceWhile(_:nextPartialResult:shouldContinue:): Combines elements in a collection into a single value using a reducing closure, until a condition is no longer satisfied.
  131. takeLast(_:repeating:): Returns a collection by repeating a given element a specified number of times, starting from the end.
  132. repeatedPermutations(of:count:): Returns a sequence of arrays representing all possible repeated permutations of a collection's elements, up to a specified count.
  133. filterDuplicates(): Filters a collection to include only the unique elements, removing any duplicates.
  134. allEqual(): Checks if all elements in a collection are equal.
  135. forEachWindow(size:do:): Performs a given operation on each window of elements in a collection with a specified size.
  136. mapIndexed(_:transform:): Transforms elements in a collection using a closure that receives both the element and its index.
  137. interpose(element:): Returns a new collection by inserting a given element between every pair of adjacent elements in the original collection.
  138. concurrentMap(_:_:): Applies a given closure to each element in a collection concurrently, returning an array of the results.
  139. forEachPermutation(_:do:): Performs a given operation on each permutation of a collection's elements.
  140. firstIndex(of:in:where:): Returns the index of the first occurrence of a given element in a collection that satisfies a given condition, or nil if the element is not found.
  141. groupBy(_:transform:): Groups elements in a collection into a dictionary based on a given closure that determines the key for each element.
  142. reduceRightWhile(_:nextPartialResult:shouldContinue:): Combines elements in a collection into a single value from right to left using a reducing closure, until a condition is no longer satisfied.
  143. rotated(by:): Returns a new collection with the elements rotated by a specified number of positions, positive for clockwise rotation and negative for counterclockwise rotation.
  144. indexedPartition(_:isIncluded:): Partitions a collection into two subcollections based on a given closure, and retains the indices of the original elements.
  145. reducedValues(): Returns an array of unique values obtained by applying a reducing closure to each element in a collection.
  146. forEachCombination(ofSize:do:): Performs a given operation on each combination of a specified size from a collection's elements.
  147. trimPrefix(_:isEquivalent:): Returns a subcollection by removing elements from the start of a collection that match a given predicate, using a custom equivalence check.
  148. filterPairs(_:include:): Filters a collection to include only the pairs of adjacent elements that satisfy a given condition.
  149. unwrapMap<U>(_:_:): Transforms elements in a collection using a closure that unwraps optional elements and maps them to a new type.
  150. recurrentReduce(into:initialResult:nextPartialResult:): Applies a recurrent reducing closure to a collection, building up an intermediate result with a given closure that can mutate the accumulator.
  151. flatMapIndexed(_:transform:): Transforms elements in a collection using a closure that receives both the element and its index, and flattens the results.

I took the help of AI to collect these methods. These methods can make life a lot easier while building an app.

Thank you

--

--

Yasir

Exploring the Swift, SwiftUI, and Apple universe.