Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumeration with all elements of given Enumeration and intermitted other elements in LINQ

Tags:

c#

linq

I have a function f() and an IEnumerable<string> from which I want to create another IEnumerable<string> so that for each string s in the original enumeration the new one has s and f(s). The order of elements is important.

Example:

IEnumerable<string> given  = { "A",         "B",         "C"         };
IEnumerable<string> wanted = { "A", f("A"), "B", f("B"), "C", f("C") };

Is there a way to do this elegantly using LINQ? (And if not, what's the most elegant way to do this?)

like image 865
sbi Avatar asked Jan 22 '23 03:01

sbi


2 Answers

var wanted = given.SelectMany(s => new[] {s, f(s)} );

Enumerable.SelectMany projects each element of a sequence to another sequence and then flattens the resulting sequence of sequences into a single sequence.

In query syntax:

var wanted = from s in given
             from item in new[] {s, f(s)}
             select item;

Another alternative (if ordering were not important):

var wanted = given.Concat(given.Select(f)); // C# 4
var wanted = given.Concat(given.Select(s => f(s)));  // C# 3
like image 98
Ani Avatar answered Feb 07 '23 12:02

Ani


What about something like this:

public static IEnumerable<string> AddFuncCall(this IEnumerable<string> input)
{
  foreach (string s in input)
  {
    yield return s;
    yield return "f(" + s + ")";
  }
}

If you need the quotes embedded in the strings, try this (not 100% sure on syntax):

public static IEnumerable<string> AddFuncCall(this IEnumerable<string> input)
{
  foreach (string s in input)
  {
    yield return @""" + s + @""";
    yield return @"f("" + s + @"")";
  }
}

IEnumerable<string> given  = { "A", "B", "C"}; 
IEnumerable<string> wanted = given.AddFuncCall();

[EDIT] Sorry, I misread the question, I thought you wanted strings like "f("A")" in the output as opposed to executing f on each string from the input. Whoops!

like image 40
wageoghe Avatar answered Feb 07 '23 12:02

wageoghe