函数式编程
一个函数输出当做另一个函数输入。有时候一个复杂问题,我们拆分成很多个步骤函数,这些函数组合起来调用解决一个复杂问题。
在C#中不支持函数组合,但可以直接像这样调用B(A(n)),这也是函数组合,但这不利于阅读,人们习惯从左往右阅读,而不是相反的方向。通过创建扩展方法可以任何组合两个函数,像下面这样
Func<A,C> Compose<A,B,C>(this Func<A.B> f ,Func<B,C> g)=>(n)=>g(f(n))
"htmlcode">
letadd4x=x+4 letmulitply3x=x*3 letlist=[0..10] letnewList=List.map(funx->mulitply3(add4(x)))list letnewList2=list|>List.map(add4mulitply3
在F#中使用中缀运算符来使函数组合可以从左到右阅读,更加精炼、简洁。
闭包的应用
闭包可以让函数访问其所在的外部函数中的参数和变量,即使在其外部函数被返回之后。在js中经常会出现闭包的场景,在C#和F#中,编译器使用闭包来增加和扩展变量的范围。
C#在.NET2.0后引入闭包。在lambda和匿名方法中得到充分的使用。像下面的匿名函数引用变量a,访问和管理变量a的状态。如果不用闭包,就需要额外创建一个类函数来调用。
strings="freevariable"; Func<string,string>lambda=value=>a+""+value;
以下载图片更新窗体PictureBox控件为例:
void UpdateImage(string url)
{
System.Windows.Forms.PictureBox picbox = this.pictureBox1;
var client = new WebClient();
client.DownloadDataCompleted += (o, e) =>
{
if (picbox != null)
{
using (var ms = new MemoryStream(e.Result))
{
picbox.Image = Image.FromStream(ms);
}
}
};
client.DownloadDataAsync(new Uri(url));
//picbox = null;
}
因为是异步下载,UPdateImage方法返回后,图片还未下载完成,但picbox变量仍然可以使用。这就是变量捕获。lambda表达式捕获了局部变量image,因此它仍停留在作用域中。但捕获的变量值是在运行时确定的,而不是在捕获时,最后一句如果放开,将不能更新窗体。运行时picbox为null了,在F#中不存在null的概念,所以也不会出现此类错误。
多线程环境中的闭包使用。猜测下面的代码运行结果如何?
for (int i = 1; i < 10; i++)
{
Task.Factory.StartNew(()=>Console.WriteLine("{0}-{1}",
Thread.CurrentThread.ManagedThreadId,i));
}
"1612877378702E" style="display: none;">"htmlcode">
//简单的函数缓存
public static Func<T, R> Memoize<T, R>(Func<T, R> func) where T : IComparable
{
Dictionary<T, R> cache = new Dictionary<T, R>();
return arg =>
{
if (cache.ContainsKey(arg))
return cache[arg];
return (cache[arg] = func(arg));
};
}
// 线程安全的函数缓存
public static Func<T, R> MemoizeThreadSafe<T, R>(Func<T, R> func) where T : IComparable
{
ConcurrentDictionary<T, R> cache = new ConcurrentDictionary<T, R>();
return arg => cache.GetOrAdd(arg, a => func(a));
}
// 利用延迟提高性能的函数缓存
public static Func<T, R> MemoizeLazyThreadSafe<T, R>(Func<T, R> func) where T : IComparable
{
ConcurrentDictionary<T, Lazy<R cache = new ConcurrentDictionary<T, Lazy<R();
return arg => cache.GetOrAdd(arg, a => new Lazy<R>(() => func(a))).Value;
}
上述示例代码中有三个版本的函数记忆化。调用像下面这样
public static string Greeting(string name)
{
return $"Warm greetings {name}, the time is {DateTime.Now.ToString("hh:mm:ss")}";
}
public static void RunDemoMemoization()
{
var greetingMemoize = Memoize<string, string>(Greeting);
Console.WriteLine(greetingMemoize("Richard"));
Console.WriteLine(greetingMemoize("Paul"));
Console.WriteLine(greetingMemoize("Richard"));
}
线程安全字典ConcurrentDictionary可以保证只向集合里添加一个相同值,但函数求值可能会被执行多次,所以利用.NET4之后的延迟对象加载技术。在真正需要使用对象时候才去实例化(通过访问延迟对象的Value属性),而且是线程安全的。