Scalaz(8)- typeclass:Monoid and Foldable

2024-04-09 05:08

本文主要是介绍Scalaz(8)- typeclass:Monoid and Foldable,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  Monoid是种最简单的typeclass类型。我们先看看scalaz的Monoid typeclass定义:scalaz/Monoid.scala

trait Monoid[F] extends Semigroup[F] { self =>/** The identity element for `append`. */def zero: F
...

Monoid trait又继承了Semigroup:scalaz/Semigroup.scala

trait Semigroup[F]  { self =>/*** The binary operation to combine `f1` and `f2`.** Implementations should not evaluate the by-name parameter `f2` if result* can be determined by `f1`.*/def append(f1: F, f2: => F): F
...

所以获取一个类型的Monoid实例需要实现zero和append这两个抽象函数。实际上Monoid typeclass也就是支持了append(|+|)这么一个简单的操作。scalaz为一些标准类型定义了Monoid实例:

0 |+| 30                                         //> res0: Int = 50
20.some |+| 30.some                               //> res1: Option[Int] = Some(50)
List(1,2,3) |+| List(4,5,6)                       //> res2: List[Int] = List(1, 2, 3, 4, 5, 6)
Tags.Multiplication(3) |+| Monoid[Int @@ Tags.Multiplication].zero//> res3: scalaz.@@[Int,scalaz.Tags.Multiplication] = 3
Tags.Conjunction(true) |+| Tags.Conjunction(false)//> res4: scalaz.@@[Boolean,scalaz.Tags.Conjunction] = false
Tags.Disjunction(true) |+| Tags.Disjunction(false)//> res5: scalaz.@@[Boolean,scalaz.Tags.Disjunction] = true
Monoid[Boolean @@ Tags.Conjunction].zero          //> res6: scalaz.@@[Boolean,scalaz.Tags.Conjunction] = true
Monoid[Boolean @@ Tags.Disjunction].zero          //> res7: scalaz.@@[Boolean,scalaz.Tags.Disjunction] = false

就这么来看好像没什么值得提的。不过Ordering的Monoid倒是值得研究一下。我们先看看Ordering trait:scalaz/Ordering.scala

 implicit val orderingInstance: Enum[Ordering] with Show[Ordering] with Monoid[Ordering] = new Enum[Ordering] with Show[Ordering] with Monoid[Ordering] {def order(a1: Ordering, a2: Ordering): Ordering = (a1, a2) match {case (LT, LT)      => EQcase (LT, EQ | GT) => LTcase (EQ, LT)      => GTcase (EQ, EQ)      => EQcase (EQ, GT)      => LTcase (GT, LT | EQ) => GTcase (GT, GT)      => EQ}override def shows(f: Ordering) = f.namedef append(f1: Ordering, f2: => Ordering): Ordering = f1 match {case Ordering.EQ => f2case o           => o}
...

这里定义了Ordering的Monoid实例。它的append函数意思是:两个Ordering类型值f1,f2的append操作结果:假如f1是EQ就是f2,否则是f1:

(Ordering.EQ: Ordering) |+| (Ordering.GT: Ordering)//> res8: scalaz.Ordering = GT
(Ordering.EQ: Ordering) |+| (Ordering.LT: Ordering)//> res9: scalaz.Ordering = LT
(Ordering.GT: Ordering) |+| (Ordering.EQ: Ordering)//> res10: scalaz.Ordering = GT
(Ordering.LT: Ordering) |+| (Ordering.EQ: Ordering)//> res11: scalaz.Ordering = LT
(Ordering.LT: Ordering) |+| (Ordering.GT: Ordering)//> res12: scalaz.Ordering = LT
(Ordering.GT: Ordering) |+| (Ordering.LT: Ordering)//> res13: scalaz.Ordering = GT

如果我用以上的特性来比较两个String的长度:如果长度相等则再比较两个String的字符顺序。这个要求刚好符合了Ordering Monoid实例的append操作:

3 ?|? 4                                           //> res14: scalaz.Ordering = LT
"abc" ?|? "bac"                                   //> res15: scalaz.Ordering = LT
def strlenCompare(lhs: String, rhs: String): Ordering =(lhs.length ?|? rhs.length) |+| (lhs ?|? rhs)    //> strlenCompare: (lhs: String, rhs: String)scalaz.OrderingstrlenCompare("abc","aabc")                       //> res16: scalaz.Ordering = LT
strlenCompare("abd","abc")                        //> res17: scalaz.Ordering = GT

这个示范倒是挺新鲜的。

好了,单看Monoid操作会觉着没什么特别,好像不值得研究。实际上Monoid的主要用途是在配合可折叠数据结构(Foldable)对结构内部元素进行操作时使用的。我们再看看这个Foldable typeclass:scalaz/Foldable.scala

trait Foldable[F[_]]  { self =>import collection.generic.CanBuildFromimport collection.immutable.IndexedSeq/** Map each element of the structure to a [[scalaz.Monoid]], and combine the results. */def foldMap[A,B](fa: F[A])(f: A => B)(implicit F: Monoid[B]): B/** As `foldMap` but returning `None` if the foldable is empty and `Some` otherwise */def foldMap1Opt[A,B](fa: F[A])(f: A => B)(implicit F: Semigroup[B]): Option[B] = {import std.option._foldMap(fa)(x => some(f(x)))}/**Right-associative fold of a structure. */def foldRight[A, B](fa: F[A], z: => B)(f: (A, => B) => B): B
...

Foldable typeclass提供了许多注入方法支持折叠操作: scalaz/syntax/FoldableSyntax.scala

final class FoldableOps[F[_],A] private[syntax](val self: F[A])(implicit val F: Foldable[F]) extends Ops[F[A]] {import collection.generic.CanBuildFromimport Leibniz.===import Liskov.<~<final def foldMap[B: Monoid](f: A => B = (a: A) => a): B = F.foldMap(self)(f)final def foldMap1Opt[B: Semigroup](f: A => B = (a: A) => a): Option[B] = F.foldMap1Opt(self)(f)final def foldRight[B](z: => B)(f: (A, => B) => B): B = F.foldRight(self, z)(f)final def foldMapRight1Opt[B](z: A => B)(f: (A, => B) => B): Option[B] = F.foldMapRight1Opt(self)(z)(f)final def foldRight1Opt(f: (A, => A) => A): Option[A] = F.foldRight1Opt(self)(f)final def foldLeft[B](z: B)(f: (B, A) => B): B = F.foldLeft(self, z)(f)final def foldMapLeft1Opt[B](z: A => B)(f: (B, A) => B): Option[B] = F.foldMapLeft1Opt(self)(z)(f)final def foldLeft1Opt(f: (A, A) => A): Option[A] = F.foldLeft1Opt(self)(f)final def foldRightM[G[_], B](z: => B)(f: (A, => B) => G[B])(implicit M: Monad[G]): G[B] = F.foldRightM(self, z)(f)final def foldLeftM[G[_], B](z: B)(f: (B, A) => G[B])(implicit M: Monad[G]): G[B] = F.foldLeftM(self, z)(f)final def foldMapM[G[_] : Monad, B : Monoid](f: A => G[B]): G[B] = F.foldMapM(self)(f)final def fold(implicit A: Monoid[A]): A = F.fold(self)(A)final def foldr[B](z: => B)(f: A => (=> B) => B): B = F.foldr(self, z)(f)final def foldr1Opt(f: A => (=> A) => A): Option[A] = F.foldr1Opt(self)(f)final def foldl[B](z: B)(f: B => A => B): B = F.foldl(self, z)(f)final def foldl1Opt(f: A => A => A): Option[A] = F.foldl1Opt(self)(f)final def foldrM[G[_], B](z: => B)(f: A => ( => B) => G[B])(implicit M: Monad[G]): G[B] = F.foldrM(self, z)(f)final def foldlM[G[_], B](z: B)(f: B => A => G[B])(implicit M: Monad[G]): G[B] = F.foldlM(self, z)(f)final def length: Int = F.length(self)final def index(n: Int): Option[A] = F.index(self, n)final def indexOr(default: => A, n: Int): A = F.indexOr(self, default, n)final def sumr(implicit A: Monoid[A]): A = F.foldRight(self, A.zero)(A.append)final def suml(implicit A: Monoid[A]): A = F.foldLeft(self, A.zero)(A.append(_, _))final def toList: List[A] = F.toList(self)final def toVector: Vector[A] = F.toVector(self)final def toSet: Set[A] = F.toSet(self)final def toStream: Stream[A] = F.toStream(self)final def toIList: IList[A] = F.toIList(self)final def toEphemeralStream: EphemeralStream[A] = F.toEphemeralStream(self)final def to[G[_]](implicit c: CanBuildFrom[Nothing, A, G[A]]) = F.to[A, G](self)final def all(p: A => Boolean): Boolean = F.all(self)(p)final def ∀(p: A => Boolean): Boolean = F.all(self)(p)final def allM[G[_]: Monad](p: A => G[Boolean]): G[Boolean] = F.allM(self)(p)final def anyM[G[_]: Monad](p: A => G[Boolean]): G[Boolean] = F.anyM(self)(p)final def any(p: A => Boolean): Boolean = F.any(self)(p)final def ∃(p: A => Boolean): Boolean = F.any(self)(p)final def count: Int = F.count(self)final def maximum(implicit A: Order[A]): Option[A] = F.maximum(self)final def maximumOf[B: Order](f: A => B): Option[B] = F.maximumOf(self)(f)final def maximumBy[B: Order](f: A => B): Option[A] = F.maximumBy(self)(f)final def minimum(implicit A: Order[A]): Option[A] = F.minimum(self)final def minimumOf[B: Order](f: A => B): Option[B] = F.minimumOf(self)(f)final def minimumBy[B: Order](f: A => B): Option[A] = F.minimumBy(self)(f)final def longDigits(implicit d: A <:< Digit): Long = F.longDigits(self)final def empty: Boolean = F.empty(self)final def element(a: A)(implicit A: Equal[A]): Boolean = F.element(self, a)final def splitWith(p: A => Boolean): List[NonEmptyList[A]] = F.splitWith(self)(p)final def selectSplit(p: A => Boolean): List[NonEmptyList[A]] = F.selectSplit(self)(p)final def collapse[X[_]](implicit A: ApplicativePlus[X]): X[A] = F.collapse(self)final def concatenate(implicit A: Monoid[A]): A = F.fold(self)final def intercalate(a: A)(implicit A: Monoid[A]): A = F.intercalate(self, a)final def traverse_[M[_]:Applicative](f: A => M[Unit]): M[Unit] = F.traverse_(self)(f)final def traverseU_[GB](f: A => GB)(implicit G: Unapply[Applicative, GB]): G.M[Unit] =F.traverseU_[A, GB](self)(f)(G)final def traverseS_[S, B](f: A => State[S, B]): State[S, Unit] = F.traverseS_(self)(f)final def sequence_[G[_], B](implicit ev: A === G[B], G: Applicative[G]): G[Unit] = F.sequence_(ev.subst[F](self))(G)final def sequenceS_[S, B](implicit ev: A === State[S,B]): State[S,Unit] = F.sequenceS_(ev.subst[F](self))def sequenceF_[M[_],B](implicit ev: F[A] <~< F[Free[M,B]]): Free[M, Unit] = F.sequenceF_(ev(self))final def msuml[G[_], B](implicit ev: A === G[B], G: PlusEmpty[G]): G[B] = F.foldLeft(ev.subst[F](self), G.empty[B])(G.plus[B](_, _))}

这简直就是一个完整的函数库嘛。scalaz为大多数标准库中的集合类型提供了Foldable实例,也就是说大多数scala集合类型都支持这么一堆折叠操作函数。我还看不到任何需要去自定义集合类型,标准库的集合类型加上Foldable typeclass应该足够用了。

在Foldable typeclass中比较重要的函数就是foldMap了:

trait Foldable[F[_]]  { self =>import collection.generic.CanBuildFromimport collection.immutable.IndexedSeq/** Map each element of the structure to a [[scalaz.Monoid]], and combine the results. */def foldMap[A,B](fa: F[A])(f: A => B)(implicit F: Monoid[B]): B

首先,foldMap需要Monoid[B]实例来实现。用List来举例:List trait 继承了Traverse:scalaz/std/List.scala

trait ListInstances extends ListInstances0 {implicit val listInstance = new Traverse[List] with MonadPlus[List] with Zip[List] with Unzip[List] with Align[List] with IsEmpty[List] with Cobind[List] {
...

在Traverse typeclass里定义了Foldable实例:scalaz/Traverse.scala

 def foldLShape[A,B](fa: F[A], z: B)(f: (B,A) => B): (B, F[Unit]) =runTraverseS(fa, z)(a => State.modify(f(_, a)))override def foldLeft[A,B](fa: F[A], z: B)(f: (B,A) => B): B = foldLShape(fa, z)(f)._1def foldMap[A,B](fa: F[A])(f: A => B)(implicit F: Monoid[B]): B = foldLShape(fa, F.zero)((b, a) => F.append(b, f(a)))._1override def foldRight[A, B](fa: F[A], z: => B)(f: (A, => B) => B) =foldMap(fa)((a: A) => (Endo.endo(f(a, _: B)))) apply z
...

这个foldMap就是一个游览可折叠结构的函数。在游览过程中用Monoid append对结构中元素进行操作。值得注意的是这个f: A => B参数:这个函数是用来在append操作之前先对内部元素进行一次转变(transform):

List(1,2,3) foldMap {x => x}                      //> res18: Int = 6
List(1,2,3) foldMap {x => (x + 3).toString}       //> res19: String = 456 变成String操作

我们试着用一些实际的例子来示范Monoid的用法。上面提到Monoid在可折叠数据结构里的元素连续处理有着很好的应用,我们先试一个例子:确定一个可折叠数据结构F[A]中的元素A是否排序的:

def ordered(xs: List[Int]): Boolean  //判断xs是否按序排列

由于我们必须游览List xs,所以用Monoid对元素Int进行判断操作是可行的方法。我们先设计一个对比数据结构:

Option[(min: Int, max: Int. ordered: Boolean)], 它记录了当前元素的状态,包括最小,最大,是否排序的:

/判断xs是否是排序的
def ordered(xs: List[Int]): Boolean = {val monoid = new Monoid[Option[(Int,Int,Boolean)]] {  //对类型Option[(Int,Int,Boolean)]定义一个Monoid实例def zero = Nonedef append(a1: Option[(Int,Int,Boolean)], a2: => Option[(Int,Int,Boolean)]) =  //对连续两个元素进行对比操作(a1,a2) match {case (x,None) => xcase (None,x) => x    //保留不为None的状态case (Some((min1,max1,ord1)),Some((min2,max2,ord2))) =>  //如果max1 <= min2状态即为trueSome((min1 min min2, max1 max max2, ord1 && ord2 && (max1 <= min2)))  //更新min,max和ord}}  //我们需要把元素转换成Option((Int,Int,Boolean))(xs.foldMap(i => Option((i, i, true)))(monoid)).map(_._3) getOrElse(true)
}                                                 //> ordered: (xs: List[Int])Booleanordered(List(1,2,12,34))                          //> res21: Boolean = true
ordered(List(1,2,34,23))                          //> res22: Boolean = false

注意这个i => Option((i,i,true)) 转换(transform)。

由于Monoid是种极简单的类型,所以很容易对Monoid进行组合。Monoid组合产生的结果还是Monoid,并且用起来可以更方便:

def productMonoid[A,B](ma: Monoid[A], mb: Monoid[B]): Monoid[(A,B)] = new Monoid[(A,B)] {def zero = (ma.zero, mb.zero)def append(x: (A,B), y: => (A,B)): (A,B) = (ma.append(x._1, y._1), mb.append(x._2, y._2))
}                                                 //> productMonoid: [A, B](ma: scalaz.Monoid[A], mb: scalaz.Monoid[B])scalaz.Mon//| oid[(A, B)]
val pm = productMonoid(Monoid[Int],Monoid[List[Int]])//> pm  : scalaz.Monoid[(Int, List[Int])] = Exercises.monoid$$anonfun$main$1$$a//| non$3@72d1ad2e

以上的pm就是两个Monoid的组合,结果是一个tuple2Monoid。我们可以使用这个tuple2Monoid对可折叠数据结构中元素进行并行操作。比如我们可以在游览一个List[Int]时同时统计长度(list length)及乘积(product):

val intMultMonoid = new Monoid[Int] {def zero = 1def append(a1: Int, a2: => Int): Int = a1 * a2
}                                                 //> intMultMonoid  : scalaz.Monoid[Int] = Exercises.monoid$$anonfun$main$1$$ano//| n$1@6c64cb25
def productMonoid[A,B](ma: Monoid[A], mb: Monoid[B]): Monoid[(A,B)] = new Monoid[(A,B)] {def zero = (ma.zero, mb.zero)def append(x: (A,B), y: => (A,B)): (A,B) = (ma.append(x._1, y._1), mb.append(x._2, y._2))
}                                                 //> productMonoid: [A, B](ma: scalaz.Monoid[A], mb: scalaz.Monoid[B])scalaz.Mon//| oid[(A, B)]
val pm = productMonoid(Monoid[Int @@ Tags.Multiplication],Monoid[Int])//> pm  : scalaz.Monoid[(scalaz.@@[Int,scalaz.Tags.Multiplication], Int)] = Exe//| rcises.monoid$$anonfun$main$1$$anon$3@72d1ad2e
List(1,2,3,4,6).foldMap(i => (i, 1))(productMonoid(intMultMonoid,Monoid[Int]))//> res23: (Int, Int) = (144,5)


我们再来一个合并多层map的Monoid:

def mapMergeMonoid[K,V](V: Monoid[V]): Monoid[Map[K, V]] =new Monoid[Map[K, V]] {def zero = Map[K,V]()def append(a: Map[K, V], b: => Map[K, V]) =(a.keySet ++ b.keySet).foldLeft(zero) { (acc,k) =>acc.updated(k, V.append(a.getOrElse(k, V.zero),b.getOrElse(k, V.zero)))}}                                               //> mapMergeMonoid: [K, V](V: scalaz.Monoid[V])scalaz.Monoid[Map[K,V]]val M: Monoid[Map[String, Map[String, Int]]] = mapMergeMonoid(mapMergeMonoid(Monoid[Int]))//> M  : scalaz.Monoid[Map[String,Map[String,Int]]] = Exercises.monoid$$anonfun//| $main$1$$anon$4@79e2c065val m1 = Map("o1" -> Map("i1" -> 1, "i2" -> 2))  //> m1  : scala.collection.immutable.Map[String,scala.collection.immutable.Map[//| String,Int]] = Map(o1 -> Map(i1 -> 1, i2 -> 2))val m2 = Map("o1" -> Map("i2" -> 3))             //> m2  : scala.collection.immutable.Map[String,scala.collection.immutable.Map[//| String,Int]] = Map(o1 -> Map(i2 -> 3))val m3 = M.append(m1, m2)                        //> m3  : Map[String,Map[String,Int]] = Map(o1 -> Map(i1 -> 1, i2 -> 5))

我们可以用这个组合成的M的append操作进行map的深度合并。m1,m2合并后:Map(o1->Map("i1"->1,"i2" -> 5))。

我们还可以用这个Monoid来统计一段字串内字符发生的频率:

def frequencyMap[A](as: List[A]): Map[A, Int] =as.foldMap((a: A) => Map(a -> 1))(mapMergeMonoid[A, Int](Monoid[Int]))//> frequencyMap: [A](as: List[A])Map[A,Int]
frequencyMap("the brown quik fox is running quikly".toList)//> res24: Map[Char,Int] = Map(e -> 1, s -> 1, x -> 1, n -> 4, y -> 1, t -> 1, //| u -> 3, f -> 1, i -> 4,   -> 6, q -> 2, b -> 1, g -> 1, l -> 1, h -> 1, r -//| > 2, w -> 1, k -> 2, o -> 2)

我们现在可以体会到Monoid必须在可折叠数据结构(Foldable)内才能正真发挥作用。

这篇关于Scalaz(8)- typeclass:Monoid and Foldable的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/887246

相关文章

Kotlin基础——Typeclass

高阶类型 如在Iterable新增泛型方法时 interface Iterable<T> {fun filter(p: (T) -> Boolean): Iterable<T>fun remove(p: (T) -> Boolean): Iterable<T> = filter { x -> !p(x) }} 对应的List、Set实现上述方法时仍需要返回具体的类型 interface

Scalaz(13)- Monad:Writer - some kind of logger

通过前面的几篇讨论我们了解到F[T]就是FP中运算的表达形式(representation of computation)。在这里F[]不仅仅是一种高阶类型,它还代表了一种运算协议(computation protocol)或者称为运算模型好点,如IO[T],Option[T]。运算模型规范了运算值T的运算方式。而Monad是一种特殊的FP运算模型M[A],它是一种持续运算模式。通过flatM

Scalaz(12)- Monad:再述述flatMap,顺便了解MonadPlus

在前面的几篇讨论里我们初步对FP有了些少了解K:FP嘛,不就是F[A]吗?也是,FP就是在F[]壳子(context)内对程序的状态进行更改,也就是在F壳子(context)内施用一些函数。再直白一点就是在F壳子内进行OOP惯用的行令编程(imperative programming)。当然,既然是在壳子(context)内进行编程这种新的模式,那么总需要些新的函数施用方法吧。我们再次审视一下以前

Scalaz(11)- Monad:你存在的意义

前面提到了scalaz是个函数式编程(FP)工具库。它提供了许多新的数据类型、拓展的标准类型及完整的一套typeclass来支持scala语言的函数式编程模式。我们知道:对于任何类型,我们只需要实现这个类型的typeclass实例就可以在对这个类型施用所对应typeclass提供的所有组件函数了(combinator)。突然之间我们的焦点好像都放在了如何获取typeclass实例上了,从而

Scalaz(10)- Monad:就是一种函数式编程模式-a design pattern

Monad typeclass不是一种类型,而是一种程序设计模式(design pattern),是泛函编程中最重要的编程概念,因而很多行内人把FP又称为Monadic Programming。这其中透露的Monad重要性则不言而喻。Scalaz是通过Monad typeclass为数据运算的程序提供了一套规范的编程方式,如常见的for-comprehension。而不同类型的Monad实

Scalaz(9)- typeclass:checking instance abiding the laws

在前几篇关于Functor和Applilcative typeclass的讨论中我们自定义了一个类型Configure,Configure类型的定义是这样的: case class Configure[+A](get: A)object Configure {implicit val configFunctor = new Functor[Configure] {def map[A,B

Scalaz(7)- typeclass:Applicative-idomatic function application

Applicative,正如它的名称所示,就是FP模式的函数施用(function application)。我们在前面的讨论中不断提到FP模式的操作一般都在管道里进行的,因为FP的变量表达形式是这样的:F[A],即变量A是包嵌在F结构里的。Scalaz的Applicative typeclass提供了各种类型的函数施用(function application)和升格(lifting)方

Scalaz(6)- typeclass:Functor-just map

Functor是范畴学(Category theory)里的概念。不过无须担心,我们在scala FP编程里并不需要先掌握范畴学知识的。在scalaz里,Functor就是一个普通的typeclass,具备map over特性。我的理解中,Functor的主要用途是在FP过程中更新包嵌在容器(高阶类)F[T]中元素T值。典型例子如:List[String], Option[Int]等。我们曾经

Scalaz(5)- typeclass:my typeclass scalaz style-demo

我们在上一篇讨论中介绍了一些基本的由scalaz提供的typeclass。这些基本typeclass主要的作用是通过操作符来保证类型安全,也就是在前期编译时就由compiler来发现错误。在这篇讨论中我希望能按照scalaz的格式设计自己的typeclass并能使之融入scalaz库结构里去。   我们来设计一个NoneZero typeclass。这个NoneZero typeclass能

Scalaz(4)- typeclass:标准类型-Equal,Order,Show,Enum

Scalaz是由一堆的typeclass组成。每一个typeclass具备自己特殊的功能。用户可以通过随意多态(ad-hoc polymorphism)把这些功能施用在自己定义的类型上。scala这个编程语言借鉴了纯函数编程语言Haskell的许多概念。typeclass这个名字就是从Haskell里引用过来的。只不过在Haskell里用的名称是type class两个分开的字。因为scala