17、函数组合与CoffeeScript编程风格

函数组合与CoffeeScript编程风格

1. 函数组合基础

1.1 Compose组合函数

在实际编程中,我们经常会遇到需要将多个函数的操作依次执行的情况。例如,计算税款时,需要先计算所有产品的净利润,再根据净利润计算税款。

profit = ->                      
tax = (amount) ->
  amount / 3                            
netProfit = (products) ->
  profits = (profit product) for product in products
  profits.reduce (acc, p) -> acc + p
netProfitForProducts = netProfit products
taxForProducts = tax netProfitForProducts

同样,在计算用户的忠诚度折扣时,需要先计算用户的总消费金额,再根据总消费金额确定忠诚度类别。

userSpend = (user) ->
  spend = 100
loyaltyDiscount = (spend) ->
  if spend < 1000 then 0
  else if spend < 5000 then 5
  else if spend < 10000 then 10
  else if spe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值