It’s a collection of 150 swift functional methods with a short description about them.
map
: Transforms each element in a collection and returns an array containing the results.flatMap
(orcompactMap
in Swift 5.0 and later): Transforms each element in a collection and flattens the results into a single-level array.filter
: Returns a new array containing only the elements that satisfy a given condition.reduce
: Combines all elements in a collection into a single value by applying a combining closure.forEach
: Performs a given operation on each element in a collection.sort
(orsorted
): Returns a new array with the elements sorted according to a given closure.contains
: Checks if a collection contains an element that satisfies a given condition.allSatisfy
: Checks if all elements in a collection satisfy a given condition.anySatisfy
: Checks if any element in a collection satisfies a given condition.first(where:)
: Returns the first element in a collection that satisfies a given condition.compactMap
(orflatMap
in Swift 4.2 and earlier): Transforms each element in a collection and removes anynil
values from the results.split
: Splits a collection into multiple subcollections based on a given condition.joined
: Returns a string by concatenating the elements of a collection into a single string.prefix
: Returns a subcollection containing the specified number of elements from the start of the collection.suffix
: Returns a subcollection containing the specified number of elements from the end of the collection.drop
: Returns a subcollection by omitting the specified number of elements from the start of the collection.compactMapValues
: Transforms the values of a dictionary and removes anynil
values from the results.- Certainly! Here are a few more higher-order functions available in Swift for working with collections:
drop(while:)
: Returns a subcollection by omitting elements from the start of the collection until a condition is no longer satisfied.prefix(while:)
: Returns a subcollection by taking elements from the start of the collection until a condition is no longer satisfied.enumerated
: Returns a sequence of pairs (index, element) for each element in a collection.reversed
: Returns a new collection with the elements in reverse order.min(by:)
: Returns the minimum element in a collection based on a given closure.max(by:)
: Returns the maximum element in a collection based on a given closure.shuffle
: Returns a new collection with the elements shuffled randomly.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.contains(where:)
: Checks if a collection contains an element that satisfies a given condition.firstIndex(where:)
: Returns the index of the first element that satisfies a given condition.lastIndex(where:)
: Returns the index of the last element that satisfies a given condition.reduce(into:_:)
: Combines all elements in a collection into a single value by applying a closure that can mutate an accumulator.forEachIndex
: Performs a given operation on each element in a collection along with its corresponding index.zip
: Creates a sequence of pairs by combining elements from two collections.compactMapUnique
: Transforms elements in a collection and removes any duplicate values from the results.reduce(intoResult:_:)
: Combines all elements in a collection into a single result value by applying a closure that can mutate the result.indices
: Returns a range of all the valid indices for a collection.first
: Returns the first element in a collection, ornil
if the collection is empty.last
: Returns the last element in a collection, ornil
if the collection is empty.first(where:)
: Returns the first element in a collection that satisfies a given condition.last(where:)
: Returns the last element in a collection that satisfies a given condition.single(where:)
: Returns the single element in a collection that satisfies a given condition, ornil
if there are zero or more than one such element.dropLast
: Returns a subcollection by omitting the specified number of elements from the end of the collection.prefix(upTo:)
: Returns a subcollection containing all the elements from the start of the collection up to, but not including, the specified index.suffix(from:)
: Returns a subcollection containing all the elements from the specified index to the end of the collection.reduce(into:combine:)
: Combines all elements in a collection into a single value using a closure that can mutate an accumulator.reduce(_:combine:)
: Combines all elements in a collection into a single value using a closure.forEach(_:do:)
: Performs a given operation on each element in a collection while passing in the index of each element.contains(_:equatable:)
: Checks if a collection contains a specific element based on equatability.allSatisfy(_:predicate:)
: Checks if all elements in a collection satisfy a given predicate.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.firstIndex(of:)
: Returns the index of the first occurrence of a given element in a collection, ornil
if the element is not found.lastIndex(of:)
: Returns the index of the last occurrence of a given element in a collection, ornil
if the element is not found.dropFirst
: Returns a subcollection by omitting the specified number of elements from the start of the collection.prefix(through:)
: Returns a subcollection containing all the elements from the start of the collection up to and including the specified index.suffix(through:)
: Returns a subcollection containing all the elements from the specified index to the end of the collection, inclusive.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.min()
: Returns the minimum element in a collection of comparable elements.max()
: Returns the maximum element in a collection of comparable elements.reversed()
: Returns a new collection with the elements in reverse order.allSatisfy(_:contains:)
: Checks if all elements in a collection satisfy a given condition by using thecontains
function.forEach(_:index:do:)
: Performs a given operation on each element in a collection while passing in both the element and its index.firstIndex(of:where:)
: Returns the index of the first occurrence of a given element that satisfies a given condition in a collection, ornil
if the element is not found.lastIndex(of:where:)
: Returns the index of the last occurrence of a given element that satisfies a given condition in a collection, ornil
if the element is not found.split(separator:maxSplits:omittingEmptySubsequences:)
: Splits a collection into subcollections based on a separator element and a maximum number of splits, optionally omitting empty subsequences.joined(separator:)
: Returns a string by concatenating the elements of a collection into a single string, separated by a given separator.lazy
: Creates a lazy sequence that defers the execution of transformations until they are actually needed.firstIndex(_:in:)
: Returns the index of the first occurrence of any element in a given set of elements within a collection, ornil
if none of the elements are found.indices(where:)
: Returns a sequence of indices for elements in a collection that satisfy a given predicate.prefix(while:)
: Returns a subcollection containing elements from the start of the collection until a condition is no longer satisfied.suffix(while:)
: Returns a subcollection containing elements from the end of the collection until a condition is no longer satisfied.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.forEachSlice(size:do:)
: Performs a given operation on each slice of elements in a collection with a specified size.compactMapUniqueValues
: Transforms values in a dictionary and removes any duplicate values from the results.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.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.forEachChunk(size:do:)
: Performs a given operation on each chunk of elements in a collection with a specified size.partitionAll(by:)
: Splits a collection into subcollections based on a given predicate, with each subcollection containing consecutive elements that satisfy the predicate.filtering(_:isIncluded:)
: Filters a collection by including only the elements that satisfy a given condition, and removes the rest.splitOnce(separator:)
: Splits a collection into two subcollections based on the first occurrence of a separator element.reduceRight(into:combine:)
: Combines elements in a collection into a single value from right to left using a closure that can mutate an accumulator.forEachIndexOffset(size:do:)
: Performs a given operation on each element in a collection along with its index and offset from the start.cycled()
: Returns a new collection that cycles through the elements of the original collection indefinitely.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.scan(_:_:)
: Applies a closure to each element in a collection, accumulating the results, and returns an array of the intermediate values.zip(_: _: _: _:)
: Creates a sequence of tuples by combining elements from four collections.lazyMap(_:)
: Creates a lazy sequence by applying a transformation to each element in a collection.interleave(_: _: _: _:)
: Creates a sequence by interleaving elements from four collections.indexed()
: Returns a sequence of tuples containing the index and element for each element in a collection.unzip(_:)
: Separates an array of tuples into separate arrays, each containing elements at a specific position.scan(into:_:)
: Applies a closure to each element in a collection, accumulating the results into an array.rotate(shiftingToStart:)
: Returns a new collection with the elements rotated by a specified number of positions.chunked(by:)
: Groups elements in a collection into subarrays based on a given closure, with the closure's result determining the grouping.noneSatisfy(_:predicate:)
: Checks if none of the elements in a collection satisfy a given predicate.indexedFilter(_:include:)
: Returns an array of elements from a collection at the specified indices.consecutiveGroups(_:areInIncreasingOrder:)
: Groups consecutive elements in a collection based on a given closure that determines the order.forEachOffset(_:do:)
: Performs a given operation on each element in a collection along with its offset from the start.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.groupAdjacent(by:)
: Groups adjacent elements in a collection based on a given closure.compactMapFirst(where:)
: Transforms elements in a collection and returns the first non-nil result that satisfies a given condition.interleave(sequence:)
: Creates a sequence by interleaving elements from a collection with another sequence.recurrentReduce(_:_:)
: Applies a recurrent reduction to a collection, building up an intermediate result with a given closure.append(_:while:)
: Appends elements to a collection until a given condition is no longer satisfied.unzip3(_:)
: Separates an array of triples into three separate arrays, each containing elements at a specific position.index(of:in:)
: Returns the index of the first occurrence of a given element in a collection, ornil
if the element is not found.containsAll(_:equatable:)
: Checks if a collection contains all elements from another collection based on equatability.firstDuplicate()
: Returns the first duplicate element in a collection, ornil
if there are no duplicates.lazyFilter(_:predicate:)
: Creates a lazy sequence by filtering elements in a collection based on a given predicate.windowed(by:size:step:)
: Returns a sequence of sliding windows over a collection with a specified size and step.take(_:repeating:)
: Returns a collection by repeating a given element a specified number of times.forEachReversed(_:do:)
: Performs a given operation on each element in a collection in reverse order.countOccurrences(of:in:)
: Returns the number of occurrences of a given element in a collection.cycled(by:)
: Returns a new collection that cycles through the elements of the original collection repeatedly, applying a given closure to each cycle.consecutiveReducing(_:initialResult:nextPartialResult:)
: Applies a reducing closure to consecutive pairs of elements in a collection, accumulating the results.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.chunked(by:isEquivalent:)
: Groups elements in a collection into subarrays based on a given closure that determines the grouping, using a custom equivalence check.rotate(shiftingToEnd:)
: Returns a new collection with the elements rotated by a specified number of positions, in the opposite direction asrotate(shiftingToStart:)
.filterUnique(by:)
: Filters a collection to include only the unique elements based on a given closure that determines uniqueness.lazyEnumerated()
: Creates a lazy sequence of tuples containing the index and element for each element in a collection.splitAt(index:)
: Splits a collection into two subcollections at the specified index.reduceRight(_:nextPartialResult:combine:)
: Combines elements in a collection into a single value from right to left using a reducing closure.enumerateGroups(_:areEquivalent:)
: Groups elements in a collection based on a given closure that determines equivalence.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.lastIndexOf(_:in:)
: Returns the index of the last occurrence of a given element in a collection, ornil
if the element is not found.chunked(into:)
: Groups elements in a collection into subarrays with a specified maximum size.unwrap()
: Returns an array by unwrapping optional elements in a collection and removing anynil
values.repeatingElements()
: Returns a new collection by repeating each element in the original collection a number of times equal to its value.forEachPair(_:do:)
: Performs a given operation on each pair of adjacent elements in a collection.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.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.slice(from:to:)
: Returns a subsequence of elements from a collection, starting from a given start index and ending before a given end index.reduceWhile(_:nextPartialResult:shouldContinue:)
: Combines elements in a collection into a single value using a reducing closure, until a condition is no longer satisfied.takeLast(_:repeating:)
: Returns a collection by repeating a given element a specified number of times, starting from the end.repeatedPermutations(of:count:)
: Returns a sequence of arrays representing all possible repeated permutations of a collection's elements, up to a specified count.filterDuplicates()
: Filters a collection to include only the unique elements, removing any duplicates.allEqual()
: Checks if all elements in a collection are equal.forEachWindow(size:do:)
: Performs a given operation on each window of elements in a collection with a specified size.mapIndexed(_:transform:)
: Transforms elements in a collection using a closure that receives both the element and its index.interpose(element:)
: Returns a new collection by inserting a given element between every pair of adjacent elements in the original collection.concurrentMap(_:_:)
: Applies a given closure to each element in a collection concurrently, returning an array of the results.forEachPermutation(_:do:)
: Performs a given operation on each permutation of a collection's elements.firstIndex(of:in:where:)
: Returns the index of the first occurrence of a given element in a collection that satisfies a given condition, ornil
if the element is not found.groupBy(_:transform:)
: Groups elements in a collection into a dictionary based on a given closure that determines the key for each element.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.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.indexedPartition(_:isIncluded:)
: Partitions a collection into two subcollections based on a given closure, and retains the indices of the original elements.reducedValues()
: Returns an array of unique values obtained by applying a reducing closure to each element in a collection.forEachCombination(ofSize:do:)
: Performs a given operation on each combination of a specified size from a collection's elements.trimPrefix(_:isEquivalent:)
: Returns a subcollection by removing elements from the start of a collection that match a given predicate, using a custom equivalence check.filterPairs(_:include:)
: Filters a collection to include only the pairs of adjacent elements that satisfy a given condition.unwrapMap<U>(_:_:)
: Transforms elements in a collection using a closure that unwraps optional elements and maps them to a new type.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.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