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);
}
}
No, you cannot write multiple lines lambda in Python. The lambda functions can have only one expression.
No, you can't write multiline lambda in Python because the lambda functions can have only one expression. The creator of the Python programming language – Guido van Rossum, answered this question in one of his blogs. where he said that it's theoretically possible, but the solution is not a Pythonic way to do that.
Unlike an expression lambda, a statement lambda can contain multiple statements separated by semicolons. delegate void ModifyInt(int input); ModifyInt addOneAndTellMe = x => { int result = x + 1; Console. WriteLine(result); };
A multi-line lambda in F# is just
(fun args ->
lots
of
code
here
)
The whole code would be something like
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)
assuming I made no transcription errors. (I used F# function types rather than Action delegates in the public interface.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With