Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zipWith to NonEmptyList and NonEmptyVector #1885

Merged
merged 1 commit into from
Sep 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will be maybe faster if we do:

toVector.iterator.zip(b.toVector.iterator).map(f).toVector

I think the current code has to allocate a NonEmptyVector after zipped and then another one after map.

But I'm just guessing since I don't know this tuple enrichment. Can you comment as to why this is incorrect if I am wrong?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes of course! I'm on my phone right now, but if you look at the source for zipped https://github.com/scala/scala/tree/v2.12.3/src/library/scala/runtime/Tuple2Zipped.scala#L1 you'll see that it does not actually iterate over either of the vectors until you call map or any of the other methods :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh nice.

}

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