Note that there are some explanatory texts on larger screens.

plurals
  1. POHousehold mail merge (code golf)
    primarykey
    data
    text
    <p>I wrote some mail merge code the other day and although it works I'm a turned off by the code. I'd like to see what it would look like in other languages.</p> <p>So for the input the routine takes a list of contacts</p> <pre><code>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 </code></pre> <p>It will then merge lines with the same address and surname into one record. Assume the rows are unsorted). The code should also be flexible enough that fields can be supplied in any order (so it will need to take field indexes as parameters). For a family of two it concatenates both first name fields. For a family of three or more the first name is set to "the" and the lastname is set to "surname family". </p> <pre><code>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 </code></pre> <p>My C# implementation of this is:</p> <pre><code>var source = File.ReadAllLines(@"sample.csv").Select(l =&gt; l.Split(',')); var merged = HouseholdMerge(source, 0, 1, new[] {1, 2, 3, 4, 5}); public static IEnumerable&lt;string[]&gt; HouseholdMerge(IEnumerable&lt;string[]&gt; data, int fnIndex, int lnIndex, int[] groupIndexes) { Func&lt;string[], string&gt; groupby = fields =&gt; String.Join("", fields.Where((f, i) =&gt; 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() &gt; 2) { result[fnIndex] = "The"; result[lnIndex] += " Family"; } yield return result; } } </code></pre> <p>I don't like how I've had to do the groupby delegate. I'd like if C# had some way to convert a string expression to a delegate. e.g. Func groupby = f => "f[2] + f[3] + f[4] + f[5] + f[1];" I have a feeling something like this can probably be done in Lisp or Python. I look forward to seeing nicer implementation in other languages.</p> <p>Edit: Where did the community wiki checkbox go? Some mod please fix that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload