设为首页 加入收藏

TOP

Scalaz(8)- typeclass:Monoid and Foldable(二)
2017-10-10 12:13:43 】 浏览:11551
Tags:Scalaz typeclass Monoid and Foldable
typeclass:scalaz/Foldable.scala

 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  8   /** As `foldMap` but returning `None` if the foldable is empty and `Some` otherwise */
 9   def foldMap1Opt[A,B](fa: F[A])(f: A => B)(implicit F: Semigroup[B]): Option[B] = { 10  import std.option._ 11     foldMap(fa)(x => some(f(x))) 12  } 13 
14   /**Right-associative fold of a structure. */
15   def foldRight[A, B](fa: F[A], z: => B)(f: (A, => B) => B): B 16 ...

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

 1 final class FoldableOps[F[_],A] private[syntax](val self: F[A])(implicit val F: Foldable[F]) extends Ops[F[A]] {  2   ////
 3  import collection.generic.CanBuildFrom  4   import Leibniz.===
 5   import Liskov.<~<
 6 
 7   final def foldMap[B: Monoid](f: A => B = (a: A) => a): B = F.foldMap(self)(f)  8   final def foldMap1Opt[B: Semigroup](f: A => B = (a: A) => a): Option[B] = F.foldMap1Opt(self)(f)  9   final def foldRight[B](z: => B)(f: (A, => B) => B): B = F.foldRight(self, z)(f) 10   final def foldMapRight1Opt[B](z: A => B)(f: (A, => B) => B): Option[B] = F.foldMapRight1Opt(self)(z)(f) 11   final def foldRight1Opt(f: (A, => A) => A): Option[A] = F.foldRight1Opt(self)(f) 12   final def foldLeft[B](z: B)(f: (B, A) => B): B = F.foldLeft(self, z)(f) 13   final def foldMapLeft1Opt[B](z: A => B)(f: (B, A) => B): Option[B] = F.foldMapLeft1Opt(self)(z)(f) 14   final def foldLeft1Opt(f: (A, A) => A): Option[A] = F.foldLeft1Opt(self)(f) 15   final def foldRightM[G[_], B](z: => B)(f: (A, => B) => G[B])(implicit M: Monad[G]): G[B] = F.foldRightM(self, z)(f) 16   final def foldLeftM[G[_], B](z: B)(f: (B, A) => G[B])(implicit M: Monad[G]): G[B] = F.foldLeftM(self, z)(f) 17   final def foldMapM[G[_] : Monad, B : Monoid](f: A => G[B]): G[B] = F.foldMapM(self)(f) 18   final def fold(implicit A: Monoid[A]): A = F.fold(self)(A) 19   final def foldr[B](z: => B)(f: A => (=> B) => B): B = F.foldr(self, z)(f) 20   final def foldr1Opt(f: A => (=> A) => A): Option[A] = F.foldr1Opt(self)(f) 21   final def foldl[B](z: B)(f: B => A => B): B = F.foldl(self, z)(f) 22   final def foldl1Opt(f: A => A => A): Option[A] = F.foldl1Opt(self)(f) 23   final def foldrM[G[_], B](z: => B)(f: A => ( => B) => G[B])(implicit M: Monad[G]): G[B] = F.foldrM(self, z)(f) 24   final def foldlM[G[_], B](z: B)(f: B => A => G[B])(implicit M: Monad[G]): G[B] = F.foldlM(self, z)(f) 25   final def length: Int = F.length(self) 26   final def index(n: Int): Option[A] = F.index(self, n) 27   final def indexOr(default: => A, n: Int): A = F.indexOr(self, default, n) 28   final def sumr(implicit A: Monoid[A]): A = F.foldRight(self, A.zero)(A.append) 29   final def suml(implicit A: Monoid[A]): A = F.foldLeft(self, A.zero)(A.append(_, _)) 30   final def toList: List[A] = F.toList(self) 31   final def toVector: Vector[A] = F.toVector(self) 32   final def toSet: Set[A] = F.toSet(self) 33   final def toStream: Stream[A] = F.toStream(self) 34   final def toIList: IList[A] = F.toIList(self) 35   final def toEphemeralStream: EphemeralStream[A] = F.toEphemeralStream(self) 36   final def to[G[_]](implicit c: CanBuildFrom[Nothing, A, G[A]]) = F.to[A, G](self) 37   final def all(p: A =>
首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇8.Spark集群测试 下一篇Scalaz(9)- typeclass:checki..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目