Swiftpack.co - Package - dennisvennink/SequenceExtensions

SequenceExtensions

Requires Swift > 4 Licensed under the MIT license

Table Of Contents

Introduction

SequenceExtensions is a library that adds various Sequence-related operations.

Installation

Swift Package Manager

To install this package, add it as a dependency to your project's manifest file (Package.swift), e.g., a package named "Example" whose main product is a library with no dependencies other than SequenceExtensions would be defined as such:

// swift-tools-version:4.0
import PackageDescription

let package = Package(
  name: "Example",
  products: [
    .library(name: "Example", targets: ["Example"])
  ],
  dependencies: [
    .package(url: "https://github.com/dennisvennink/SequenceExtensions.git", from: "4.0.0")
  ],
  targets: [
    .target(name: "Example", dependencies: ["SequenceExtensions"]),
    .testTarget(name: "ExampleTests", dependencies: ["Example"])
  ]
)

Then, import it into your project:

import SequenceExtensions

Contributing

To contribute, think of a missing feature or issue to work on, then fork the project and create your feature branch:

git checkout -b my-new-feature

When you're done implementing your feature, commit your changes:

git commit -am "Add some new feature"

Then push to the feature branch:

git push origin my-new-feature

Finally, submit a pull request!

Testing

All tests are written in XCTest. To perform them, run:

swift test

API

Operator Overloads

==(_:_:)

Declarations
public func == <Sequence1: Sequence, Sequence2: Sequence, Equatable1: Equatable, Equatable2: Equatable> (lhs: Sequence1, rhs: Sequence2) -> Bool where Sequence1.Element == (Equatable1, Equatable2), Sequence2.Element == (Equatable1, Equatable2)
public func == <Sequence1: Sequence, Sequence2: Sequence, Equatable1: Equatable, Equatable2: Equatable, Equatable3: Equatable> (lhs: Sequence1, rhs: Sequence2) -> Bool where Sequence1.Element == (Equatable1, Equatable2, Equatable3), Sequence2.Element == (Equatable1, Equatable2, Equatable3)
public func == <Sequence1: Sequence, Sequence2: Sequence, Equatable1: Equatable, Equatable2: Equatable, Equatable3: Equatable, Equatable4: Equatable> (lhs: Sequence1, rhs: Sequence2) -> Bool where Sequence1.Element == (Equatable1, Equatable2, Equatable3, Equatable4), Sequence2.Element == (Equatable1, Equatable2, Equatable3, Equatable4)
public func == <Sequence1: Sequence, Sequence2: Sequence, Equatable1: Equatable, Equatable2: Equatable, Equatable3: Equatable, Equatable4: Equatable, Equatable5: Equatable> (lhs: Sequence1, rhs: Sequence2) -> Bool where Sequence1.Element == (Equatable1, Equatable2, Equatable3, Equatable4, Equatable5), Sequence2.Element == (Equatable1, Equatable2, Equatable3, Equatable4, Equatable5)
public func == <Sequence1: Sequence, Sequence2: Sequence, Equatable1: Equatable, Equatable2: Equatable, Equatable3: Equatable, Equatable4: Equatable, Equatable5: Equatable, Equatable6: Equatable> (lhs: Sequence1, rhs: Sequence2) -> Bool where Sequence1.Element == (Equatable1, Equatable2, Equatable3, Equatable4, Equatable5, Equatable6), Sequence2.Element == (Equatable1, Equatable2, Equatable3, Equatable4, Equatable5, Equatable6)
Description

Compares whether the left-hand Sequence of n-tuples is equal to the right-hand Sequence of n-tuples.

print([(0, 1)] == [(0, 1)])
// true
print([(0, 1, 2)] == [(0, 1, 2)])
// true
print([(0, 1, 2, 3)] == [(0, 1, 2, 3)])
// true
print([(0, 1, 2, 3, 4)] == [(0, 1, 2, 3, 4)])
// true
print([(0, 1, 2, 3, 4, 5)] == [(0, 1, 2, 3, 4, 5)])
// true
Parameters
  • lhs: The left-hand Sequence of n-tuples.
  • rhs: The right-hand Sequence of n-tuples.
