楼主: franky_sas
1446 6

Scala Functional Programming Patterns [推广有奖]

  • 3关注
  • 18粉丝

学术权威

23%

还不是VIP/贵宾

-

威望
0
论坛币
64049 个
通用积分
153.6027
学术水平
65 点
热心指数
98 点
信用等级
41 点
经验
69355 点
帖子
9165
精华
0
在线时间
2477 小时
注册时间
2016-5-17
最后登录
2023-6-13

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币


Grok and perform effective functional programming in Scala

About This Book
  • Understand functional programming patterns by comparing them with the traditional object-oriented design patterns
  • Write robust, safer, and better code using the declarative programming paradigm
  • An illustrative guide for programmers to create functional programming patterns with Scala

Who This Book Is ForIf you have done Java programming before and have a basic knowledge of Scala and its syntax, then this book is an ideal choice to help you to understand the context, the traditional design pattern applicable, and the Scala way. Having previous knowledge of design patterns will help, though it is not strictly necessary.

What You Will Learn
  • Get to know about functional programming and the value Scala's FP idioms bring to the table
  • Solve day-to-day programming problems using functional programming idioms
  • Cut down the boiler-plate and express patterns simply and elegantly using Scala's concise syntax
  • Tame system complexity by reducing the moving parts
  • Write easier to reason about concurrent code using the actor paradigm and the Akka library
  • Apply recursive thinking and understand how to create solutions without mutation
  • Reuse existing code to compose new behavior
  • Combine the object-oriented and functional programming approaches for effective programming using Scala

In DetailScala is used to construct elegant class hierarchies for maximum code reuse and extensibility and to implement their behavior using higher-order functions. Its functional programming (FP) features are a boon to help you design “easy to reason about” systems to control the growing software complexities. Knowing how and where to apply the many Scala techniques is challenging. Looking at Scala best practices in the context of what you already know helps you grasp these concepts quickly, and helps you see where and why to use them.
This book begins with the rationale behind patterns to help you understand where and why each pattern is applied. You will discover what tail recursion brings to your table and will get an understanding of how to create solutions without mutations. We then explain the concept of memorization and infinite sequences for on-demand computation. Further, the book takes you through Scala's stackable traits and dependency injection, a popular technique to produce loosely-coupled software systems.
You will also explore how to currying favors to your code and how to simplify it by de-construction via pattern matching. We also show you how to do pipeline transformations using higher order functions such as the pipes and filters pattern. Then we guide you through the increasing importance of concurrent programming and the pitfalls of traditional code concurrency. Lastly, the book takes a paradigm shift to show you the different techniques that functional programming brings to your plate.
This book is an invaluable source to help you understand and perform functional programming and solve common programming problems using Scala's programming patterns.

Style and approach This is a hands-on guide to Scala's game-changing features for programming.
It is filled with many code examples and figures that illustrate various Scala idioms and best practices.


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Programming Functional function Patterns Pattern comparing effective knowledge design before

Scala Functional Programming Patterns.pdf

3.22 MB

需要: 5 个论坛币  [购买]

已有 2 人评分经验 论坛币 学术水平 收起 理由
Nicolle + 100 + 100 精彩帖子
ReneeBK + 100 + 1 精彩帖子

总评分: 经验 + 200  论坛币 + 100  学术水平 + 1   查看全部评分

本帖被以下文库推荐

沙发
ReneeBK 发表于 2017-1-11 08:08:48 |只看作者 |坛友微信交流群
  1. Singletons – being one and only one

  2. A singleton is a class of which only a single instance can exist. How do we prevent anyone from creating yet another instance? The solution is to make the constructor inaccessible. Here it is:

  3. public class Singleton {
  4.       // Eager initialization
  5.   private static final Singleton instance = new Singleton(); // 1

  6.   private Singleton() { // 2
  7.   /* client code cannot create instance */
  8. }

  9.       // Static factory method
  10. public static Singleton getInstance() { // 3
  11.   return instance;
  12. }

  13. // Driver code
  14. public static void main(String[] args) {
  15.   System.out.println(Singleton.getInstance());
  16.   System.out.println(Singleton.getInstance());
  17. }
  18. }
  19. Dissecting the code:

  20. At 1, the static initializer creates the instance—also the final keyword ensures that the instance cannot be redefined.
  21. At 2, the constructor access is private, so only the class methods can access it.
  22. At 3, the public factory method gives access to the client code.
复制代码

使用道具

藤椅
ReneeBK 发表于 2017-1-11 11:39:20 |只看作者 |坛友微信交流群
  1. Null Objects – the Scala way

  2. Instead of using null to indicate missing values, Scala provides an Option class to represent optional values. Option can hold either Some value or None value. None indicates missing values. Please see http://www.scala-lang.org/api/current/index.html#scala.Option.

  3. So instead of nulls, we return None. To see why, try the following code snippets in REPL:

  4. scala> val m = List(Some("on"), None, Some("us"), None)

  5. m: List[Option[String]] = List(Some(on), None, Some(us), None)
  6. scala> for {

  7.      |     Some(p) <- m

  8.      | } println(p)

  9. on

  10. us

  11. scala> val m = List(Some("on"), None, Some("us"), None)

  12. m: List[Option[String]] = List(Some(on), None, Some(us), None)

  13. scala> val p = m.flatten

  14. p: List[String] = List(on, us)
