Last active
August 14, 2019 01:31
-
-
Save jayrbolton/f8293204b563882fda40569d36a204d1 to your computer and use it in GitHub Desktop.
Asynchronously count words from from stdin and print formatted results to stdout
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
import @/table | |
export word_count | |
main | |
# Channel of words | |
| words <- !split_to_channel /\s/ stdin | |
# Hash table of word mapped to count | |
| word_counts <- !word_count words | |
$ iterate line <- !format word_counts | |
| !stdout line | |
$ repeat | |
fun !word_count | |
arg words <- :channel :str | |
ret :table/table | |
| counts <- !table/create_table | |
# Each word in this loop is processed asynchronously | |
$ receive word <- words | |
| int <- !table/get_default word 0 counts | |
| incr <- !incr int | |
| !table/set word incr counts | |
$ repeat | |
$ return counts | |
fun !format | |
arg counts <- :table/table | |
gen :str | |
| key_vals <- !table/iter counts | |
$ iterate key_val <- key_vals | |
| key <- @key key_val | |
| val <- @val key_val | |
$ generate "${key}\t${val}\n" | |
$ repeat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment