I want to write a function that reads a file and counts the number of times each word happens. Assuming the file is being read and creating a list of lines representing each line in the file, I need a function to count the occurrence of each word. First, uses Dictionary<string,int> better approach? The key is the word, and the meaning is the number of occurrences of the word.
I wrote this function that iterates over each line and every word in a line and creates a dictionary:
static IDictionary<string, int> CountWords(IEnumerable<string> lines) var dict = new Dictionary<string, int>(); foreach (string line in lines) { string[] words = line.Split(' '); foreach (string word in words) { if (dict.ContainsKey(word)) dict[word]++; else dict.Add(word, 1); } }
However, I would like to write this function somehow .. functionally using LINQ (because LINQ is fun and I'm trying to improve my functional programming skills: D) I managed to come up with this expression, m not sure if this is the best way to do it is functional:
static IDictionary<string, int> CountWords2(IEnumerable<string> lines) { return lines .SelectMany(line => line.Split(' ')) .Aggregate(new Dictionary<string, int>(), (dict, word) => { if (dict.ContainsKey(word)) dict[word]++; else dict.Add(word, 1); return dict; }); }
So, although I have two working solutions, I am also interested to know what is the best approach to this problem. Anyone with an understanding of LINQ and FP?
source share