Returns

A Boolean value indicating whether the left-hand Sequence of n-tuples is equal to the right-hand Sequence of n-tuples.

!=(_:_:)

Declarations
public func != <Sequence1: Sequence, Sequence2: Sequence, Equatable1: Equatable, Equatable2: Equatable> (lhs: Sequence1, rhs: Sequence2) -> Bool where Sequence1.Element == (Equatable1, Equatable2), Sequence2.Element == (Equatable1, Equatable2)
public func != <Sequence1: Sequence, Sequence2: Sequence, Equatable1: Equatable, Equatable2: Equatable, Equatable3: Equatable> (lhs: Sequence1, rhs: Sequence2) -> Bool where Sequence1.Element == (Equatable1, Equatable2, Equatable3), Sequence2.Element == (Equatable1, Equatable2, Equatable3)
public func != <Sequence1: Sequence, Sequence2: Sequence, Equatable1: Equatable, Equatable2: Equatable, Equatable3: Equatable, Equatable4: Equatable> (lhs: Sequence1, rhs: Sequence2) -> Bool where Sequence1.Element == (Equatable1, Equatable2, Equatable3, Equatable4), Sequence2.Element == (Equatable1, Equatable2, Equatable3, Equatable4)
public func != <Sequence1: Sequence, Sequence2: Sequence, Equatable1: Equatable, Equatable2: Equatable, Equatable3: Equatable, Equatable4: Equatable, Equatable5: Equatable> (lhs: Sequence1, rhs: Sequence2) -> Bool where Sequence1.Element == (Equatable1, Equatable2, Equatable3, Equatable4, Equatable5), Sequence2.Element == (Equatable1, Equatable2, Equatable3, Equatable4, Equatable5)
public func != <Sequence1: Sequence, Sequence2: Sequence, Equatable1: Equatable, Equatable2: Equatable, Equatable3: Equatable, Equatable4: Equatable, Equatable5: Equatable, Equatable6: Equatable> (lhs: Sequence1, rhs: Sequence2) -> Bool where Sequence1.Element == (Equatable1, Equatable2, Equatable3, Equatable4, Equatable5, Equatable6), Sequence2.Element == (Equatable1, Equatable2, Equatable3, Equatable4, Equatable5, Equatable6)
Description

Compares whether the left-hand Sequence of n-tuples is not equal to the right-hand Sequence of n-tuples.

print([(0, 1)] != [(0, 2)])
// true
print([(0, 1, 2)] != [(0, 1, 3)])
// true
print([(0, 1, 2, 3)] != [(0, 1, 2, 4)])
// true
print([(0, 1, 2, 3, 4)] != [(0, 1, 2, 3, 5)])
// true
print([(0, 1, 2, 3, 4, 5)] != [(0, 1, 2, 3, 4, 6)])
// true
Parameters
  • lhs: The left-hand Sequence of n-tuples.
  • rhs: The right-hand Sequence of n-tuples.
Returns

A Boolean value indicating whether the left-hand Sequence of n-tuples is not equal to the right-hand Sequence of n-tuples.

Free Functions

product( _:_: _:_:_: _:_:_:_: _:_:_:_:_: _:_:_:_:_:_: )

Declarations
public func product <Sequence1: Sequence, Sequence2: Sequence> (_ sequence1: Sequence1, _ sequence2: Sequence2) -> AnySequence<(Sequence1.Element, Sequence2.Element)>
public func product <Sequence1: Sequence, Sequence2: Sequence, Sequence3: Sequence> (_ sequence1: Sequence1, _ sequence2: Sequence2, _ sequence3: Sequence3) -> AnySequence<(Sequence1.Element, Sequence2.Element, Sequence3.Element)>
public func product <Sequence1: Sequence, Sequence2: Sequence, Sequence3: Sequence, Sequence4: Sequence> (_ sequence1: Sequence1, _ sequence2: Sequence2, _ sequence3: Sequence3, _ sequence4: Sequence4) -> AnySequence<(Sequence1.Element, Sequence2.Element, Sequence3.Element, Sequence4.Element)>
public func product <Sequence1: Sequence, Sequence2: Sequence, Sequence3: Sequence, Sequence4: Sequence, Sequence5: Sequence> (_ sequence1: Sequence1, _ sequence2: Sequence2, _ sequence3: Sequence3, _ sequence4: Sequence4, _ sequence5: Sequence5) -> AnySequence<(Sequence1.Element, Sequence2.Element, Sequence3.Element, Sequence4.Element, Sequence5.Element)>
public func product <Sequence1: Sequence, Sequence2: Sequence, Sequence3: Sequence, Sequence4: Sequence, Sequence5: Sequence, Sequence6: Sequence> (_ sequence1: Sequence1, _ sequence2: Sequence2, _ sequence3: Sequence3, _ sequence4: Sequence4, _ sequence5: Sequence5, _ sequence6: Sequence6) -> AnySequence<(Sequence1.Element, Sequence2.Element, Sequence3.Element, Sequence4.Element, Sequence5.Element, Sequence6.Element)>
Description

Creates the Cartesian product of two Sequences as a Sequence of n-tuples.

print(Array(product([1, 2], [3, 4])))
// [(1, 3), (1, 4), (2, 3), (2, 4)]
print(Array(product([1, 2], [3, 4], [5, 6]).prefix(4)))
// [(1, 3, 5), (1, 3, 6), (1, 4, 5), (1, 4, 6)]
print(Array(product([1, 2], [3, 4], [5, 6], [7, 8]).prefix(4)))
// [(1, 3, 5, 7), (1, 3, 5, 8), (1, 3, 6, 7), (1, 3, 6, 8)]
print(Array(product([1, 2], [3, 4], [5, 6], [7, 8], [9, 10]).prefix(4)))
// [(1, 3, 5, 7, 9), (1, 3, 5, 7, 10), (1, 3, 5, 8, 9), (1, 3, 5, 8, 10)]
print(Array(product([1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]).prefix(4)))
// [(1, 3, 5, 7, 9, 11), (1, 3, 5, 7, 9, 12), (1, 3, 5, 7, 10, 11), (1, 3, 5, 7, 10, 12)]
Parameters
  • sequence1: The first Sequence.
  • sequence2: The second Sequence.
  • (sequence3: The third Sequence.)
  • (sequence4: The fourth Sequence.)
  • (sequence5: The fifth Sequence.)
  • (sequence6: The sixth Sequence.)
Returns

A Cartesian product as a Sequence of n-tuples.

zip( _:_:_: _:_:_:_: _:_:_:_:_: _:_:_:_:_:_: )

Declarations
public func zip <Sequence1: Sequence, Sequence2: Sequence, Sequence3: Sequence> (_ sequence1: Sequence1, _ sequence2: Sequence2, _ sequence3: Sequence3) -> AnySequence<(Sequence1.Element, Sequence2.Element, Sequence3.Element)>
public func zip <Sequence1: Sequence, Sequence2: Sequence, Sequence3: Sequence, Sequence4: Sequence> (_ sequence1: Sequence1, _ sequence2: Sequence2, _ sequence3: Sequence3, _ sequence4: Sequence4) -> AnySequence<(Sequence1.Element, Sequence2.Element, Sequence3.Element, Sequence4.Element)>
public func zip <Sequence1: Sequence, Sequence2: Sequence, Sequence3: Sequence, Sequence4: Sequence, Sequence5: Sequence> (_ sequence1: Sequence1, _ sequence2: Sequence2, _ sequence3: Sequence3, _ sequence4: Sequence4, _ sequence5: Sequence5) -> AnySequence<(Sequence1.Element, Sequence2.Element, Sequence3.Element, Sequence4.Element, Sequence5.Element)>
public func zip <Sequence1: Sequence, Sequence2: Sequence, Sequence3: Sequence, Sequence4: Sequence, Sequence5: Sequence, Sequence6: Sequence> (_ sequence1: Sequence1, _ sequence2: Sequence2, _ sequence3: Sequence3, _ sequence4: Sequence4, _ sequence5: Sequence5, _ sequence6: Sequence6) -> AnySequence<(Sequence1.Element, Sequence2.Element, Sequence3.Element, Sequence4.Element, Sequence5.Element, Sequence6.Element)>
Description

Creates the zip of three Sequences as a Sequence of n-tuples.

print(Array(zip([1, 2], [3, 4])))
// [(1, 3), (2, 4)]
print(Array(zip([1, 2], [3, 4], [5, 6], [7, 8])))
// [(1, 3, 5, 7), (2, 4, 6, 8)]
print(Array(zip([1, 2], [3, 4], [5, 6], [7, 8], [9, 10])))
// [(1, 3, 5, 7, 9), (2, 4, 6, 8, 10)]
print(Array(zip([1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12])))
// [(1, 3, 5, 7, 9, 11), (2, 4, 6, 8, 10, 12)]
Attention

If the Sequences are of different lengths, the resulting Sequence is the same length as the shortest Sequence.

Parameters
  • sequence1: The first Sequence.
  • sequence2: The second Sequence.
  • sequence3: The third Sequence.
  • (sequence4: The fourth Sequence.)
  • (sequence5: The fifth Sequence.)
  • (sequence6: The sixth Sequence.)
Returns

A zip as a Sequence of n-tuples.

Property Extensions on Sequence

Where Element: Equatable

containsDuplicates
Declaration
var containsDuplicates: Bool { get }
Description

Returns a Boolean that specifies if self contains duplicates.

print([0, 1, 2].containsDuplicates)
// false

Where Element: Hashable

duplicates
Declaration
var duplicates: [Element: [Int]] { get }
Description

Returns a Dictionary in which the keys correspond to all Elements in self that contain duplicates and the values to their duplicates in self as an Array of Integer offsets.

print([0, 1, 1, 2, 2, 2, 3, 3, 3, 3].duplicates)
// [1: [2], 2: [4, 5], 3: [7, 8, 9]]
occurrences
Declaration
var occurrences: [Element: [Int]] { get }
Description

Returns a Dictionary in which the keys correspond to all Elements in self and the values to their occurrences in self as an Array of Integer offsets.

print([0, 1, 1, 2, 2, 2, 3, 3, 3, 3].occurrences)
// [0: [0], 1: [1, 2], 2: [3, 4, 5], 3: [6, 7, 8, 9]]

Method Extensions On Sequence

Where Element: Equatable

occurrences(of:)
Declaration
func occurrences (of element: Element) -> [Int]
Description

Returns an Array containing the occurrences of element in self as Integer offsets.

print([0, 1, 1, 2, 2, 2, 3, 3, 3, 3].occurrences(of: 2))
// [3, 4, 5]
Parameters
  • element: The Element to get the occurrences of.
Returns

An Array containing the occurrences of element in self as Integer offsets.

Where Element: Hashable

occurrences(of:)
Declaration
func occurrences (of elements: [Element]) -> [Element: [Int]]
Description

Returns a Dictionary in which the keys correspond to the Elements in elements and the values to their occurrences in self as an Array of Integer offsets.

print([0, 1, 1, 2, 2, 2, 3, 3, 3, 3].occurrences(of: [1, 3]))
// [1: [1, 2], 3: [6, 7, 8, 9]]
Parameters
  • elements: The Elements to get the occurrences of.
Returns

A Dictionary in which the keys correspond to the Elements in elements and the values to their occurrences in self as an Array of Integer offsets.

Github

link
Stars: 4
Help us keep the lights on

Dependencies

Used By

Total: