Scalaz(36)- Free :实践-Free In Action - 实用体验

在上面几期讨论中我们连续介绍了Free Monad。因为FP是纯函数编程,也既是纯函数的组合集成,要求把纯代码和副作用代码可以分离开来。Free Monad的程序描述(AST)和程序实现(Interpretation)关注分离(separation of concern)模式恰恰能满足FP要求。我们可以用一些代数数据类型(ADT Algebraic Data Type)来模拟功能,再把这些ADT组合起来形成AST(Abstract Syntax Tree)。AST既是对程序功能的描述,它的组成过程也就是Monadic Programming了。在另外一个过程中,我们可以按需要去实现各种Interpreter,从而达到实际运算的目的。我认为既然FP也被称为Monadic Programming,那么Free Monad应该是FP里最重要的数据结构,它的应用模式代表了主流FP,应该有个规范的具体使用方式。在本次讨论中我们将会集中对Free Monad的应用模式进行示范体验。

我们在这次示范中模拟一个针对键值存储(Key Value Store)的操作例子:

1、ADT设计

1 sealed trait KVS[+Next]
2 object KVS {
3  case class Get[Next](key: String, onValue: String => Next) extends KVS[Next]
4  case class Put[Next](key: String, value: String, n: Next) extends KVS[Next]
5  case class Del[Next](key: String, n: Next) extends KVS[Next]

KVS[+Next]就是一种F[A]类型。从Suspend[F[Free[F,A]]可以得出A类型即Free类型,那么Next就是一个Free类,代表Free的下一个状态。如果需要使用Next,F[_]必须是个Functor, 这样才能通过F.map(A=>B)来获取F[B],B==另一个Free。 Put,Del模拟了无返回结果指令,那么如果需要链接到下一个Free状态的话就直接把一个Free放人Next位置。Get返回一个String,onValue函数接过这个返回值再连接到下一个Free状态。

2、获取Functor实例

1   implicit val kvsFunctor = new Functor[KVS] {
2      def map[A,B](kvs: KVS[A])(f: A => B): KVS[B] = kvs match {
3        case Get(key, onResult) => Get(key, onResult andThen f)
4        case Put(key, value, next) => Put(key,value,f(next))
5        case Del(key,next) => Del(key,f(next))
6      }
7   }

把A转换成B就是把Free[KVS,A]转成Free[KVS,B],其实就是map over Next,对Next进行转换。对于函数C=>Next,map就是函数组合了:(C=>Next) andThen (Next=>B)。

3、类型升格,lift to Free

1 implicit def kvsToFree[A](ka: KVS[A]): Free[KVS,A] = Free.liftF(ka)
2  def put(key: String , value: String): Free[KVS,Unit] = Free.liftF(Put(key,value,()))
3  def get(key: String): Free[KVS,String] = Free.liftF(Get(key,identity))
4  def del(key: String): Free[KVS,Unit] = Free.liftF(Del(key,()))

包括隐式类型转换kvsToFree,可以把任何KVS[A]升格成Free[KVS,A]。独立指令升格put,get,del,因为不涉及下一个状态所以使用了()和identity。

4、Composition,Free Monad组合

1 import KVS._
2 def modify(key: String, f: String => String): Free[KVS,Unit] =
3   for {
4    v <- Get(key,identity)
5    _ <- Put(key,f(v), ())
6   } yield()              //> modify: (key: String, f: String => String)scalaz.Free[Exercises.freeExamples.KVS,Unit]

通过隐式函数kvsToFree把ADT Get,Put升格成Free[KVS,A],然后实现函数组合。

5、功能描述,AST设计

1 val script = for {
2   _ <- put("USA","United States Of America")
3   _ <- put("CHN","China")
4   _ <- put("PIL","Pilipines")
5   _ <- put("JPN","Japan")
6   _ <- modify("CHN",_ =>"People's Republic Of China")
7   _ <- del("PIL")
8   chn <- get("CHN")
9 } yield chn              //> script  : scalaz.Free[Exercises.freeExamples.KVS,String] = Gosub()

使用的是独立直接升格指令函数。函数直接返回了Free类型。就像是在for-loop里进行我们熟悉的行令编程:逐条指令编写。

6、功能实现,Interpretation

a、尾递归编译,tail-recursive interpretation

 1 def foldScript(kvs: Free[KVS,String],table: Map[String,String] = Map.empty): Map[String,String] =
 2   kvs.resume.fold (
 3   {
 4     case Get(key,onResult) => foldScript(onResult(table(key)), table)
 5     case Put(key,value, next) => foldScript(next, table + (key -> value))
 6     case Del(key,next) => foldScript(next, table - key)
 7   },
 8   _ => table
 9  )              //> foldScript: (kvs: scalaz.Free[Exercises.freeExamples.KVS,String], table: Map[String,String])Map[String,String]
10  foldScript(script,Map.empty)     //> res0: Map[String,String] = Map(USA -> United States Of America, CHN -> People's Republic Of China, JPN -> Japan)

注意,fold其实是Either.fold。foldScript是个尾递归函数。这时候Next就成为下一步递归的链接了。

b、foldMap,高阶类型转换,Natural Transformation,F[A]~>G[A]

1 type KVState[A] = State[Map[String,String],A]
2  object KvsToMap extends (KVS ~> KVState) {
3    def apply[A](kvs: KVS[A]): KVState[A] = kvs match {
4       case Get(key,onResult) => State { m => (m, onResult(m(key))) }
5       case Put(key,value,next) => State { m => (m + (key -> value), next) }
6       case Del(key,next) => State { m => (m - key, next) }
7    }
8  }
9  script.foldMap(KvsToMap).run(Map.empty)          //> res1: scalaz.Id.Id[(Map[String,String], String)] = (Map(USA -> United States Of America, CHN -> People's Republic Of China, JPN -> Japan),People's Republic Of China)

c、mutable实现方法:

 1 def goScript(kvs: Free[KVS,String],table: scala.collection.mutable.Map[String,String]):Unit =
 2    kvs.go {
 3      case Get(key,onResult) => onResult(table(key))
 4      case Put(key,value,next) => table += (key -> value); next
 5      case Del(key,next) => table -= key; next
 6    }                  //> goScript: (kvs: scalaz.Free[Exercises.freeExamples.KVS,String], table: scala.collection.mutable.Map[String,String])Unit
 7  val mutableMap = scala.collection.mutable.Map[String,String]()
 8                       //> mutableMap  : scala.collection.mutable.Map[String,String] = Map()
 9  goScript(script,mutableMap)
10  println(mutableMap)  //> Map(JPN -> Japan, CHN -> People's Republic Of China, USA -> United States Of America)

把完整的示范源代码提供给大家:

 1 package Exercises
 2 import scalaz._
 3 import Scalaz._
 4 import scala.language.higherKinds
 5 import scala.language.implicitConversions
 6 object freeExamples {
 7 sealed trait KVS[+Next]
 8 object KVS {
 9  case class Get[Next](key: String, onValue: String => Next) extends KVS[Next]
10  case class Put[Next](key: String, value: String, n: Next) extends KVS[Next]
11  case class Del[Next](key: String, n: Next) extends KVS[Next]
12   implicit val kvsFunctor = new Functor[KVS] {
13      def map[A,B](kvs: KVS[A])(f: A => B): KVS[B] = kvs match {
14        case Get(key, onResult) => Get(key, onResult andThen f)
15        case Put(key, value, next) => Put(key,value,f(next))
16        case Del(key,next) => Del(key,f(next))
17      }
18   }
19  implicit def kvsToFree[A](ka: KVS[A]): Free[KVS,A] = Free.liftF(ka)
20  def put(key: String , value: String): Free[KVS,Unit] = Free.liftF(Put(key,value,()))
21  def get(key: String): Free[KVS,String] = Free.liftF(Get(key,identity))
22  def del(key: String): Free[KVS,Unit] = Free.liftF(Del(key,()))
23 }
24 import KVS._
25 def modify(key: String, f: String => String): Free[KVS,Unit] =
26   for {
27    v <- Get(key,identity)
28    _ <- Put(key,f(v), ())
29   } yield()
30 val script = for {
31   _ <- put("USA","United States Of America")
32   _ <- put("CHN","China")
33   _ <- put("PIL","Pilipines")
34   _ <- put("JPN","Japan")
35   _ <- modify("CHN",_ =>"People's Republic Of China")
36   _ <- del("PIL")
37   chn <- get("CHN")
38 } yield chn
39 
40 def foldScript(kvs: Free[KVS,String],table: Map[String,String] = Map.empty): Map[String,String] =
41   kvs.resume.fold (
42   {
43     case Get(key,onResult) => foldScript(onResult(table(key)), table)
44     case Put(key,value, next) => foldScript(next, table + (key -> value))
45     case Del(key,next) => foldScript(next, table - key)
46   },
47   _ => table
48  )
49  foldScript(script,Map.empty)
50 
51  type KVState[A] = State[Map[String,String],A]
52  object KvsToMap extends (KVS ~> KVState) {
53    def apply[A](kvs: KVS[A]): KVState[A] = kvs match {
54       case Get(key,onResult) => State { m => (m, onResult(m(key))) }
55       case Put(key,value,next) => State { m => (m + (key -> value), next) }
56       case Del(key,next) => State { m => (m - key, next) }
57    }
58  }
59  script.foldMap(KvsToMap).run(Map.empty)
60  
61  def goScript(kvs: Free[KVS,String],table: scala.collection.mutable.Map[String,String]):Unit =
62    kvs.go {
63      case Get(key,onResult) => onResult(table(key))
64      case Put(key,value,next) => table += (key -> value); next
65      case Del(key,next) => table -= key; next
66    }
67  val mutableMap = scala.collection.mutable.Map[String,String]()
68  goScript(script,mutableMap)
69  println(mutableMap)
70 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Hongten

模仿Hibernate的逆向工程_java版_源码下载

在这篇blog:"Hibernate逆向工程原理_java版本"中谈到了Hibernate逆向工程原理。

1324
来自专栏码匠的流水账

聊聊eureka的ZoneAffinityServerListFilter

本文主要研究一下eureka的ZoneAffinityServerListFilter

702
来自专栏峰会SaaS大佬云集

关于 hibernate 双向一对多的数据保存

    private Set<Enroll> enrolls=new HashSet<Enroll>();

772
来自专栏不会写文章的程序员不是好厨师

ThreadLocal解析与最佳实践

用于同一个线程内的方法要共享某些变量或状态的时候,提供线程内的局部变量,这种变量在线程的生命周期内起作用,减少同一个线程内多个函数或者组件之间一些公共变量的传递...

721
来自专栏DOTNET

.NET MongoDB Driver 2.2使用示例

说明:mongoDBService是对各种常用操作的封装 public class MongoDBService { #region 变量 ...

33110
来自专栏大数据架构

Java进阶(七)正确理解Thread Local的原理与适用场景

1384
来自专栏Java成神之路

Java_脚本引擎_01_用法入门

最近有个需求,需要在js中调用java,这样能避免更新java,从而实现代码的热更新。

1064
来自专栏haifeiWu与他朋友们的专栏

死磕Java之聊聊ThreadLocal源码(基于JDK1.8)

记得在一次面试中被问到ThreadLocal,答得马马虎虎,所以打算研究一下ThreadLocal的源码

2302
来自专栏Ldpe2G的个人博客

Graphviz4S ---- 在Scala中使用DOT语言绘图的开源工具

1716
来自专栏古时的风筝

模拟实现Spring中的注解装配

在Spring中,XML文件中的bean配置是实现Spring IOC的核心配置文件,在早版本的Spring中,只能基于XML配置文件,配置各个对象之间的依赖关...

2135

扫码关注云+社区