F#之旅1 - Why use F#?为什么要用F#?

时间:2021-05-07 16:28:06

原文地址:http://fsharpforfunandprofit.com/why-use-fsharp/

Why use F#?
Why you should consider using F# for your next project

为什么要用F#?
为什么你应该考虑在下一个项目使用F#

Although F# is great for specialist areas such as scientific or data analysis, it is also an excellent choice for enterprise development. Here are five good reasons why you should consider using F# for your next project.
F#不仅仅在科学和数据分析这类专业领域很适用,对于企业开发来说它也是个很好的选择。以下是为什么你应该考虑在下一个项目使用F#的五个原因。

Conciseness
F# is not cluttered up with coding "noise" such as curly brackets, semicolons and so on.
You almost never have to specify the type of an object, thanks to a powerful type inference system.
And, compared with C#, it generally takes fewer lines of code to solve the same problem.
简洁
F#不会被大括号、分号等代码“杂音”弄得乱七八糟。
多亏强大的类型推导系统,你几乎不需要指定对象的具体类型。
并且,相对于C#,它通常只用更少的代码就能解决同样的问题。

Convenience
Many common programming tasks are much simpler in F#. This includes things like creating and using complex type definitions, doing list processing, comparison and equality, state machines, and much more.
And because functions are first class objects, it is very easy to create powerful and reusable code by creating functions that have other functions as parameters, or that combine existing functions to create new functionality.
方便
一些常见的编程任务在F#里非常简单。例如:创建和使用一个复杂的自定义类型,处理列表,比较,状态机等等。
并且,由于函数是"第一等公民",所以可以通过创建函数的方式,来轻松的编写强大可重用的代码,创建函数时,可以用其它函数作为参数,或者合并已有的函数来创造新的功能。

Correctness
F# has a powerful type system which prevents many common errors such as null reference exceptions.
Values are immutable by default, which prevents a large class of errors.
In addition, you can often encode business logic using the type system itself in such a way that it is actually impossible to write incorrect code or mix up units of measure, greatly reducing the need for unit tests.
正确
F#有一个强大的类型系统,可以预防例如空指针应用之类的常见错误。
值默认是不可变的,也可以预防一大类的错误。
此外,你还可以使用类型系统本身对业务逻辑进行编码,这样就不可能写出不正确的代码,或者混淆度量单位,大大降低了对单元测试的需求。

Concurrency
F# has a number of built-in libraries to help when more than one thing at a time is happening. Asynchronous programming is very easy, as is parallelism. F# also has a built-in actor model, and excellent support for event handling and functional reactive programming.
And of course, because data structures are immutable by default, sharing state and avoiding locks is much easier.
并发
F#有一些内置的库来帮助处理多件事同时发生的情况。因为并发,异步编程也很简单。F#还内置了一个actor模型,对事件处理和反射式编程也有很好的支持。
当然,因为默认情况下数据结构是不可变的,共享状态和避免锁也容易得多了。

Completeness
Although it is a functional language at heart, F# does support other styles which are not 100% pure, which makes it much easier to interact with the non-pure world of web sites, databases, other applications, and so on. In particular, F# is designed as a hybrid functional/OO language, so it can do virtually everything that C# can do.
Of course, F# is part of the .NET ecosystem, which gives you seamless access to all the third party .NET libraries and tools. It runs on most platforms, including Linux and smart phones (via Mono).
Finally, it is well integrated with Visual Studio, which means you get a great IDE with IntelliSense support, a debugger, and many plug-ins for unit tests, source control, and other development tasks. Or on Linux, you can use the MonoDevelop IDE instead.
完备
尽管F#的核心是函数式编程语言,但是它并不是百分百的纯函数式,F#支持其它的编程范式,这样可以让它更方便的与web网站、数据库、其它应用等进行交互。特别是F#被设计成一个混合性/面向对象的编程语言,所以它几乎可以做所有C#能做的事。
当然,作为.NET生态系统的一部分,F#可以无缝连接所有的第三方.NET库和工具。他可以运行在包括Linux和智能设备在内的大部分系统平台。
最后,它与VS高度集成,意味着你可以得到强悍的智能感知,调试器,以及各种单元测试、代码版本控制之类的插件等等。在Linux平台,你也可以使用MonoDevelop来作为IDE。