cha*_*rit 42 c# linq lambda functional-programming
看到一篇关于C#中隐藏功能的帖子,但很多人都没有写过linq/lambdas的例子......所以......我想知道......
你见过/写过的C#LINQ和/或Lambdas /匿名代表的最酷(最优雅)是什么?
奖金,如果它也投入生产!
Chr*_*man 19
一些基本功能:
public static class Functionals
{
// One-argument Y-Combinator.
public static Func<T, TResult> Y<T, TResult>(Func<Func<T, TResult>, Func<T, TResult>> F)
{
return t => F(Y(F))(t);
}
// Two-argument Y-Combinator.
public static Func<T1, T2, TResult> Y<T1, T2, TResult>(Func<Func<T1, T2, TResult>, Func<T1, T2, TResult>> F)
{
return (t1, t2) => F(Y(F))(t1, t2);
}
// Three-arugument Y-Combinator.
public static Func<T1, T2, T3, TResult> Y<T1, T2, T3, TResult>(Func<Func<T1, T2, T3, TResult>, Func<T1, T2, T3, TResult>> F)
{
return (t1, t2, t3) => F(Y(F))(t1, t2, t3);
}
// Four-arugument Y-Combinator.
public static Func<T1, T2, T3, T4, TResult> Y<T1, T2, T3, T4, TResult>(Func<Func<T1, T2, T3, T4, TResult>, Func<T1, T2, T3, T4, TResult>> F)
{
return (t1, t2, t3, t4) => F(Y(F))(t1, t2, t3, t4);
}
// Curry first argument
public static Func<T1, Func<T2, TResult>> Curry<T1, T2, TResult>(Func<T1, T2, TResult> F)
{
return t1 => t2 => F(t1, t2);
}
// Curry second argument.
public static Func<T2, Func<T1, TResult>> Curry2nd<T1, T2, TResult>(Func<T1, T2, TResult> F)
{
return t2 => t1 => F(t1, t2);
}
// Uncurry first argument.
public static Func<T1, T2, TResult> Uncurry<T1, T2, TResult>(Func<T1, Func<T2, TResult>> F)
{
return (t1, t2) => F(t1)(t2);
}
// Uncurry second argument.
public static Func<T1, T2, TResult> Uncurry2nd<T1, T2, TResult>(Func<T2, Func<T1, TResult>> F)
{
return (t1, t2) => F(t2)(t1);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您不知道如何使用它们,请不要做太多好事.为了了解这一点,您需要知道它们的用途:
gjv*_*amp 17
到目前为止,我遇到的最令人印象深刻的Linq实现是Brahma框架.
它可以用于使用"Linq to GPU"将并行计算卸载到GPU.您在linq中编写了一个"查询",然后Brahma将其转换为HLSL(高级着色器语言),以便DirectX可以在GPU上处理它.
这个网站只允许我粘贴一个链接,所以尝试从dotnetrocks这个网络广播:
http://www.dotnetrocks.com/default.aspx?showNum=466
另外谷歌为Brahma项目,你会得到正确的页面.
非常酷的东西.
GJ
| 归档时间: |
|
| 查看次数: |
12843 次 |
| 最近记录: |