设为首页 加入收藏

TOP

Scalaz(22)- 泛函编程思维: Coerce Monadic Thinking(一)
2017-10-10 12:13:25 】 浏览:8719
Tags:Scalaz 编程 思维 Coerce Monadic Thinking

  马上进入新的一年2016了,来点轻松点的内容吧。前面写过一篇关于用Reader实现依赖注入管理的博文(Scalaz(16)- Monad:依赖注入-Dependency Injection By Reader Monad)。刚好年底这几天抽空重审了一遍,这时才真正认识到让一个老资格OOP程序猿去编写一段FP程序时会发生什么事情:他会用FP语法和数据类型按照OOP的思维编写程序。其结果就是一段尴尬的代码,让人看得不知怎么去形容,更不用提FP程序的精简高雅了。我在前面博文的示范程序正是落入了这个OOP思维陷阱。

我们先把源代码搬过来看看:

package Exercises import scalaz._ import Scalaz._ object reader3 { trait OnOffDevice { def on: String def off: String } trait SensorDevice { def isCoffeePresent: Boolean } trait PowerConfig { def getPowerVolts(country: String): Int def isUSStandard(volt: Int): Boolean } trait OnOffComponent { def onOffDevice: OnOffDevice } trait SensorComponent { def sensorDevice: SensorDevice } trait Device extends OnOffComponent with SensorComponent trait DeviceComponent { def onOffDevice: OnOffDevice def sensorDevice: SensorDevice } trait PowerComponent { def powerConfig: PowerConfig } trait Appliance extends DeviceComponent with PowerComponent object Appliance { val appliance = Reader[Appliance,Appliance](identity) val onOffDevice = appliance map {_.onOffDevice} val sensorDevice = appliance map {_.sensorDevice} val powerConfig = appliance map {_.powerConfig} } object OnOffDevice { import Appliance.onOffDevice def on: Reader[Appliance,String] = onOffDevice map { _.on } def off: Reader[Appliance,String] = onOffDevice map { _.off } } object SensorDevice { import Appliance.sensorDevice def isCoffeePresent: Reader[Appliance,Boolean] = sensorDevice map { _.isCoffeePresent } } object PowerConfig { import Appliance.powerConfig def getPowerVolts(country: String) = powerConfig map {_.getPowerVolts(country)} def isUSStandard(volts: Int) = powerConfig map {_.isUSStandard(volts)} } object OnOffService { def on = for { ison <- OnOffDevice.on } yield ison def off = for { isoff <- OnOffDevice.off } yield isoff } object SensorService { def isCoffeePresent = for { hasCoffee <- SensorDevice.isCoffeePresent } yield hasCoffee } object PowerService { def isUSStandard(country: String) = for { is110v <- PowerConfig.getPowerVolts(country) isUSS <- PowerConfig.isUSStandard(is110v) } yield isUSS } class OnOffDeviceImpl extends OnOffDevice { def on = "SomeDevice.On" def off = "SomeDevice.Off" } class SensorDeviceImpl extends SensorDevice { def isCoffeePresent = true } class PowerConfigImpl extends PowerConfig { def getPowerVolts(country: String) = country match { case "USA" => 110
        case "UK" => 220
        case "HK" => 220
        case "CHN" => 110
        case _  => 0 } def isUSStandard(volts: Int) = volts === 110 } object MockOnOffDevice extends OnOffDeviceImpl object MockSensorDevice extends SensorDeviceImpl object MockPowerConfig extends PowerConfigImpl trait OnOffFunctions extends OnOffComponent { def onOffDevice = MockOnOffDevice } trait SensorFunctions extends SensorComponent { def sensorDevice = MockSensorDevice } trait DeviceFunctions extends DeviceComponent { def onOffDevice = MockOnOffDevice def sensorDevice = MockSensorDevice } trait PowerFunctions extends PowerComponent { def powerConfig = MockPowerConfig } object MockAppliance extends Appliance with DeviceFunctions with PowerFunctions def trigger =
  if ((PowerService.isUSStandard("CHN")(MockAppliance)) && (SensorService.isCoffeePresent(MockAppliance))) OnOffService.on(MockAppliance) else OnOffService.off(MockAppliance) //&
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Scalaz(21)-类型例证:Liskov .. 下一篇Scalaz(23)- 泛函数据结构: Z..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目