Skip to content

Commit

Permalink
Update Foldable/Reducible intercalate to reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
peterneyens committed Jan 10, 2017
1 parent fd15e07 commit cc484d6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 7 deletions.
18 changes: 15 additions & 3 deletions core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cats

import scala.collection.mutable
import cats.instances.list._
import cats.instances.long._
import simulacrum.typeclass

Expand Down Expand Up @@ -402,9 +403,20 @@ import simulacrum.typeclass
* }}}
*/
def intercalate[A](fa: F[A], a: A)(implicit A: Monoid[A]): A =
reduceLeftOption(fa){ (acc, aa) =>
A.combine(acc, A.combine(a, aa))
}.getOrElse(A.empty)
Foldable[List].combineAll(intersperseList(toList(fa), a))

protected def intersperseList[A](xs: List[A], x: A): List[A] = {
val bld = List.newBuilder[A]
val it = xs.iterator
if (it.hasNext) {
bld += it.next
while(it.hasNext) {
bld += x
bld += it.next
}
}
bld.result
}

def compose[G[_]: Foldable]: Foldable[λ[α => F[G[α]]]] =
new ComposedFoldable[F, G] {
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/scala/cats/Reducible.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@ import simulacrum.typeclass
* }}}
*/
def intercalate1[A](fa: F[A], a: A)(implicit A: Semigroup[A]): A =
reduceLeft(fa)((acc, aa) => A.combine(acc, A.combine(a, aa)))

override def intercalate[A](fa: F[A], a: A)(implicit A: Monoid[A]): A =
intercalate1(fa, a)
toNonEmptyList(fa) match {
case NonEmptyList(hd, Nil) => hd
case NonEmptyList(hd, tl) =>
Reducible[NonEmptyList].reduce(NonEmptyList(hd, a :: intersperseList(tl, a)))
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyVector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ private[data] sealed trait NonEmptyVectorInstances {
go(f(a))
NonEmptyVector.fromVectorUnsafe(buf.result())
}

override def toList[A](fa: NonEmptyVector[A]): List[A] = fa.toVector.toList

override def toNonEmptyList[A](fa: NonEmptyVector[A]): NonEmptyList[A] =
NonEmptyList(fa.head, fa.tail.toList)
}

implicit def catsDataEqForNonEmptyVector[A](implicit A: Eq[A]): Eq[NonEmptyVector[A]] =
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/instances/list.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ trait ListInstances extends cats.kernel.instances.ListInstances {

override def foldM[G[_], A, B](fa: List[A], z: B)(f: (B, A) => G[B])(implicit G: Monad[G]): G[B] =
Foldable.iteratorFoldM(fa.toIterator, z)(f)

override def toList[A](fa: List[A]): List[A] = fa
}

implicit def catsStdShowForList[A:Show]: Show[List[A]] =
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/instances/set.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ trait SetInstances extends cats.kernel.instances.SetInstances {

override def foldM[G[_], A, B](fa: Set[A], z: B)(f: (B, A) => G[B])(implicit G: Monad[G]): G[B] =
Foldable.iteratorFoldM(fa.toIterator, z)(f)

override def toList[A](fa: Set[A]): List[A] = fa.toList
}

implicit def catsStdShowForSet[A:Show]: Show[Set[A]] = new Show[Set[A]] {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/instances/stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ trait StreamInstances extends cats.kernel.instances.StreamInstances {

override def foldM[G[_], A, B](fa: Stream[A], z: B)(f: (B, A) => G[B])(implicit G: Monad[G]): G[B] =
Foldable.iteratorFoldM(fa.toIterator, z)(f)

override def toList[A](fa: Stream[A]): List[A] = fa.toList
}

implicit def catsStdShowForStream[A: Show]: Show[Stream[A]] =
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/instances/vector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ trait VectorInstances extends cats.kernel.instances.VectorInstances {

override def foldM[G[_], A, B](fa: Vector[A], z: B)(f: (B, A) => G[B])(implicit G: Monad[G]): G[B] =
Foldable.iteratorFoldM(fa.toIterator, z)(f)

override def toList[A](fa: Vector[A]): List[A] = fa.toList
}

implicit def catsStdShowForVector[A:Show]: Show[Vector[A]] =
Expand Down

0 comments on commit cc484d6

Please sign in to comment.