Last active
May 13, 2016 08:54
-
-
Save huguesbr/b2fbece936ca2f0b8950404cd66a26a9 to your computer and use it in GitHub Desktop.
Array groupedBy Extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import XCTest | |
extension Array { | |
func groupedByA<Key: Hashable>(mappingClosure: (Element) -> Key?) -> [Key: [Element]] { | |
return reduce([:]) { (groupedBy, element) in | |
guard let key = mappingClosure(element) else { | |
return groupedBy | |
} | |
var groupedBy = groupedBy | |
groupedBy[key] = groupedBy[key] ?? [Element]() | |
groupedBy[key]?.append(element) | |
return groupedBy | |
} | |
} | |
func groupedByB<Key: Hashable>(mappingClosure: (Element) -> Key?) -> [Key: [Element]] { | |
var groupedBy: [Key: [Element]] = [:] | |
forEach { element in | |
if let key = mappingClosure(element) { | |
groupedBy[key] = groupedBy[key] ?? [Element]() | |
groupedBy[key]?.append(element) | |
} | |
} | |
return groupedBy | |
} | |
} | |
extension String { | |
static func randomAlphanumericString(length: Int) -> String { | |
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".characters | |
let lettersLength = UInt32(letters.count) | |
let randomCharacters = (0..<length).map { i -> String in | |
let offset = Int(arc4random_uniform(lettersLength)) | |
let c = letters[letters.startIndex.advancedBy(offset)] | |
return String(c) | |
} | |
return randomCharacters.joinWithSeparator("") | |
} | |
} | |
extension Int { | |
static func random(max: UInt32) -> Int { | |
return Int(arc4random_uniform(max)) | |
} | |
} | |
extension Bool { | |
static func random() -> Bool { | |
return arc4random_uniform(1) > 0 | |
} | |
} | |
class Test: XCTestCase { | |
struct Person { | |
var name: String | |
var age: Int? | |
} | |
lazy var persons: [Person] = { | |
var persons: [Person] = [] | |
for _ in 0...100000 { | |
let length = Int.random(20) | |
let name = String.randomAlphanumericString(length) | |
let age = Int.random(99) | |
let person = Person(name: name, age: age) | |
persons.append(person) | |
} | |
return persons | |
}() | |
func testA() { | |
measureBlock { | |
self.persons.groupedByA { $0.name.characters.first } | |
} | |
} | |
func testB() { | |
measureBlock { | |
self.persons.groupedByB { $0.name.characters.first } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test Suite 'All tests' started at 2016-05-13 10:49:24.949
Test Suite 'GroupedByTests.xctest' started at 2016-05-13 10:49:24.950
Test Suite 'Test' started at 2016-05-13 10:49:24.951
Test Case '-[GroupedByTests.Test testA]' started.
GroupedByTests.swift:86: Test Case '-[GroupedByTests.Test testA]' measured [Time, seconds] average: 4.866, relative standard deviation: 30.558%, values: [9.325309, 4.356858, 4.313964, 4.359974, 4.352255, 4.455384, 4.371096, 4.324231, 4.436787, 4.365156], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100
Test Case '-[GroupedByTests.Test testA]' passed (48.932 seconds).
Test Case '-[GroupedByTests.Test testB]' started.
GroupedByTests.swift:92: Test Case '-[GroupedByTests.Test testB]' measured [Time, seconds] average: 3.817, relative standard deviation: 38.830%, values: [8.261461, 3.290685, 3.328974, 3.281495, 3.315450, 3.381211, 3.335369, 3.346904, 3.289857, 3.333601], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100
Test Case '-[GroupedByTests.Test testB]' passed (38.419 seconds).
Test Suite 'Test' passed at 2016-05-13 10:50:52.303.
Executed 2 tests, with 0 failures (0 unexpected) in 87.351 (87.353) seconds