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& ...
随机推荐
- 01 IO流(一)—— 流的概念、File类
1 流的概念理解(重要) 理解流的概念非常重要. 流,就是程序到数据源或目的地的一个通道. 我们把这个通道实例化得到一个具体的流,相当于一个数据传输工具,它可以在程序与资源之间进行数据交换. 换言之, ...
- 小程序--e.target和e.currentTarget区别
事件捕获与事件冒泡 事件捕获是从外到内,事件冒泡是从内到外. 注意:不管是不是冒泡事件,都不会改变事件传递的参数值,都还是在dataset中获取(******) target:指事件源组件对象 ...
- 机器学习支持向量机SVM笔记
SVM简述: SVM是一个线性二类分类器,当然通过选取特定的核函数也可也建立一个非线性支持向量机.SVM也可以做一些回归任务,但是它预测的时效性不是太长,他通过训练只能预测比较近的数据变化,至于再往后 ...
- Arraylist的遍历方式、java反射机制
先定义ArrayList再添加几条数据: ArrayList arr=new ArrayList(); //往arrList中增加几条数据 arr.add(1); arr.add(2) ...
- Arm-Linux 移植 jpeg库
背景: jpeg库的使用可以提高显示效率. host平台 :Ubuntu 16.04 arm平台 : S5P6818 jpeg :v9c arm-gcc :4.8.1 主机准备: 运行以下脚 ...
- Python通用爬虫,聚焦爬虫概念理解
通用爬虫:百度.360.搜狐.谷歌.必应....... 原理: (1)抓取网页 (2)采集数据 (3)数据处理 (4)提供检索服务 百度爬虫:Baiduspider 通用爬虫如何抓取新网站? (1)主 ...
- 安装jQuery
description jQuery,顾名思义,也就是JavaScript和Query(查询),即辅助JavaScript开发的库.jQuery是一个快速.简洁的JavaScript框架,是继Prot ...
- 上传图片,图片过大压缩处理以及解决自拍时会出现图片横屏的bug修复、长按保存图片
js部分:module.exports = { resize: function (file, callback, options) { //配置 options = Object.assign({ ...
- sql 四舍五入 保留两位小数
一.问题描述 数据库里的 float momey 类型,都会精确到多位小数.但有时候 我们不需要那么精确,例如,只精确到两位有效数字. 二.sqlserver解决方案: 1. 使用 Round() 函 ...
- 虚拟机安装master,克隆slave0、slave1(多台机相连,网络匹配)
1.在虚拟机中创建一个名叫master的主机 2.创建成功后,打开编辑选项——虚拟网络网络编辑器,填网关 3.打开终端,进入root权限,编写命令 设置虚拟机DNS 4.给master网络配置好后,克 ...