c#函数式编程 Functional Programming in C# [6]

1.6 函数式编程的好处

  上一节展示了如何使用HOFs来避免重复,并实现更好的关注点分离。事实上,FP的优势之一就是它的简洁性:你可以用更少的代码行实现同样的结果。将其乘以一个典型应用中的数万行代码,简洁性对应用的可维护性也有积极的影响。
  通过应用你在本书中学到的功能技术,还有很多好处,它们大致可分为三类:

  • 更干净的代码 —— 除了前面提到的简洁性之外,FP还能使代码更有表现力,更易读,更易测试。 整洁的代码不仅是开发人员的智力享受,而且还能通过减少维护成本为企业带来巨大的经济效益。
  • 更好地支持并发性 —— 从多核CPU到分布式系统等几个因素,给你的应用程序带来了高度的并发性。并发在传统上与一些棘手的问题有关,如死锁、丢失更新等;FP提供的技术可以防止这些问题的发生。你会在第2章看到一个介绍性的例子,在本书的最后会看到更高级的例子。
  • 多范式方法 —— 他们说,如果你唯一的工具是一把锤子,每一个问题看起来都像一颗钉子。相反,你能从越多的角度来看待一个特定的问题,你就越有可能找到一个最佳的解决方案。如果你已经熟练掌握了OOP,学习不同的范式,如FP,将不可避免地给你带来更丰富的视角。 当面临一个问题时,你将能够考虑几种方法并选择最有效的。

练习
  我建议你花时间做这些练习,并顺便想出一些自己的想法。 GitHub上的代码样本库(https://github.com/la-yumba/functional-csharp-code)包括占位符,这样你就可以用最少的设置工作编写、编译和运行你的代码。它还包括一些解决方案,你可以对照这些解决方案检查你的结果:

  1. 浏览 System.Linq.Enumerable (https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable) 的方法。哪些是 HOF?你认为哪个意味着给定函数的迭代应用?
  2. 编写一个否定给定谓词的函数:只要给定的谓词评价为真,结果函数评价为假,反之亦然。
  3. 编写一个使用快速排序对 List< int > 进行排序的方法(返回一个新列表,而不是原地排序)。
  4. 将之前的实现概括为一个 List< T > 和一个 Comparison< T > 委托。
  5. 在本章中,你已经看到了一个 Using 函数,它接受一个 IDisposable 和一个 Func<TDisp,R> 类型的函数。编写使用 aFunc< IDisposable > 作为第一个参数而不是 IDisposable 的重载。 (这可用于避免某些代码分析工具引发关于实例化 IDisposable 而不是处置它的警告。)

总结

  • FP是一个强大的范式,可以帮助你使你的代码更加简洁、可维护、有表现力、健壮、可测试和并发友好。
  • FP与OOP的不同之处在于,它关注的是函数而不是对象,是数据转换而不是状态突变。
  • FP可以被看作是一个基于两个基本原理的技术集合:
    • 函数式第一公民
    • 应避免就地更新
  • C# 中的函数可以用方法、委托和 lambda 来表示。
  • FP利用高阶函数(将其他函数作为输入或输出的函数);因此,语言必须将函数作为第一公民。
Chapter 1, Tasting Functional Style in C#, introduces the functional programming approach by discussing its concepts and the comparison between functional and imperative programming. We also try to refactor a simple imperative code into a functional approach. Chapter 2, Walkthrough Delegates, covers the definition, syntax, and use of delegates. We also discuss the variance of delegates and the built-in delegate. Chapter 3, Expressing Anonymous Methods with Lambda Expressions, walks us through the concept of delegates and uses it to create and use an anonymous method. After we dig through the anonymous method, we can transform it into a lambda expression and then apply it to functional programming. Chapter 4, Extending Object Functionality with Extension Methods, elaborates the benefits of using the extension method in functional programming. Before that, we discuss the use of the extension method and also discuss how to get this new method in IntelliSense. Also, we try to invoke the extension method from other assemblies. Chapter 5, Querying Any Collection Easily with LINQ, enumerates the LINQ operator provided by C# and compares the two LINQ syntaxes: Fluent Syntax and Query Expression Syntax. We also discuss deferred execution in the LINQ process. Chapter 6, Enhancing the Responsiveness of the Functional Program with Asynchronous Programming, covers asynchronous programming for the functional approach. It will explain the Asynchronous Programming Model and the Task-based Asynchronous pattern. Chapter 7, Learning Recursion, explains the advantages of recursion over the loop sequence. We also discuss direct and indirect recursion in this chapter. Chapter 8, Optimizing the Code Using Laziness and Caching Techniques, covers the technique used to optimize the code in the functional approach. We talk about laziness thinking and the caching technique in order to optimize our code. Chapter 9, Working with Pattern, covers the advantages of using patterns compared to conventional switch-case operations. We discuss pattern matching and monad in this chapter. We use the pattern matching feature, which is the new feature provided by C# 7. Chapter 10, Taking an Action in C# Functional Programming, walks us through developing functional code based on given imperative code. We use our learning in the previous chapter to create an application using the functional approach. Chapter 11, Coding Best Practice and Testing the Functional Code, explains the best practice in the functional approach, including the creation of an honest signature and dealing with the side-effects. We also separate the code into domain logic and mutable shell and then test it using unit testing.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值