Created
June 27, 2012 03:32
-
-
Save hachibeeDI/3001208 to your computer and use it in GitHub Desktop.
on C#, demonstration of lambda and lexical scope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// map自体は、.NETでは.select拡張メソッドとして提供されているかんじっぽい? | |
// old | |
static int[] | |
squarefunc(int[] lis) | |
{ | |
for(var i = 0; i <= lis.Count() -1; ++i) | |
{ | |
lis[i] = lis[i] * lis[i]; | |
} | |
return lis; | |
} | |
// plain | |
static IEnumerable<T> | |
map<T>(IEnumerable<T> lis, Func<T,T> func) | |
{ | |
var result = new List<T>(); | |
foreach (T val in lis ){ result.Add(func(val)); }; | |
return result; | |
} | |
// closure | |
static Func<IEnumerable<T>, IEnumerable<T>> | |
clomap<T>(Func<T,T> func) | |
{ | |
Func<IEnumerable<T>, IEnumerable<T>> f = | |
(IEnumerable<T> lis) => | |
{ | |
//ここの部分は一行で書くとlis.select(func)になる | |
var result = new List<T>(); | |
foreach(T val in lis) {result.Add(func(val));}; | |
return result; | |
}; | |
return f; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment