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& ...
随机推荐
- dotnet Core学习之旅(序)
.NET Core 新玩具,新工具,新生产力 我将在博客园我从0.1开始探索.NET Core 的过程. 为什么是从0.1开始而不是从0开始 我在微软刚宣布开源的时候便接触到了.NET Core 1. ...
- QuartzNet 任务管理系统
最近有面试!都有问道Quartz方面的问题,之前的项目有使用过,也知道怎么用,但面试时要说出它的原理,一时半会还真说不来!查阅了一些资料先记录下来吧 Quartz.NET官网地址:https://ww ...
- Luogu5280 [ZJOI2019] 线段树 【线段树】
题目分析: 这题除了分类讨论就没啥了... 容易发现问题实际就是所有操作选和不选按顺序执行的所有答案和.考虑每个点在多少种情况下会有tag. 那么,考虑新插入一个[l,r],所有有交集的点都会被清空, ...
- 为什么要使用Optional
为什么使用Java Optional Why use Optional? NullPointerException 有个很有名的说法: Null Pointer References: The Bil ...
- AVOSCloud入门教程:Android Parse云服务的Hello World
本文时间戳:2013年8月30日 AVOSCloud(万象云)才刚刚推出来不久的咯,其背后创业的推动者据说是Youtube的华人老大陈士骏(SteveChen,貌似手头有很多创业,美味,玩拍,都是,开 ...
- Vue+VSCode开发环境搭建
时间2019.9月 1. 安装 nodeJS; 安装VScode 安装好nodeJS之后,默认会安装好npm 测试 nodeJS 和npm是否可以执行 node -v npm -v 1.1 VScod ...
- 多线程使用libcurl
curl默认情况下有两个地方是线程不安全的, 需要特殊处理, 1是curl_global_init 这个函数必须单线程调用, 2是默认多线程调用https会莫名其妙的挂掉, 以下是网上的解决方案 ht ...
- Gogs + Drone 实现CI/CD(CI)
本文通过docker-compose方式安装运行drone,先将drone的server和agent镜像拉取到本地,这样docker-compose脚本执行速度会快一点.当然,不是必须先拉取drone ...
- activemq BytesMessage || TextMessage
需求:使用 python 程序向 activemq 的主题推送数据,默认推送的数据类型是 BytesMessage,java 程序那边接收较为麻烦,改为推送 TextMessage 类型的数据 解决方 ...
- JavaScript时钟效果
在JavaScript中,有一个内置对象Date,它重要的一个作用就是实现了时间的时刻更新,通过代码来创造一个实实在在的时间表. 代码例子: <!DOCTYPE html> <htm ...