learning scala Function Composition andThen
- Ordering using andThen: f(x) andThen g(x) = g(f(x))
- Ordering using compose: f(x) compose g(x) = f(g(x))
println("Step 1: Assume a pre-calculated total cost amount")
val totalCost: Double =
println("\nStep 2: How to define a val function to apply discount to total cost")
val applyDiscountValFunction = (amount: Double) => {
println("Apply discount function")
val discount = // fetch discount from database
amount - discount
}
println("\nStep 3: How to call a val function")
println(s"Total cost of 5 donuts with discount = ${applyDiscountValFunction(totalCost)}")
println("\nStep 4: How to define a val function to apply tax to total cost")
val applyTaxValFunction = (amount: Double) => {
println("Apply tax function")
val tax = // fetch tax from database
amount + tax
}
println("\nStep 5: How to call andThen on a val function")
println(s"Total cost of 5 donuts = ${ (applyDiscountValFunction andThen applyTaxValFunction)(totalCost) }")
result:
Step : Assume a pre-calculated total cost amount Step : How to define a val function to apply discount to total cost Step : How to call a val function
Apply discount function
Total cost of donuts with discount = Step : How to define a val function to apply tax to total cost Step : How to call andThen on a val function
Apply discount function
Apply tax function
Total cost of donuts =
learning scala Function Composition andThen的更多相关文章
- learning scala Function Recursive Tail Call
可以使用scala库,可以从字面上看出是在调用 递归函数: code import scala.util.control.TailCalls._ val arrayDonuts: Array[Stri ...
- [machine learning] Loss Function view
[machine learning] Loss Function view 有关Loss Function(LF),只想说,终于写了 一.Loss Function 什么是Loss Function? ...
- [Javascript] Understand Function Composition By Building Compose and ComposeAll Utility Functions
Function composition allows us to build up powerful functions from smaller, more focused functions. ...
- [Ramda] Refactor a Promise Chain to Function Composition using Ramda
Promise chains can be a powerful way to handle a series of transformations to the results of an asyn ...
- learning scala How To Create Variable Argument Function - varargs :_ *
Scala collection such as List or Sequence or even an Array to variable argument function using the s ...
- learning scala How To Create Implicit Function
println("Step 1: How to create a wrapper String class which will extend the String type") ...
- Function Composition vs Object Composition
In functional programming, we create large functions by composing small functions; in object-oriente ...
- [Ramda] Convert a QueryString to an Object using Function Composition in Ramda
In this lesson we'll use a handful of Ramda's utility functions to take a queryString full of name/v ...
- learning scala PartialFunction
Partial函数的定义 scala> val isVeryTasty: PartialFunction[String, String] = { case "Glazed Donut& ...
随机推荐
- 机器学习之逻辑回归(Logistic)笔记
在说逻辑回归之前,可以先说一说逻辑回归与线性回归的区别: 逻辑回归与线性回归在学习规则形式上是完全一致的,它们的区别在于hθ(x(i))为什么样的函数 当hθ(x(i))=θTx(i)时,表示的是线性 ...
- 【SCALA】1、我要开始学习scala啦
因为scala也是基于jvm上运行的,所以能跑java,原则上就能跑scala 1.国际惯例,先来个hello world走走 package demo1 //object之下全为静态,scala没有 ...
- echarts饼状图位置设置
series: { name: "流量占比分布", type: "pie", radius: ["40%", "60%" ...
- websocket 协议简述
WebSocket 是一种网络通信协议,RFC 6455 定义了它的通信标准,属于服务器推送技术的一种 由于 HTTP 无状态.无连接.单向通信的特性,导致 HTTP 协议无法实现服务器主动向客户端发 ...
- tslint 忽略格式检查
// tslint:disable——忽略该行以下所有代码出现的错误提示,可以在文件首行添加达到忽略整个文件的格式提示 // tslint:enable——当前ts文件重新启用tslint// tsl ...
- python——操作系统的发展史
一.手工操作 —— 穿孔卡片 1946年第一台计算机诞生--20世纪50年代中期,计算机工作还在采用手工操作方式.此时还没有操作系统的概念. 程序员将对应于程序和数据的已穿孔的纸带(或卡片)装入 ...
- Java反射机制、注解及JPA实现
1.java反射概述 JAVA反射机制是在运行状态中,对于任意一个实体类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称 ...
- 【SpringMVC】RESTful支持
一.概述 1.1 什么是RESTful 1.2 URL的RESTful实现 二.演示 2.1 需求 2.2 第一步更改DispatcherServlet配置 2.3 第二步参数通过url传递 2.4 ...
- Yarn介绍(设计理念与基本架构)
Yarn : 新的计算框架,是一个全局资源管理器,负责整个集群的资源管理和分配 一. Yarn产生背景 Hadoop1.0MR有局限性,概括为以下几个方面 : 扩展性差 可靠性差 资 ...
- Flutter中的按钮组件介绍
Flutter 里有很多的 Button 组件很多,常见的按钮组件有:RaisedButton.FlatButton.IconButton.OutlineButton.ButtonBar.Floati ...