The other day I wrote a merge code and although it works, I am disabled by the code. I would like to see how it will look in other languages.
So, for input, the routine accepts the contact list
Jim,Smith,2681 Eagle Peak,,Bellevue,Washington,United States,98004 Erica,Johnson,2681 Eagle Peak,,Bellevue,Washington,United States,98004 Abraham,Johnson,2681 Eagle Peak,,Bellevue,Washington,United States,98004 Marge,Simpson,6388 Lake City Way,,Burnaby,British Columbia,Canada,V5A 3A6 Larry,Lyon,52560 Free Street,,Toronto,Ontario,Canada,M4B 1V7 Ted,Simpson,6388 Lake City Way,,Burnaby,British Columbia,Canada,V5A 3A6 Raoul,Simpson,6388 Lake City Way,,Burnaby,British Columbia,Canada,V5A 3A6
Then it combines the lines with the same address and last name into one record. Suppose the rows are not sorted). The code should also be flexible enough so that the fields can be set in any order (therefore, field indices will be required as parameters). For a family of two, it combines both name fields. For a family of three or more, the first name is set to "the" and the last to "last name".
Erica and Abraham,Johnson,2681 Eagle Peak,,Bellevue,Washington,United States,98004 Larry,Lyon,52560 Free Street,,Toronto,Ontario,Canada,M4B 1V7 The,Simpson Family,6388 Lake City Way,,Burnaby,British Columbia,Canada,V5A 3A6 Jim,Smith,2681 Eagle Peak,,Bellevue,Washington,United States,98004
My C # implementation:
var source = File.ReadAllLines(@"sample.csv").Select(l => l.Split(',')); var merged = HouseholdMerge(source, 0, 1, new[] {1, 2, 3, 4, 5}); public static IEnumerable<string[]> HouseholdMerge(IEnumerable<string[]> data, int fnIndex, int lnIndex, int[] groupIndexes) { Func<string[], string> groupby = fields => String.Join("", fields.Where((f, i) => groupIndexes.Contains(i))); var groups = data.OrderBy(groupby).GroupBy(groupby); foreach (var group in groups) { string[] result = group.First().ToArray(); if (group.Count() == 2) { result[fnIndex] += " and " + group.ElementAt(1)[fnIndex]; } else if (group.Count() > 2) { result[fnIndex] = "The"; result[lnIndex] += " Family"; } yield return result; } }
I don't like the way I needed to delegate the group. I would like C # to somehow convert the string expression to a delegate. eg Func groupby = f => "f [2] + f [3] + f [4] + f [5] + f [1];" I have a feeling that can probably be done in Lisp or Python. I look forward to a good implementation in other languages.
Edit: where is the wiki community checkbox checked? Some mod, please fix this.