挺有意思 实验: Func<int, Func<int, int>> add = x => (y => x + y); int n = 0; var i = add(add(n++)(n++))(add(n++)(n++)); //6 n = 0; var j = add(add(++n)(++n))(add(++n)(++n)); //10
希望下一版可以支持。 目前来说可以定义一些辅助定义稍微简化一下写法:public delegate Func<T2, TResult> Curry<in T1, in T2, out TResult>(T1 x); public delegate Curry<T2, T3, TResult> Curry<in T1, in T2, in T3, out TResult>(T1 x);Curry<int, int, int> add = x => y => x + y; // 表示两个整型参数,一个整型返回值 int i = add(3)(4); Console.WriteLine(i);Curry<int, Curry<int, int, int>, int, int> eval = x => y => z => y(x)(z); // 表示三个参数,第一、第三为整数,中间为接受两个整数的curry function i = eval(3)(add)(5); Console.WriteLine(i);
在linq in action里面见过这种写法, 当时就觉得碉堡了, using System; using System.Collections.Generic; using System.Diagnostics;internal static class LanguageFeatures { private static void DisplayProcesses( Func<Process , Boolean> match) { var processes = new List< Object>(); foreach (var process in Process.GetProcesses()) { if (match(process)) { processes.Add( new { process.Id, Name = process.ProcessName, Memory = process.WorkingSet64 }); } } ObjectDumper.Write(processes); //打印里面的内容,别处实现的 } private static void Main( string[] args) { DisplayProcesses(process => process.WorkingSet64 >= 20*1024*1024); } }因为这完全是对函数的粒度进行了重新划分, 将原来普通强类型语言的最小功能粒度--函数--进一步细化,这对于架构的设计完全是颠覆式的啊。 原来就叫做“ Currying ”么?
如果从面向过程的角度来解读,肯定是很头疼的。 但如果以函数的角度来看,其实还好 比如 Func<int, Func<Func<int, Func<int, int>>, Func<int, int>>> eval = x => new Func<Func<int, Func<int, int>>, Func<int, int>>( y => new Func<int, int>(z => y(x)(z))); Func<int, Func<int, int>> add = x => new Func<int, int>(y => x + y); 5个func依次为f1,f2,...f5. 那么就是 f1(x)=f2 f2(f3) = f5 f3(x) = f4 f4(x) = y f5(x) = y 其中f3 也就是代码中的y 就是一个运算规则 也就是后面的add
实验:
Func<int, Func<int, int>> add = x => (y => x + y);
int n = 0;
var i = add(add(n++)(n++))(add(n++)(n++)); //6
n = 0;
var j = add(add(++n)(++n))(add(++n)(++n)); //10
只是C#实现起来,如Func<int, Func<Func<int, Func<int, int>>, Func<int, int>>>类型看得令人郁闷;
{
return (delegate(int y)
{
return x + y;
});
}; Func<int, Func<Func<int, Func<int, int>>, Func<int, int>>> eval = delegate(int x)
{
return (delegate(Func<int, Func<int, int>> y)
{
return delegate(int z)
{
return y(x)(z);
};
});
};
这样是不是更易懂一些
希望下一版可以支持。
目前来说可以定义一些辅助定义稍微简化一下写法:public delegate Func<T2, TResult> Curry<in T1, in T2, out TResult>(T1 x);
public delegate Curry<T2, T3, TResult> Curry<in T1, in T2, in T3, out TResult>(T1 x);Curry<int, int, int> add = x => y => x + y; // 表示两个整型参数,一个整型返回值
int i = add(3)(4);
Console.WriteLine(i);Curry<int, Curry<int, int, int>, int, int> eval = x => y => z => y(x)(z);
// 表示三个参数,第一、第三为整数,中间为接受两个整数的curry function
i = eval(3)(add)(5);
Console.WriteLine(i);
当时就觉得碉堡了,
using System;
using System.Collections.Generic;
using System.Diagnostics;internal static class LanguageFeatures
{
private static void DisplayProcesses( Func<Process , Boolean> match)
{
var processes = new List< Object>();
foreach (var process in Process.GetProcesses())
{
if (match(process))
{
processes.Add( new
{
process.Id,
Name = process.ProcessName,
Memory = process.WorkingSet64
});
}
} ObjectDumper.Write(processes); //打印里面的内容,别处实现的
} private static void Main( string[] args)
{
DisplayProcesses(process => process.WorkingSet64 >= 20*1024*1024);
}
}因为这完全是对函数的粒度进行了重新划分,
将原来普通强类型语言的最小功能粒度--函数--进一步细化,这对于架构的设计完全是颠覆式的啊。
原来就叫做“ Currying ”么?
但如果以函数的角度来看,其实还好
比如
Func<int, Func<Func<int, Func<int, int>>, Func<int, int>>> eval =
x => new Func<Func<int, Func<int, int>>, Func<int, int>>(
y => new Func<int, int>(z => y(x)(z)));
Func<int, Func<int, int>> add = x => new Func<int, int>(y => x + y);
5个func依次为f1,f2,...f5.
那么就是
f1(x)=f2
f2(f3) = f5
f3(x) = f4
f4(x) = y
f5(x) = y
其中f3 也就是代码中的y 就是一个运算规则 也就是后面的add