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& ...
随机推荐
- jdk8新特性--函数式接口的使用
函数式接口的概念: 函数式接口的格式: 示例: 函数式接口的使用: 简化lambda表达式:
- oracle_job进程相关学习测试
Oracle cjq0进程测试 测试流程: .CJQ进程不存在 .模拟问题处理 .问题总结 一.问题现象 CJQ0进程不存在 [root@adg1 ~]# ps -ef|grep cjq root : ...
- C# ObservableCollection两个字段排序的情况
相对于System.Linq的OrderBy及OrderByDescending方法,调用后产生IOrderedEnumberable对象,这个对象为排序后的返回值,但原对象未发生变化. 试想,有这种 ...
- redis设置密码,解决重启后密码丢失及自启服务配置
一.安装redis redis3.0及redisManage管理工具 链接:https://pan.baidu.com/s/1p5EWeF2Jgsw9xOE1ADMmRg 提取码:thyf 二.red ...
- JAVA操作ORACLE大对象
一:操作CLOB (1)数据库表结构如下: create table CLOB_TEST ( ID VARCHAR2(5) not null, ...
- 【转载】 C#使用String.Format拼接字符串
在C#程序开发过程中,很多时候会使用字符串拼接,最简单的字符串拼接操作就是所有的字符串使用加号+相加连接起来,但这种代码形式非常不适合代码维护阅读,尤其是拼接字符串语句比较复杂的时候,如拼接SQL语句 ...
- AetherUpload大文件传输
AetherUpload-Laravel是laravel框架下的一个大文件传输组件 github:https://github.com/peinhu/AetherUpload-Laravel 文件传输 ...
- javascript之ECMAScript:语法的操作标准
一.如何书写一个javascript代码 javascript代码需要写在javascript标签中才会生效,而javascript标签可以写在任何地方,但考虑到规范化及页面的加载问题,最好是写在bo ...
- 用js刷剑指offer(连续子数组的最大和)
题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...
- Luogu P1276 校门外的树(增强版)
Luogu P1276 校门外的树(增强版) 本来看着是道普及-,就不打算写博客了,结果因为出了3次错,调试了15min就还是决定写一下-- 本题坑点: 1.每个位置有三种情况:空穴,树苗,树(而不只 ...