Skip to content

Instantly share code, notes, and snippets.

@kemchenj
Last active September 13, 2017 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kemchenj/b06d763c7621c6aaff5024d2bc0bee9a to your computer and use it in GitHub Desktop.
Save kemchenj/b06d763c7621c6aaff5024d2bc0bee9a to your computer and use it in GitHub Desktop.
通过给 Sequence 添加 extension 去检测 Element 的频率 https://twitter.com/chriseidhof/status/906148040020553728
let result = "hello".frequencies.filter { $0.value > 1 }
print(result) // ["l": 2]
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