• 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. CentOS7+Docker+MangoDB下部署简单的MongoDB分片集群

    简单的在Docker上快速部署MongoDB分片集群 前言 文中使用的环境如下 OS:CentOS Linux release 7.5.1804 (Core) Docker:Docker versio ...

  2. 怎样在python中写多行语句

    一般来说, 一行就是一条语句, 但有时语句过长不利于阅读, 一般会写成多行的形式, 这时需要在换行时使用反斜杠: \ name = "Lilei" age = 23 gender ...

  3. wannafly 挑战赛10 小H和密码

    题意:中文题就不解释了 题解: dp[i][j]表示前i 个轮盘 和一个字符串前j 个字符的匹配情况 ,具体的状态转移解释见代码 #include <cstdio> #include &l ...

  4. 表单送件前的Check(二) (未完)

    #region 新增表单前的Check动作 public static void NewFormRequestCheck(string _FormID, string _VocaEmpID, stri ...

  5. Linux Wireless Supported Devices

    Linux Wireless Supported Devices https://ark.intel.com/content/www/us/en/ark/products/series/59484/i ...

  6. Extjs 兼容IE8常见问题及解决方法

    1. 在IE8中整个页面都打不开,一般情况是: 页面组件中最后一个属性出现了逗号 没有多余的逗号,就很有可能是组件中没有设置renderTo:Ext.getBody(); 2. 页面按钮颜色失效 自定 ...

  7. 【华为云】MRS journey node HDFS 不一致

    HDFS JournalNode数据不同步告警 恢复指导 1 停止有问题的 JN 实例 2 清楚无效数据 登录有问题JN的后台,使用omm用户操作以下命令: cd /srv/BigData/journ ...

  8. IAR建立stm32工程

    stm32是一个当下非常流行的微控制器,很多人都加入了学习stm32的行列中,常用的stm32编译器有IAR和mdk两种,接下来是利用stm32固件库3.5在IAR下的建立的工程模板历程: 1.在常用 ...

  9. 浅谈Java中的AOP面向切面的变成和控制反转IOC

    https://blog.csdn.net/hi_kevin/article/details/7325554 https://www.cnblogs.com/zedosu/p/6632260.html ...

  10. 解决Hibernate validator抛出Ljavax/validation/ParameterNameProvider异常方法

    最近升级CAS Client4.0客户端Spring版本至Spring4.*以上,升级整合hibernate框架时,Hibernate validator 4.3.0.Final使用版本(从Hiber ...