Swift Standard Libraries: Sequence and Collection Functions

In the last post in this series, I took at look at the protocols that Swift uses to define generators, sequences and collections. In this post I am going to present examples of using the Standard Library Functions that operate on these types. I’ll run through them in alphabetical order.

Throughout this post I will use “a SequenceType” as a shorthand for “an object conforming to SequenceType protocol” and similar shorthand for other type-like protocols.

I have purposely left out the function definitions. Click on the swifter links to see the gory details. In the playground for the post you can also option-click.

So without further ado let’s get going.

advance

Advances an index by a given number of elements.

advance reference on Swifter

contains

Determines if a SequenceType contains an element

or contains an element that satisfies a predicate

contains reference on Swifter

count

Counts the number of elements in a range

count reference on Swifter

countElements

Counts the number of elements in a CollectionType

countElements reference on Swifter

distance

The distance in elements between two ForwardIndexTypes

distance reference on Swifter

dropFirst

Returns the slice obtained by dropping the first element of a sequence

dropFirst reference on Swifter

dropLast

Returns the slice obtained by dropping the first element of a sequence

dropLast reference on Swifter

enumerate

Prints

enumerate reference on Swifter

equal

Tests two sequences for equality

equal reference on Swifter

extend

Extends a mutable sequence

extend reference on Swifter

filter

Filters a sequence based on a predicate

filter reference on Swifter

first

Returns the first element of a collection

first reference on Swifter

indices

Returns the range of valid indices for a collection

indices reference on Swifter

insert

Inserts a new element into a RangeReplaceableCollectionType

insert reference on Swifter

isEmpty

Returns true if a collection contains no elements

isEmpty reference on Swifter

join

Returns a collection formed by placing a separator between each element of a sequence

join reference on Swifter

last

Returns the last element of a collection (with a BidirectionalIndexType) or nil

last reference on Swifter

lazy

Will be handled in a future post

map

Returns the array generated by applying a function to each element of an array

map reference on Swifter

maxElement

Returns the maximum element of a sequence

maxElement reference on Swifter

minElement

Return the minimum element of a sequence

minElement reference on Swifter

prefix

Will be handled in a future post. Operates on a Sliceable.

reduce

Accumulates the result of a function on each element of sequence

reduce reference on Swifter

removeAll

Remove all elements from a RangeReplaceableCollectionType, optionally requesting the storage capacity be preserved.

removeAll reference on Swifter

removeAtIndex

Remove and return an element from a RangeReplaceableCollectionType

removeAtIndex reference on Swifter

removeLast

Remove and return the last element from a nonempty RangeReplaceableCollectionType

removeLast reference on Swifter

removeRange

Remove elements within a specified index range

removeRange reference on Swifter

reverse

Reverses a CollectionType with an index conforming to BidirectionalIndexType

reverse reference on Swifter

sort

Sorts a mutable collection in place using the < operator or a user supplied comparison function.

sort reference on Swifter

sorted

Returns the Array obtained by sorting a SequenceType using the < operator or a user supplied comparison function.

sorted reference on Swifter

splice

Inserts the elements of a collection into a RangeReplaceableCollectionType at a given index.

splice reference on Swifter

split

split reference on Swifter

startsWith

Determines if the prefix of one SequenceType is equivalent to another SequenceType either using the == operator or a user defined equivalence function

startsWith reference on Swifter

stride

Creates sequences from a given value, to or through a given value, steping by a given increment (or stride).

stride reference on Swifter

underestimateCount

Will be covered in a future post.

Download the Playground

The playground for this, and all other posts in the series, can be found on GitHub in the SwiftStandardLibraryPlaygrounds repository.


About idz

A professional software engineer dabbling in iOS app development.
This entry was posted in Code Snippets, Swift, Swift Standard Library. Bookmark the permalink.

Leave a Reply