Skip to content

Commit

Permalink
Add zipWith to NEL and NEV (#1885)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaJCB authored and kailuowang committed Sep 1, 2017
1 parent 3124534 commit e8891b1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
23 changes: 23 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,29 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) {
go(head, tail, Nil)
}

/**
* Zips this `NonEmptyList` with another `NonEmptyList` and applies a function for each pair of elements.
*
* {{{
* scala> import cats.data.NonEmptyList
* scala> val as = NonEmptyList.of(1, 2, 3)
* scala> val bs = NonEmptyList.of("A", "B", "C")
* scala> as.zipWith(bs)(_ + _)
* res0: cats.data.NonEmptyList[String] = NonEmptyList(1A, 2B, 3C)
* }}}
*/
def zipWith[B, C](b: NonEmptyList[B])(f: (A, B) => C): NonEmptyList[C] = {

@tailrec
def zwRev(as: List[A], bs: List[B], acc: List[C]): List[C] = (as, bs) match {
case (Nil, _) => acc
case (_, Nil) => acc
case (x :: xs, y :: ys) => zwRev(xs, ys, f(x, y) :: acc)
}

NonEmptyList(f(head, b.head), zwRev(tail, b.tail, Nil).reverse)
}

/**
* Zips each element of this `NonEmptyList` with its index.
*
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyVector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,20 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A]) extends AnyVal

NonEmptyVector(head, buf.result())
}

/**
* Zips this `NonEmptyVector` with another `NonEmptyVector` and applies a function for each pair of elements.
*
* {{{
* scala> import cats.data.NonEmptyVector
* scala> val as = NonEmptyVector.of(1, 2, 3)
* scala> val bs = NonEmptyVector.of("A", "B", "C")
* scala> as.zipWith(bs)(_ + _)
* res0: cats.data.NonEmptyVector[String] = NonEmptyVector(1A, 2B, 3C)
* }}}
*/
def zipWith[B, C](b: NonEmptyVector[B])(f: (A, B) => C): NonEmptyVector[C] =
NonEmptyVector.fromVectorUnsafe((toVector, b.toVector).zipped.map(f))
}

private[data] sealed trait NonEmptyVectorInstances {
Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/NonEmptyListTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ class NonEmptyListTests extends CatsSuite {
NonEmptyList.fromReducible(xs) should === (Reducible[NonEmptyVector].toNonEmptyList(xs))
}
}

test("NonEmptyList#zipWith is consistent with List#zip and then List#map") {
forAll { (a: NonEmptyList[Int], b: NonEmptyList[Int], f: (Int, Int) => Int) =>
a.zipWith(b)(f).toList should === (a.toList.zip(b.toList).map {case (x, y) => f(x, y)})
}
}
}

class ReducibleNonEmptyListCheck extends ReducibleCheck[NonEmptyList]("NonEmptyList") {
Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/NonEmptyVectorTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ class NonEmptyVectorTests extends CatsSuite {
nonEmptyVector.distinct.toVector should === (nonEmptyVector.toVector.distinct)
}
}

test("NonEmptyVector#zipWith is consistent with Vector#zip and then Vector#map") {
forAll { (a: NonEmptyVector[Int], b: NonEmptyVector[Int], f: (Int, Int) => Int) =>
a.zipWith(b)(f).toVector should === (a.toVector.zip(b.toVector).map { case (x, y) => f(x, y)})
}
}
}

class ReducibleNonEmptyVectorCheck extends ReducibleCheck[NonEmptyVector]("NonEmptyVector") {
Expand Down

0 comments on commit e8891b1

Please sign in to comment.