Skip to content

Commit

Permalink
Add Reducible.intercalate1
Browse files Browse the repository at this point in the history
  • Loading branch information
peterneyens committed Jan 6, 2017
1 parent 5fb96ec commit b859ea3
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions core/src/main/scala/cats/Reducible.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,23 @@ import simulacrum.typeclass

def maximum[A](fa: F[A])(implicit A: Order[A]): A =
reduceLeft(fa)(A.max)

/**
* Intercalate/insert an element between the existing elements while reducing.
*
* {{{
* scala> import cats.implicits._
* scala> import cats.data.NonEmptyList
* scala> val nel = NonEmptyList.of("a", "b", "c")
* scala> Reducible[NonEmptyList].intercalate1(nel, "-")
* res0: String = a-b-c
* }}}
*/
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)
}

/**
Expand Down

0 comments on commit b859ea3

Please sign in to comment.