Skip to content

Instantly share code, notes, and snippets.

@ShelbyCohen
Created December 18, 2019 04:04
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 ShelbyCohen/05097a7bec9ad7d6ef60d7d184627a9a to your computer and use it in GitHub Desktop.
Save ShelbyCohen/05097a7bec9ad7d6ef60d7d184627a9a to your computer and use it in GitHub Desktop.
val toIntegerStream = { string: String ->
IterableSpliterator.of(string.chars().boxed().spliterator()).asFlow()
}
val histoOfLetters = { word: String ->
flow {
emit(toIntegerStream(word).fold(HashMap<Int, LongWrapper>()) { accumulator, value ->
var newValue: LongWrapper? = accumulator[value]
if (newValue == null) {
newValue = LongWrapper.zero()
}
accumulator[value] = newValue.incAndSet()
accumulator
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment