C#
public static string CommaSeparate(this IEnumerable source, Func func) { return string.Join(",", source.Select(s => func(s).ToString()).ToArray()); }
I appreciate that a Lambda expression matches a Func delegate in terms of Input maps to Output.
So why does the Lambda in the Select method show s => func(s).ToString(); rather than s => s.ToString();
Thanks
VulpesRank #55 · 29.2K reputationPosted Mar 28, 2013, 3:03 PM EDT
It's transforming the members first using Func and then comma separating the results.
Here's a simple example which takes an array of ints, doubles them and then comma separates them:
The output is:
2,4,6
Guest UserRank #4,868 · 321 reputationPosted Mar 29, 2013, 1:07 AM EDT
The Type parameters for Func
So given s => func(s).ToString();
Why does s hold and pass User to Func
VulpesRank #55 · 29.2K reputationPosted Mar 29, 2013, 1:51 AM EDT
Guest UserRank #4,868 · 321 reputationPosted Mar 29, 2013, 4:48 AM EDT
Do you know of any other examples in or out of the .NET Framework that use the Lambda mapping syntax:-
(Input) => someMethod(Input) i.e s => func(s);
Is the above only possible due to the Lambda being used within the Select method in which itself has a Func delegate?
As opposed to the more common form when invoking a Func delegate with a Lambda?
(Input) => (Output\Result) i.e s => s;
Regards
VulpesRank #55 · 29.2K reputationPosted Mar 29, 2013, 7:16 AM EDT
http://msdn.microsoft.com/en-us/library/system.linq.enumerable_methods(v=vs.100).aspx
As you'll see from this list Select is one of them.
Of course, the Func delegate is completely general and if you use Func
Guest UserRank #4,868 · 321 reputationPosted Mar 29, 2013, 7:39 AM EDT
I thought that syntax was, input goes to result? which corresponds\maps
with Func
VulpesRank #55 · 29.2K reputationPosted Mar 29, 2013, 8:36 AM EDT
However, the point I was trying to make is that if the input and output types are the same, this doesn't necessarily mean that the input parameter has to be transformed in some way; it can simply be passed out again unmodified. The position would then be similar to having a parameter of type T rather than Func
The use of Func
Guest UserRank #4,868 · 321 reputationPosted Mar 29, 2013, 11:19 AM EDT
What threw me was the usage of the Func
And the fact that the method then uses the Select Extension method which again takes Func
There are a lot of Func delegates flying about in this one method which confused me...
I now appreciate that (s) => func(s).ToString(); is the Lambda expression used in the Select Extension method's parameter to correspond\map to the func parameter of the CommaSeperate Extension method.
PS I assume the Type of TResult that is inferred to be of Type int, is hidden from view? as id expect to see it before the call to the ToString() method!?
(s) => func(s).ToString();
Regards...
VulpesRank #55 · 29.2K reputationPosted Mar 30, 2013, 12:44 PM EDT
So, if you have:
s => func(s).ToString()
the compiler infers TResult to be string.
Guest UserRank #4,868 · 321 reputationPosted Mar 31, 2013, 4:51 AM EDT
One last question on this topic please if I may...
I found an example of:-
public static List
Guest UserRank #4,868 · 321 reputationPosted Apr 1, 2013, 4:23 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 1, 2013, 5:12 AM EDT
The Select method is not bothered whether you pass it an actual delegate or a lambda expression because the latter is always convertible to a compatible delegate.