Last active
September 13, 2017 16:44
-
-
Save kemchenj/b06d763c7621c6aaff5024d2bc0bee9a to your computer and use it in GitHub Desktop.
通过给 Sequence 添加 extension 去检测 Element 的频率 https://twitter.com/chriseidhof/status/906148040020553728
This file contains 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
let result = "hello".frequencies.filter { $0.value > 1 } | |
print(result) // ["l": 2] |
This file contains 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 | |
extension Sequence where Element: Hashable { | |
var frequencies: [Element: Int] { | |
return Dictionary(lazy.map { ($0: 1) }, uniquingKeysWith: +) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment