Created
March 11, 2018 06:24
-
-
Save trickster/c563eeab4b4f99dafcd6790330db6832 to your computer and use it in GitHub Desktop.
Parallel stuff in Nim
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 threadpool, tables, strutils | |
const | |
filesArray = ["data1.txt", "data2.txt", "data3.txt"] | |
proc countWords2(filename: string): CountTableRef[string] = | |
result = newCountTable[string]() | |
for word in readFile(filename).splitWhitespace(): | |
result.inc word | |
proc main() = | |
var tab = newCountTable[string]() | |
var results: array[filesArray.len, FlowVar[CountTableRef[string]]] | |
for i, f in filesArray: | |
results[i] = spawn countWords2(f) | |
for i in 0..results.high: | |
tab.merge(^results[i]) | |
echo tab | |
tab.sort() | |
echo tab.largest | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment