Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Monad instance (but not implicit) for Id

Fix foldLeft in Foldable for FingerTree (and add tests)
Add simple StreamT functions
  • Loading branch information...
commit 59b87907a2faa129c813bbdfeb932b286c3b8afd 1 parent 86bf331
@copumpkin copumpkin authored
View
7 core/src/main/scala/scalaz/Extras.scala
@@ -38,6 +38,13 @@ trait On[P[_,_], F[_]] {
trait Extras {
type Id[A] = A
+
+ /* N.B: not implicit, as that breaks everything. Make an implicit alias for this when you need it. */
+ val idMonad = new Monad[Id] {
+ def pure[A](a: => A): A = a
+ def bind[A,B](a: A, f: A => B): B = f(a)
+ }
+
trait Konst[A] {
type Apply[B] = A
}
View
4 core/src/main/scala/scalaz/Foldable.scala
@@ -211,9 +211,9 @@ object Foldable extends FoldableLow {
t.fold(v => b,
(v, a) => f(b, a),
(v, pr, m, sf) =>
- FingerFoldable[V].foldLeft(pr,
+ FingerFoldable[V].foldLeft(sf,
foldLeft[Node[V, A], B](m,
- FingerFoldable[V].foldLeft(sf, b, f),
+ FingerFoldable[V].foldLeft(pr, b, f),
(x, y) => NodeFoldable[V].foldLeft(y, x, f)),
f))
}
View
16 core/src/main/scala/scalaz/StreamT.scala
@@ -1,10 +1,11 @@
package scalaz
+import Scalaz._
+
sealed class StreamT[M[_],A](stepper: => M[StreamT.Step[A, StreamT[M,A]]]) {
def step: M[StreamT.Step[A, StreamT[M,A]]] = stepper
import StreamT._
- import Scalaz._
def uncons(implicit M:Monad[M]): M[Option[(A, StreamT[M,A])]] =
step flatMap {
@@ -151,6 +152,19 @@ object StreamT extends Extras {
val zero = empty[M,A]
}
+ def unfoldM[M[_],A,B](start: B)(f: B => M[Option[(A,B)]])(implicit M: Functor[M]): StreamT[M,A] =
+ new StreamT[M,A](f(start) map {
+ case Some((a, b)) => Yield(a, error("oi"))
+ case None => Done
+ })
+
+ def unfold[A,B](b: B)(f: B => Option[(A,B)]): StreamT[Id,A] = unfoldM[Id,A,B](b)(f)(idMonad)
+
+ def fromIterable[A](s: Iterable[A]): StreamT[Id,A] = {
+ def stepper(b: Iterable[A]): Option[(A,Iterable[A])] = if (b.isEmpty) None else Some((b.head, b.tail))
+ unfold(s)(stepper)
+ }
+
def runStreamT[S,A](stream : StreamT[({type λ[X] = State[S,X]})#λ,A], s0: S)
: StreamT[Id,A]
= StreamT[Id,A](
View
3  tests/src/test/scala/scalaz/FingerTreeTest.scala
@@ -57,6 +57,9 @@ class FingerTreeTest extends Specification with Sugar with ScalaCheck {
tree.isEmpty || ((tree.last ≟ tree.toStream.last) && (tree.init.toStream ≟ tree.toStream.init))
}
+ "foldl snoc is identity" verifies {(tree: SequenceTree[Int]) => tree.foldl(FingerTree.empty(SizeReducer[Int]))(_ :+ _).toStream ?= tree.toStream}
+ "foldl cons is reverse" verifies {(tree: SequenceTree[Int]) => tree.foldl(FingerTree.empty(SizeReducer[Int]))((x, y) => y +: x).toStream ?= tree.toStream.reverse}
+
// "viewl works correctly" verifies {(tree: SequenceTree[Int]) =>
// val asStream = tree.toStream
// tree.viewl.fold[Boolean](true, (x: Int, t: ({type λ[α]=FingerTree[Int, α]})#λ) => (x ≟ asStream.head) && (t.toStream ≟ asStream.tail))
Please sign in to comment.
Something went wrong with that request. Please try again.