简体   繁体   English

F#函数与C#“Func”tions

[英]F# functions vs C# “Func”tions

So I ran across this tour of F#: https://docs.microsoft.com/en-us/dotnet/articles/fsharp/tour 所以我遇到了这个F#之旅: https//docs.microsoft.com/en-us/dotnet/articles/fsharp/tour

... and boy howdy is F# interesting! ...和男孩你好F#有趣! The very beginning of the tour defined a sample function, which looks pretty straightforward: 游览的一开始就定义了一个示例函数,它看起来非常简单:

/// You use 'let' to define a function. This one accepts an integer argument and returns an integer. 
/// Parentheses are optional for function arguments, except for when you use an explicit type annotation.
let sampleFunction1 x = x*x + 3

So this makes sense to me. 所以这对我来说很有意义。 It defines what the function is so if I were to pass some number into this thing, it squares it and adds 3 to that result, as seen by the next line in the tour: 它定义了函数是什么,所以如果我将一些数字传递给这个东西,它会将它平方并为该结果增加3,如巡视中的下一行所示:

 /// Apply the function, naming the function return result using 'let'. 
/// The variable type is inferred from the function return type.
let result1 = sampleFunction1 4573

After giving this a few more minutes of thought, I came up with the conclusion that C# can do this too! 经过几分钟的思考后,我得出结论,C#也可以做到这一点! I sure do love C# a whole lot. 我确实非常喜欢C#。 This is what the above would look like in C# as far as I can tell: 就我所知,这就是上面在C#中的样子:

        Func<int, int> sampleFunction1 = x => x*x + 3;
        var result = sampleFunction1(4573);

So my main question is, what is the difference between what I wrote in C# and what the F# tour showed me? 所以我的主要问题是,我在C#中写的内容与F#tour给我的内容有什么区别? Sub-questions are: Is the IL code any different even though it's the same CLR? 子问题是:IL代码是否有任何不同,即使它是相同的CLR? What are a few reasons I would use F# over C#? 我会使用F#而不是C#的几个原因是什么?

Technically, these are equivalent. 从技术上讲,这些是等价的。 The IL might be a tad different, just because these are different compilers, but not much. IL可能有点不同,只是因为这些是不同的编译器,但并不多。 In essence these are compiled in the same way. 实质上,这些是以相同的方式编译的。

But C# can't do exactly that. 但是C#不能做到一点。 Did you notice how you had to write Func<int,int> in front? 您是否注意到如何在前面编写Func<int,int> But that's just a very small toy function. 但这只是一个非常小的玩具功能。 What would happen in more practical cases? 在更实际的情况下会发生什么? Observe: 注意:

// F#
let f x m = Map.find (x, x+1) m |> Seq.map ((+) 1)

// C#
Func<int, IDictionary<Tuple<int, int>, IEnumerable<int>>, IEnumerable<int>> f = (x, m) => m[Tuple.Create(x, x+1)].Select( i => i+1 );

Fun, isn't it? 好玩,不是吗?
This is called "type inference". 这称为“类型推断”。 As in, F# is able to infer types of stuff based on how stuff is used. 如同,F#能够根据使用的东西推断出类型的东西。 You can almost write a full program and never once use a type annotation. 你几乎可以写一个完整的程序,从来没有使用过类型注释。 C# has this too, to some limited extent. C#在某种程度上也有这个。 That's how I'm able to call .Select( i => i+1 ) , and C# knows that i is int , because whatever came before .Select was IEnumerable<int> . 这就是我能够调用的方式.Select( i => i+1 ) ,C#知道iint ,因为之前发生的事情.SelectIEnumerable<int> But it's very limited, not nearly as powerful. 但它非常有限,而不是那么强大。

Type inference is just one of the many benefits of F#. 类型推断仅仅是F#的众多优势之一 I picked it, because you were looking right at it and not seeing it. 我选择了它,因为你看着它并没有看到它。 But there are many more. 但还有更多。 Order of compilation, lack of nulls, immutability by default, algebraic data types, automatic currying and partial application... Much more, in fact, than will fit in a SO answer. 编译顺序,缺少空值,默认不变,代数数据类型,自动currying和部分应用......实际上,还要比SO答案更合适。

Those who wish to discover the wonderful and exciting world of functional programming in general and F# in particular, I usually send right off to https://fsharpforfunandprofit.com/ , into Mr. Wlaschin's kind and capable hands. 那些希望发现功能性编程的奇妙和令人兴奋的世界的人,特别是F#,我通常会直接发送到https://fsharpforfunandprofit.com/ ,进入Wlaschin先生亲切而有能力的人手中。 A wonderful resource, read it all. 一个美妙的资源,阅读所有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM