Skip to content

Instantly share code, notes, and snippets.

@masakih
Created March 16, 2021 14:30
Show Gist options
  • Select an option

  • Save masakih/09e37f241947c816dbb7ead7904ca80c to your computer and use it in GitHub Desktop.

Select an option

Save masakih/09e37f241947c816dbb7ead7904ca80c to your computer and use it in GitHub Desktop.
これ #CodePiece
struct Reader: Sequence {
struct Iterator: IteratorProtocol {
func next() -> String? {
readLine()
}
}
func makeIterator() -> Iterator {
.init()
}
}
Reader()
.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