设为首页 加入收藏

TOP

Scalaz(8)- typeclass:Monoid and Foldable(五)
2017-10-10 12:13:43 】 浏览:11547
Tags:Scalaz typeclass Monoid and Foldable
; Boolean): Boolean = F.all(self)(p) 38 final def ?(p: A => Boolean): Boolean = F.all(self)(p) 39 final def allM[G[_]: Monad](p: A => G[Boolean]): G[Boolean] = F.allM(self)(p) 40 final def anyM[G[_]: Monad](p: A => G[Boolean]): G[Boolean] = F.anyM(self)(p) 41 final def any(p: A => Boolean): Boolean = F.any(self)(p) 42 final def ?(p: A => Boolean): Boolean = F.any(self)(p) 43 final def count: Int = F.count(self) 44 final def maximum(implicit A: Order[A]): Option[A] = F.maximum(self) 45 final def maximumOf[B: Order](f: A => B): Option[B] = F.maximumOf(self)(f) 46 final def maximumBy[B: Order](f: A => B): Option[A] = F.maximumBy(self)(f) 47 final def minimum(implicit A: Order[A]): Option[A] = F.minimum(self) 48 final def minimumOf[B: Order](f: A => B): Option[B] = F.minimumOf(self)(f) 49 final def minimumBy[B: Order](f: A => B): Option[A] = F.minimumBy(self)(f) 50 final def longDigits(implicit d: A <:< Digit): Long = F.longDigits(self) 51 final def empty: Boolean = F.empty(self) 52 final def element(a: A)(implicit A: Equal[A]): Boolean = F.element(self, a) 53 final def splitWith(p: A => Boolean): List[NonEmptyList[A]] = F.splitWith(self)(p) 54 final def selectSplit(p: A => Boolean): List[NonEmptyList[A]] = F.selectSplit(self)(p) 55 final def collapse[X[_]](implicit A: ApplicativePlus[X]): X[A] = F.collapse(self) 56 final def concatenate(implicit A: Monoid[A]): A = F.fold(self) 57 final def intercalate(a: A)(implicit A: Monoid[A]): A = F.intercalate(self, a) 58 final def traverse_[M[_]:Applicative](f: A => M[Unit]): M[Unit] = F.traverse_(self)(f) 59 final def traverseU_[GB](f: A => GB)(implicit G: Unapply[Applicative, GB]): G.M[Unit] = 60 F.traverseU_[A, GB](self)(f)(G) 61 final def traverseS_[S, B](f: A => State[S, B]): State[S, Unit] = F.traverseS_(self)(f) 62 final def sequence_[G[_], B](implicit ev: A === G[B], G: Applicative[G]): G[Unit] = F.sequence_(ev.subst[F](self))(G) 63 final def sequenceS_[S, B](implicit ev: A === State[S,B]): State[S,Unit] = F.sequenceS_(ev.subst[F](self)) 64 def sequenceF_[M[_],B](implicit ev: F[A] <~< F[Free[M,B]]): Free[M, Unit] = F.sequenceF_(ev(self)) 65 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](_, _)) 66 //// 67 }

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

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

1 trait Foldable[F[_]]  { self =>
2   ////
3  import collection.generic.CanBuildFrom 4  import collection.immutable.IndexedSeq 5 
6   /** Map each element of the structure to a [[scalaz.Monoid]], and combine the results. */
7   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

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

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

 1  def foldLShape[A,B](fa: F[A], z: B)(f: (B,A) => B): (B, F[Unit]) =
 2     runTraverseS(fa, z)(a => State.modify(f(_, a)))  3 
 4   override def foldLeft[A,B](fa: F[A], z: B)(f: (B,A) => B): B = foldLShape(fa, z)(f)._1  5 
 6   def foldMap[A,B](fa: F[A])(f: A
首页 上一页 2 3 4 5 下一页 尾页 5/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇8.Spark集群测试 下一篇Scalaz(9)- typeclass:checki..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目