• Ordering using andThenf(x) andThen g(x) = g(f(x))
  • Ordering using composef(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的更多相关文章

  1. learning scala Function Recursive Tail Call

    可以使用scala库,可以从字面上看出是在调用 递归函数: code import scala.util.control.TailCalls._ val arrayDonuts: Array[Stri ...

  2. [machine learning] Loss Function view

    [machine learning] Loss Function view 有关Loss Function(LF),只想说,终于写了 一.Loss Function 什么是Loss Function? ...

  3. [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. ...

  4. [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 ...

  5. 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 ...

  6. learning scala How To Create Implicit Function

    println("Step 1: How to create a wrapper String class which will extend the String type") ...

  7. Function Composition vs Object Composition

    In functional programming, we create large functions by composing small functions; in object-oriente ...

  8. [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 ...

  9. learning scala PartialFunction

    Partial函数的定义 scala> val isVeryTasty: PartialFunction[String, String] = { case "Glazed Donut& ...

随机推荐

  1. BZOJ4400 TJOI2012桥(最短路+线段树)

    首先找出任意一条1-n的最短路径.显然删除的边只有在该最短路上才会对最短路长度产生影响. 不会证明地给出一个找不到反例的结论:删除一条边后,新图中一定有一条1-n的最短路径上存在一条边x->y, ...

  2. windows下使用 fdfs_client 上传文件(转)

    https://www.cnblogs.com/kindleheart/p/10134502.html 注意:client = Fdfs_client(r"绝对路径"),一定要是绝 ...

  3. Error while launching application Error: spawn ENOMEM 解决

    当NodeJs PM2 无法启动应用 出现 Error while launching application Error: spawn ENOMEM 错误时     执行一下  pm2 update ...

  4. 怎样输出Hello World

    方法一: 进入python交互模式, 然后使用: print()函数输出 方法二:  新建一个.py文件, 然后写入print()函数, 再使用python命令执行输出:

  5. SQL Server2008数据库报错与解决方法

    一. 报错信息 启动MSSQLSERVER时有以下报错信息 打开SQL SERVER配置管理器,发现以下情况报错: 原因:由于先前安装了2005版VS,然后又安装了2015版VS 解决办法:卸载Loc ...

  6. Markdown笔记(git提交带有emoji的commit描述)

    用法:> git commit -m ' :tada: initial ' emoji来源:> ![gitmoji](https://gitmoji.carloscuesta.me/sta ...

  7. 数据多的时候为什么要使用redis而不用mysql?

    2018-06-28  136465569...  转自 庆亮trj21bc... 修改   微信 分享: Redis和MySQL的应用场景是不同的. 通常来说,没有说用Redis就不用MySQL的这 ...

  8. mac os安装mtr

    MTR是Linux平台上一款非常好用的网络诊断工具,集成了traceroute.ping.nslookup的功能,用于诊断网络状态非常有用 现使用的方法是下载pkg包手动安装 mtr的pkg下载地址 ...

  9. 简单理解undefine和null的区别

    直接进入主题: 相同点:都表示“值的空缺” 不同点: null: 定义:一个空对象指针. 使用typeOf得到Object,相当于是一个特殊值 undefine: 定义:声明变量却未对其加以初始化的变 ...

  10. Linux Exploit系列之三 Off-By-One 漏洞 (基于栈)

    Off-By-One 漏洞 (基于栈) 原文地址:https://bbs.pediy.com/thread-216954.htm 什么是off by one? 将源字符串复制到目标缓冲区可能会导致of ...