复制代码

使用道具

板凳
ReneeBK 发表于 2017-1-11 11:40:48 |只看作者 |坛友微信交流群
  1. Scala singletons

  2. Scala has singleton objects called companion objects. A companion object is an object with the same name as a class. A companion object also can access private methods and fields of its companion class. Both a class and its companion object must be defined in the same source file. The companion object is where the apply() factory method may be defined. Let's have a look at the following example of a companion class:

  3. class Singleton {  // Companion class
  4.   def m() {
  5.     println("class")
  6.   }         
  7. }
  8. And then its companion object as:

  9. object Singleton { // Companion Object
  10.   def m() {
  11.     println("companion")
  12.   }         
  13. }
复制代码

使用道具

报纸
ReneeBK 发表于 2017-1-11 11:43:10 |只看作者 |坛友微信交流群
  1. The apply() factory method

  2. If a companion object defines an apply() method, the Scala compiler calls it when it sees the class name followed by (). So, for example, when Scala sees something like:

  3.                  Singleton(arg1, arg2, …, argN) // syntactic sugar
  4. It translates the call into:

  5.                  Singleton.apply(arg1, arg2,...,argN)
  6. Open a Scala console and enter the following:

  7. scala> val p = Map("one" -> 1, "two" -> 2)
  8. p: scala.collection.immutable.Map[String,Int] = Map(one -> 1, two -> 2)
  9. When we say Map("one" -> 1, "two" -> 2), it seems like we are calling a function named Map with the arguments—however, this form is just syntactic sugar. Under the hood, Map has a companion object whose apply method is being called, then the apply() method creates the Map object and assigns it to p.
复制代码

使用道具

地板
ReneeBK 发表于 2017-1-11 11:45:23 |只看作者 |坛友微信交流群
Builders
  1. public class UsedCar {
  2. private String make;
  3. private String model;
  4. private int kmDriven;
  5. private int yearOfManufacturing;

  6. private boolean hasGps;
  7. private boolean hasAc;
  8. private boolean hasAirBags;
  9. private boolean hasAbs;
  10.       // Setters/Getters not shown
  11. }

  12. public class UsedCarBuilder {

  13. private final UsedCar car;

  14. public UsedCarBuilder() {
  15.   car = new UsedCar();
  16. }

  17. public UsedCarBuilder hasAirBags(final boolean b) {
  18.   car.setHasAirBags(b);
  19.   return this;
  20. }

  21. public UsedCarBuilder hasAbs(final boolean b) {
  22.   car.setHasAbs(b);
  23.   return this;
  24. }

  25. public UsedCarBuilder hasAc(final boolean b) {
  26.   car.setHasAc(b);
  27.   return this;
  28. }

  29. public UsedCarBuilder hasGps(final boolean b) {
  30.   car.setHasGps(b);
  31.   return this;
  32. }

  33. public UsedCarBuilder yearOfManufacturing(final int year) {
  34.   car.setYearOfManufacturing(year);
  35.   return this;
  36. }

  37. public UsedCarBuilder kmDriven(final int km) {
  38.   car.setKmDriven(km);
  39.   return this;
  40. }

  41. public UsedCarBuilder model(final String itsModel) {
  42.   car.setModel(itsModel);
  43.   return this;
  44. }

  45. public UsedCarBuilder make(final String itsMake) {
  46.   car.setMake(itsMake);
  47.   return this;
  48. }

  49. public UsedCar build() {
  50.   // set sensible defaults for optional attributes - gps, ac, airbags, abs
  51.   // check make and model are consistent
  52.   // check year of manufacturing is sensible
  53.   // check kmDriven is not negative
  54.   return car;
  55. }
  56. }

  57. public class Driver {
  58. public static void main(String[] args) {
  59.   // Note the method chaining
  60.   UsedCar car = new UsedCarBuilder().make("Maruti").model("Alto")
  61. .kmDriven(10000).yearOfManufacturing(2006).hasGps(false)
  62. .hasAc(false).hasAbs(false).hasAirBags(false).build();
  63.   System.out.println(car);
  64. }
  65. }
  66. We design it like this for method chaining. Note the build() method of UsedCarBuilder. We get one place where we can check all the parameters rigorously before we return the UsedCar object. Making sure all fields satisfy the preconditions ensures the client code is not violating the contract.
复制代码

使用道具

7
HappyAndy_Lo 发表于 2017-1-11 13:40:08 |只看作者 |坛友微信交流群

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 17:53