Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the C#-idiomatic way for applying an operator across two lists?

Tags:

c#

list

I'm used to doing this (from other languages):

 a = 1, 2, 3;  b = 5, 1, 2;   c = a * b;  // c = 5, 2, 6 

This takes two lists of equal size and applies a function to their members, one at a time, to get a list of the results. It could be a function as simple as multiplication (above) or something more complex:

 c = b>a ? b-a : 0;  // c = 4, 0, 0 

I can think of a few different ways to do this in C#, but I'm not sure how a C#-trained programmer would do it. What's the proper way of going about this in the C# world?

(The only part I'm asking about is where c = f(a,b). I'm familiar with creating lists and accessing their elements.)

like image 447
Joe Avatar asked Jun 03 '14 23:06

Joe


People also ask

What is meant by C?

noun plural c's, C's or Cs. the third letter and second consonant of the modern English alphabet. a speech sound represented by this letter, in English usually either a voiceless alveolar fricative, as in cigar, or a voiceless velar stop, as in case.

What is C for computer?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners.

What is C in simple language?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C and why it is used?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

var c = a.Zip(b, (x, y) => x * y); 

For the more complex one after your edit:

var c = a.Zip(b, (x, y) => x > y ? x - y : 0); 

Note that Zip is an extension method both from Enumerable that acts on IEnumerable<T> and from Queryable that acts on IQueryable<T>, so it is possible that, should the lambda be one a given query provider can deal with, that it could be processed as a SQL query on a database, or some other way other than in-memory in .NET.

Someone mentioned that this was new with 4.0 in the comments. It's not hard to implement for 3.5 yourself:

public class MyExtraLinqyStuff {     public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)     {       //Do null checks immediately;       if(first == null)         throw new ArgumentNullException("first");       if(second == null)         throw new ArgumentNullException("second");       if(resultSelector == null)         throw new ArgumentNullException("resultSelector");       return DoZip(first, second, resultSelector);     }     private static IEnumerable<TResult> DoZip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)     {       using(var enF = first.GetEnumerator())       using(var enS = second.GetEnumerator())         while(enF.MoveNext() && enS.MoveNext())           yield return resultSelector(enF.Current, enS.Current);     } } 

For .NET2.0 or .NET3.0 you can have the same, but not as an extension method, which answers another question from the comments; there wasn't really an idiomatic way of doing such things in .NET at that time, or at least not with a firm consensus among those of us coding in .NET then. Some of us had methods like the above in our toolkits (though not extension methods obviously), but that was more that we were influenced by other languages and libraries than anything else (e.g. I was doing things like the above because of stuff I knew from C++'s STL, but that was hardly the only possible source of inspiration)

like image 63
Jon Hanna Avatar answered Oct 05 '22 23:10

Jon Hanna