Skip to content

Instantly share code, notes, and snippets.

@hachibeeDI
Created June 27, 2012 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hachibeeDI/3001208 to your computer and use it in GitHub Desktop.
Save hachibeeDI/3001208 to your computer and use it in GitHub Desktop.
on C#, demonstration of lambda and lexical scope
// 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