bokoness

Untitled

Jun 16th, 2021
686
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Package letter counts the frequency of letters in text
  2. package letter
  3.  
  4. type FreqMap map[rune]int
  5.  
  6. // ConcurrentFrequency uses concurreny to calculate
  7. //the frequency of each letter each string in list map
  8. func ConcurrentFrequency(list []string) FreqMap {
  9.     f := FreqMap{}
  10.     c := make(chan FreqMap)
  11.     for _, s := range list {
  12.         go func(s string) {
  13.             c <- Frequency(s)
  14.         }(s)
  15.     }
  16.     for range list {
  17.         for i, v := range <-c {
  18.             f[i] += v
  19.         }
  20.     }
  21.     return f
  22. }
  23.  
  24. // Frequency calculates the frequency of each letter in s
  25. func Frequency(s string) FreqMap {
  26.     m := FreqMap{}
  27.     for _, r := range s {
  28.         m[r]++
  29.     }
  30.     return m
  31. }
  32.  
RAW Paste Data