Skip to content

Commit

Permalink
Merge pull request #2165 from realm/jp-reduce-into
Browse files Browse the repository at this point in the history
Use Sequence.reduce(into:) when possible
  • Loading branch information
jpsim authored Apr 23, 2018
2 parents c3bff9c + fbeaaeb commit 536fa6e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 14 deletions.
7 changes: 2 additions & 5 deletions Source/SwiftLintFramework/Models/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,8 @@ private func containsDuplicateIdentifiers(_ identifiers: [String]) -> Bool {
return false
}

let duplicateRules = identifiers.reduce([String: Int]()) { accu, element in
var accu = accu
accu[element] = (accu[element] ?? 0) + 1
return accu
}.filter { $0.1 > 1 }
let duplicateRules = identifiers.reduce(into: [String: Int]()) { $0[$1] = ($0[$1] ?? 0) + 1 }
.filter { $0.1 > 1 }
queuedPrintError(duplicateRules.map { rule in
"configuration error: '\(rule.0)' is listed \(rule.1) times"
}.joined(separator: "\n"))
Expand Down
4 changes: 1 addition & 3 deletions Source/swiftlint/Helpers/Benchmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ struct Benchmark {

func save() {
// Decomposed to improve compile times
let entriesDict: [String: Double] = entries.reduce([String: Double]()) { accu, idAndTime in
var accu = accu
let entriesDict: [String: Double] = entries.reduce(into: [String: Double]()) { accu, idAndTime in
accu[idAndTime.id] = (accu[idAndTime.id] ?? 0) + idAndTime.time
return accu
}
let entriesKeyValues: [(String, Double)] = entriesDict.sorted { $0.1 < $1.1 }
let lines: [String] = entriesKeyValues.map { id, time -> String in
Expand Down
7 changes: 1 addition & 6 deletions Tests/SwiftLintFrameworkTests/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ private extension Configuration {
}
}

// swiftlint:disable type_body_length
class ConfigurationTests: XCTestCase {

func testInit() {
Expand Down Expand Up @@ -98,11 +97,7 @@ class ConfigurationTests: XCTestCase {
"disabled_rules": ["identifier_name"],
"whitelist_rules": whitelist
]
let combinedRulesConfigDict = enabledRulesConfigDict.reduce(disabledRulesConfigDict) {
var dict = $0
dict[$1.0] = $1.1
return dict
}
let combinedRulesConfigDict = enabledRulesConfigDict.reduce(into: disabledRulesConfigDict) { $0[$1.0] = $1.1 }
var configuration = Configuration(dict: enabledRulesConfigDict)
XCTAssertNil(configuration)
configuration = Configuration(dict: disabledRulesConfigDict)
Expand Down

0 comments on commit 536fa6e

Please sign in to comment.