Csharp/CSharp Tutorial/LINQ/Func — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 18:31, 26 мая 2010
Содержание
- 1 Accumulator function
- 2 Add extension to Func
- 3 Build the necessary Func<> delegates using anonymous methods
- 4 Double recursive extension
- 5 Lambda Expression for Func
- 6 Lambda Expression to retrieve string length
- 7 Overloading By Delegate Return Type
- 8 Pass the delegates into the methods of Sequence
Accumulator function
<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Text;
class MainClass { static void Main() { Func<int, Func<int, int>> accumulatorGenerator = (n => (i => n += i+8)); var accumulator = accumulatorGenerator(1); Console.WriteLine(accumulator(1)); } }</source>
Add extension to Func
<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Text;
static class CurryingExtensions { public static Func<A, Func<B, R>> myExt<A, B, R>(this Func<A, B, R> f) { return a => b => f(a, b); } } class MainClass { static void Main() { Func<int, int, int> adder = (x, y) => (x + y); Func<int, Func<int, int>> c = adder.myExt(); Func<int, int> addsTwo = c(2); Console.WriteLine(addsTwo(5)); } }</source>
Build the necessary Func<> delegates using anonymous methods
<source lang="csharp">using System; using System.Collections.Generic; using System.Text; using System.Xml.Linq; using System.Linq;
class Program { static void Main(string[] args) { string[] currentVideoGames = {"test", "this is a test", "asdf", "game 1","game 2", "game 3"}; Func<string, bool> searchFilter = delegate(string game) { return game.Length > 6; }; Func<string, string> itemToProcess = delegate(string s) { return s; }; var subset = currentVideoGames .Where(searchFilter).OrderBy(itemToProcess).Select(itemToProcess); // Print out the results. foreach (var game in subset) Console.WriteLine("Item: {0}", game); } }</source>
Double recursive extension
<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Text;
static class DoubleApplicationExtensions { public static Func<T, T> ApplyTwice<T>(this Func<T, T> original) { return x => original(original(x)); } } class MainClass { static void Main() { Func<int, int> incrementer = x => x + x; Func<int, int> doubleIncrementer = incrementer.ApplyTwice(); Console.WriteLine(doubleIncrementer(5)); } }</source>
Lambda Expression for Func
<source lang="csharp">using System; using System.Collections.Generic; using System.Text; using System.ruponentModel;
class LambdaExpression { static void Main() { Func<int, int, string> func = (x, y) => (x * y).ToString(); Console.WriteLine(func(5, 20)); } }</source>
Lambda Expression to retrieve string length
<source lang="csharp">using System; using System.ruponentModel;
class MainClass { static void Main() { Func<string, int> returnLength = (string text) => { return text.Length; }; Console.WriteLine(returnLength("Hello")); } }</source>
Overloading By Delegate Return Type
<source lang="csharp">using System; using System.ruponentModel;
class MainClass { static void Execute(Func<int> action) { Console.WriteLine("action returns an int: " + action()); } static void Execute(Func<double> action) { Console.WriteLine("action returns a double: " + action()); } static void Main() { Execute(() => 1); } }</source>
Pass the delegates into the methods of Sequence
<source lang="csharp">using System; using System.Collections.Generic; using System.Text; using System.Xml.Linq; using System.Linq;
class Program { static void Main(string[] args) { string[] currentVideoGames = {"test", "this is a test", "asdf", "game 1", "game 2", "game 3"}; Func<string, bool> searchFilter = delegate(string game) { return game.Length > 6; }; Func<string, string> itemToProcess = delegate(string s) { return s; }; var subset = currentVideoGames .Where(searchFilter).OrderBy(itemToProcess).Select(itemToProcess); foreach (var game in subset) Console.WriteLine("Item: {0}", game); } }</source>