Sha256: 310f8633ffffbe90e8b0e7dc31845efdc57389f468a68f516bd540d4667f3bf4

Contents?: true

Size: 564 Bytes

Versions: 27

Compression:

Stored size: 564 Bytes

Contents

import
  critbits, strutils

type TWordCount* = CritBitTree[int]
  ## A mapping from strings (`words`) to ints (`counts`).

iterator words(s: string): string =
  for word in s.split(AllChars - Letters - Digits - {'\0'}):
    yield toLowerAscii(word)

proc wordCount*(s: string): TWordCount {.noSideEffect.} =
  ## Returns a mapping from the words (alphanumeric sequences) in `s` to their
  ## respective counts.
  for word in words(s):
    if word.len == 0:
      continue
    if not result.hasKey(word):
      result[word] = 0
    result[word] = result[word] + 1

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/nim/exercises/word-count/example.nim
trackler-2.2.1.138 tracks/nim/exercises/word-count/example.nim
trackler-2.2.1.137 tracks/nim/exercises/word-count/example.nim
trackler-2.2.1.136 tracks/nim/exercises/word-count/example.nim
trackler-2.2.1.135 tracks/nim/exercises/word-count/example.nim
trackler-2.2.1.134 tracks/nim/exercises/word-count/example.nim
trackler-2.2.1.133 tracks/nim/exercises/word-count/example.nim