如何在F#中执行多行Lambda表达式? [英] How to do multiline lambda expressions in f#?
本文介绍了如何在F#中执行多行Lambda表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我该如何在F#中执行此操作(C#)
How would I do this (C#) in F#
public class MyClass
{
void Render(TextWriter textWriter)
{
Tag(() =>
{
textWriter.WriteLine("line 1");
textWriter.WriteLine("line 2");
});
Tag(value =>
{
textWriter.WriteLine("line 1");
textWriter.WriteLine(value);
}, "a");
}
public void Tag(Action action)
{
action();
}
public void Tag<T>(Action<T> action, T t)
{
action(t);
}
}
推荐答案
F#中的多行lambda就是
A multi-line lambda in F# is just
(fun args ->
lots
of
code
here
)
整个代码就像
open System.IO
type MyClass() as this =
let Render(tw : TextWriter) =
this.Tag(fun() ->
tw.WriteLine("line1")
tw.WriteLine("line2")
)
this.Tag(fun(value : string) ->
tw.WriteLine("line1")
tw.WriteLine(value)
, "a"
)
member this.Tag(action) =
action()
member this.Tag(action, x) =
action(x)
假设我没有任何转录错误. (我在公共接口中使用了F#函数类型,而不是Action委托.)
assuming I made no transcription errors. (I used F# function types rather than Action delegates in the public interface.)
这篇关于如何在F#中执行多行Lambda表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文