1 val p: Process[Task,Int] = Process.emitAll(Seq(1,2,3)) //> p : scalaz.stream.Process[scalaz.concurrent.Task,Int] = Append(Halt(End),Vector(<function1>))
2 Process.range(1,2,3).toSource //> res4: scalaz.stream.Process[scalaz.concurrent.Task,Int] = Append(Halt(End),Vector(<function1>)) 3 //把F[A]升格成Process[F,A]
4 Process.eva l(Task.delay {5 * 8}) //> res5: scalaz.stream.Process[scalaz.concurrent.Task,Int] = Await(scalaz.concurrent.Task@56aac163,<function1>,<function1>)
/** * Collect the outputs of this `Process[F,O]` into a Monoid `B`, given a `Monad[F]` in * which we can catch exceptions. This function is not tail recursive and * relies on the `Monad[F]` to ensure stack safety. */ final def runFoldMap[F2[x] >: F[x], B](f: O => B)(implicit F: Monad[F2], C: Catchable[F2], B: Monoid[B]): F2[B] = { ...} /** * Collect the outputs of this `Process[F,O]`, given a `Monad[F]` in * which we can catch exceptions. This function is not tail recursive and * relies on the `Monad[F]` to ensure stack safety. */ final def runLog[F2[x] >: F[x], O2 >: O](implicit F: Monad[F2], C: Catchable[F2]): F2[Vector[O2]] = { ...} /** Run this `Process` solely for its final emitted value, if one exists. */ final def runLast[F2[x] >: F[x], O2 >: O](implicit F: Monad[F2], C: Catchable[F2]): F2[Option[O2]] = { ...} /** Run this `Process` solely for its final emitted value, if one exists, using `o2` otherwise. */ final def runLastOr[F2[x] >: F[x], O2 >: O](o2: => O2)(implicit F: Monad[F2], C: Catchable[F2]): F2[O2] = runLast[F2, O2] map { _ getOrElse o2 } /** Run this `Process`, purely for its effects. */ final def run[F2[x] >: F[x]](implicit F: Monad[F2], C: Catchable[F2]): F2[Unit] = F.void(drain.runLog(F, C))
1 //runFoldMap就好比Monoid的sum
2 p.runFoldMap(identity).run //> res6: Int = 6
3 p.runFoldMap(i => i * 2).run //> res7: Int = 12
4 p.runFoldMap(_.toString).run //> res8: String = 123 5 //runLog把收到的元素放入vector中
6 p.runLog.run //> res9: Vector[Int] = Vector(1, 2, 3) 7 //runLast取最后一个元素,返回Option
8 p.runLast.run //> res10: Option[Int] = Some(3)
9 Process.halt.toSource.runLast.run //> res11: Option[Nothing] = None
10 Process.halt.toSource.runLastOr(65).run //> res12: Int = 65 11 //run只进行F的运算,放弃所有元素
12 p.run //> res13: scalaz.concurrent.Task[Unit] = scalaz.concurrent.Task@26b3fd41
13 p.run.run //Task[Unit] 返回Unit
14 Process.emit(print("haha")).toSource.run.run //> haha
1 p.take(2).runLog.run //> res14: Vector[Int] = Vector(1, 2)
2 p.filter {_ > 2}.runLog.run //> res15: Vector[Int] = Vector(3)
3 p.last.runLog.run //> res16: Vector[Int] = Vector(3)
4 p.drop(1).runLog.run //> res17: Vector[Int] = Vector(2, 3)
5 p.exists{_ > 5}.runLog.run //> res18: Vector[Boolean] = Vector(false)
1 p.map{i => s"Int:$i"}.runLog.run //> res19: Vector[String] = Vector(Int:1, Int:2, Int:3)
2 p.flatMap{i => Process(i,i-1)}.runLog.run //> res20: Vector[Int] = Vector(1, 0, 2, 1, 3, 2)