Skip to content

Instantly share code, notes, and snippets.

@foxoman
Forked from trickster/parallel.nim
Created December 21, 2022 19:32
Show Gist options
  • Save foxoman/50084ac5b8a9ef5b570e21149e77416f to your computer and use it in GitHub Desktop.
Save foxoman/50084ac5b8a9ef5b570e21149e77416f to your computer and use it in GitHub Desktop.
Parallel stuff in Nim
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