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& ...
随机推荐
- go语言实现链式栈
haa哈哈== import "errors" var ( // ErrEmpty 栈为空 ErrEmpty = errors.New("stack is empty&q ...
- Mybatis动态sql及分页、特殊符号
目的: mybatis动态sql(案例:万能查询) 查询返回结果集的处理 mybatis的分页运用 mybatis的特殊符号 mybatis动态sql(案例:万能查询) 根据id查询 模糊查询 (参数 ...
- Codeforces Round #222 (Div. 1) (ABCDE)
377A Maze 大意: 给定棋盘, 保证初始所有白格连通, 求将$k$个白格变为黑格, 使得白格仍然连通. $dfs$回溯时删除即可. #include <iostream> #inc ...
- C#-System.Dynamic.ExpandoObject
dynamic dynamicObject = new System.Dynamic.ExpandoObject(); dynamicObject.Id = Guid.NewGuid(); (dyna ...
- 你不知道的JSON.stringify知识点
1. 定义 stringify 函数的定义: JSON.stringify(value [, replacer [, space]]) 参数: value : 将要转为JSON字符串的javascri ...
- jQuery的淡入和淡出简单介绍
在jQuery中的一些特效中,可以通过四个方法来实现元素的淡入淡出,这四个方法分别是:fadeIn().fadeOut().fadeToggle() 以及 fadeTo(),下面为分别为大家介绍各个方 ...
- iPhone电话与短信相关代码小结
关于iPhone上电话与短信相关功能,做一个简单总结: 使用公开SDK能实现的功能: (1)获取和操作通讯录.使用函数 ABAddressBookRequestAccessWithCompletion ...
- PCI总线学习
PCI总线概述: 因为不实际操作了,所以就写一些方法论上的东西,纪念一下. PCI总线有三个非常显著的优点: 1. 在计算机和外设传输数据时具有更好的性能. 2. 能够尽量独立于具体的平台. 3. 可 ...
- Mac命令行指定特定程序打开文件
如果文件已被指定默认程序 open httpd.conf 指定一个特定程序打开文件 # 用 sublime text 打开 httpd.conf open -a /Applications/Subli ...
- 查询自动生成Guid列
--newid()可以在查询的时候自动生成Guid列 ' ordey by Random --创建对应的列 用 uniqueidentifier 类型 IF NOT EXISTS ( SELECT * ...