Skip to content

Commit

Permalink
Add map to pair protocols (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwetherfield authored and jsbean committed Nov 16, 2018
1 parent 4ad3b05 commit 83e5818
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 4 deletions.
11 changes: 11 additions & 0 deletions Sources/DataStructures/Pair/Pair.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,14 @@ public protocol Pair {
/// Creates a `Pair` with the given values.
init(_ a: A, _ b: B)
}

extension Pair {

// MARK: - Instance Methods

/// - Returns: A `Pair` with its members transformed by the given function.
public func map <P,C,D> (_ f: (A,B) -> (C,D)) -> P where P: Pair, P.A == C, P.B == D {
let (c,d) = f(self.a, self.b)
return P(c,d)
}
}
10 changes: 10 additions & 0 deletions Sources/DataStructures/Pair/SymmetricPair.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ extension SymmetricPair where A: Equatable {
return a == value || b == value
}
}

extension SymmetricPair {

// MARK: - Instance Methods

/// - Returns: A `SymmetricPair` with its members transformed by the given function.
public func map <P,C> (_ f: (A) -> C) -> P where P: SymmetricPair, P.A == C {
return P(f(self.a), f(self.b))
}
}
2 changes: 1 addition & 1 deletion Tests/AlgorithmsPerformanceTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import XCTest

extension StableSortPerformanceTests {
static let __allTests = [
("testStableSort", testStableSort_O_nlogn),
("testStableSort_O_nlogn", testStableSort_O_nlogn),
]
}

Expand Down
9 changes: 9 additions & 0 deletions Tests/DataStructuresTests/CrossTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@ class CrossTests: XCTestCase {
XCTAssertFalse(a < b)
XCTAssertFalse(b > a)
}

func testMap() {
let start = Cross("a",2)
let expected = Cross(4,"aa")
let function: (String,Int) -> (Int,String) = { string, int in
(int*2, string+string)
}
XCTAssertEqual(start.map(function),expected)
}
}
18 changes: 18 additions & 0 deletions Tests/DataStructuresTests/PairTests/OrderedPairTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// OrderedPairTests.swift
// Structure
//
// Created by Benjamin Wetherfield on 15/11/2018.
//

import XCTest
import DataStructures

class OrderedPairTests: XCTestCase {

func testMap() {
let start = OrderedPair("a","ab")
let expected = OrderedPair(1,2)
XCTAssertEqual(start.map { $0.count }, expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ class UnorderedPairTests: XCTestCase {
}
}
}

func testMap() {
let start = UnorderedPair("a","ab")
let expected = UnorderedPair(1,2)
XCTAssertEqual(start.map { $0.count }, expected)
}
}

func randomString(maxLength: Int = 10) -> String {
Expand Down
15 changes: 12 additions & 3 deletions Tests/DataStructuresTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ extension ArrayExtensionsTests {

extension BimapTests {
static let __allTests = [
("testCompose", testCompose),
("testComposeOperator", testComposeOperator),
("testInitEmpty", testInitEmpty),
("testKeySubscript", testKeySubscript),
("testRemoveAll", testRemoveAll),
Expand All @@ -43,8 +45,6 @@ extension BimapTests {
("testUpdateKey", testUpdateKey),
("testUpdateValue", testUpdateValue),
("testValueSubscript", testValueSubscript),
("testCompose", testCompose),
("testComposeOperator", testComposeOperator),
]
}

Expand All @@ -53,7 +53,7 @@ extension BinaryHeapTests {
("testBalance", testBalance),
("testBasicInsertPop", testBasicInsertPop),
("testPopNil", testPopNil),
("testSimpleBalance", testSimpleBalance)
("testSimpleBalance", testSimpleBalance),
]
}

Expand Down Expand Up @@ -110,6 +110,7 @@ extension CrossTests {
("testComparableFalseEqual", testComparableFalseEqual),
("testComparableLexicographic", testComparableLexicographic),
("testComparableLexicographicFalse", testComparableLexicographicFalse),
("testMap", testMap),
]
}

Expand Down Expand Up @@ -282,6 +283,12 @@ extension OrderedDictionaryTests {
]
}

extension OrderedPairTests {
static let __allTests = [
("testMap", testMap),
]
}

extension PairsTests {
static let __allTests = [
("testPairs", testPairs),
Expand Down Expand Up @@ -517,6 +524,7 @@ extension UnorderedPairTests {
("testManyHashValuesIntForCollisions", testManyHashValuesIntForCollisions),
("testManyHashValuesString", testManyHashValuesString),
("testManyHashValuesStringForCollisions", testManyHashValuesStringForCollisions),
("testMap", testMap),
]
}

Expand Down Expand Up @@ -581,6 +589,7 @@ public func __allTests() -> [XCTestCaseEntry] {
testCase(MutableGraphTests.__allTests),
testCase(NewTypeTests.__allTests),
testCase(OrderedDictionaryTests.__allTests),
testCase(OrderedPairTests.__allTests),
testCase(PairsTests.__allTests),
testCase(QueueTests.__allTests),
testCase(ReferenceTreeProtocolTests.__allTests),
Expand Down

0 comments on commit 83e5818

Please sign in to comment.