Swiftpack.co - Package - dennisvennink/SequenceExtensions

SequenceExtensions

Requires Swift > 4 Licensed under the MIT license

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

Table Of Contents

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: "2.1.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 five Sequence.)
  • (sequence6: The six 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 five Sequence.)
  • (sequence6: The six Sequence.)
Returns

A zip as a Sequence of n-tuples.

Property Extensions on Sequence

duplicates

Declaration
var duplicates: [Element] { get }
Description

Returns an Array containing all duplicate values.

print([0, 1, 1, 2, 2, 2].duplicates)
// [1, 2, 2]
Note

To get unique duplicate values call duplicates.uniques.

occurrences

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

Returns a Dictionary containing the occurrence of each value.

print([0, 1, 1, 2, 2, 2].occurrences)
// [0: 1, 1: 2, 2: 3]

uniques

Declaration
var uniques: [Element] { get }
Description

Returns an Array containing all unique values.

print([0, 1, 1, 2, 2, 2].uniques)
// [0, 1, 2]

Github

link
Stars: 3
Help us keep the lights on

Dependencies

Used By

Total: