Created
March 16, 2021 14:46
-
-
Save masakih/2f6067f50d9dc00d78dcd1743d92d861 to your computer and use it in GitHub Desktop.
lazy付けて配列生成のオーバーヘッド減らしたらちょっとだけ速くなった #CodePiece
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
| struct Reader: Sequence { | |
| struct Iterator: IteratorProtocol { | |
| func next() -> String? { | |
| readLine() | |
| } | |
| } | |
| func makeIterator() -> Iterator { | |
| .init() | |
| } | |
| } | |
| Reader() | |
| .lazy | |
| .flatMap { $0.split(separator: " ") } | |
| .map { $0.lowercased() } | |
| .reduce(into: [String: Int]()) { d, w in d[w, default: 0] += 1 } | |
| .sorted { $0.1 > $1.1 } | |
| .forEach { print($0.key, $0.value) } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment