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 NonEmptyChain #2406

Merged
merged 23 commits into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add reverse, groupBy and zipWith
  • Loading branch information
Luka Jacobowitz committed Aug 9, 2018
commit 79f22b2817de189943b5a1c3178ed54ff5e8ae70
57 changes: 55 additions & 2 deletions core/src/main/scala/cats/data/Catenable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cats.implicits._
import Catenable._

import scala.annotation.tailrec
import scala.collection.{SortedMap, mutable}

/**
* Trivial catenable sequence. Supports O(1) append, and (amortized)
Expand Down Expand Up @@ -122,12 +123,64 @@ sealed abstract class Catenable[+A] {
}

/** Check whether all elements satisfy the predicate */
final def forall(f: A => Boolean): Boolean =
exists(a => !f(a))
final def forall(f: A => Boolean): Boolean = {
var result: Boolean = true
foreachUntil { a =>
val b = f(a)
if (!b) result = false
!b
}
result
}

/** Check whether an element is in this structure */
final def contains[AA >: A](a: AA)(implicit A: Eq[AA]): Boolean =
exists(A.eqv(a, _))

/** Zips this `Catenable` with another `Catenable` and applies a function for each pair of elements. */
final def zipWith[B, C](other: Catenable[B])(f: (A, B) => C): Catenable[C] =
if (this.isEmpty || other.isEmpty) Catenable.Empty
else {
val iterA = iterator
val iterB = other.iterator

var result: Catenable[C] = Catenable.one(f(iterA.next(), iterB.next()))

while (iterA.hasNext && iterB.hasNext) {
result = result :+ f(iterA.next(), iterB.next())
}
result
}

/**
* Groups elements inside this `Catenable` according to the `Order`
* of the keys produced by the given mapping function.
*/
final def groupBy[B](f: A => B)(implicit B: Order[B]): SortedMap[B, Catenable[A]] = {
implicit val ordering: Ordering[B] = B.toOrdering
var m = mutable.TreeMap.empty[B, Catenable[A]]

foreach { elem =>
val k = f(elem)

m.get(k) match {
case None => m += ((k, singleton(elem))); ()
case Some(cat) => m.update(k, cat :+ elem)
}
}
m
}

/** Reverses this `Catenable` */
def reverse: Catenable[A] = {
var result: Catenable[A] = Catenable.empty
foreach { a =>
result = a +: result
()
}
result
}


/**
* Yields to Some(a, Catenable[A]) with `a` removed where `f` holds for the first time,
Expand Down
40 changes: 32 additions & 8 deletions tests/src/test/scala/cats/tests/CatenableSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,73 @@ class CatenableSuite extends CatsSuite {

test("filterNot and then exists should always be false") {
forAll { (ci: Catenable[Int], f: Int => Boolean) =>
ci.filterNot(f).exists(f) === false
ci.filterNot(f).exists(f) should === (false)
}
}

test("filter and then forall should always be true") {
forAll { (ci: Catenable[Int], f: Int => Boolean) =>
ci.filter(f).forall(f) === true
ci.filter(f).forall(f) should === (true)
}
}

test("exists should be consistent with find + isDefined") {
forAll { (ci: Catenable[Int], f: Int => Boolean) =>
ci.exists(f) === ci.find(f).isDefined
ci.exists(f) should === (ci.find(f).isDefined)
}
}

test("deleteFirst consistent with find") {
forAll { (ci: Catenable[Int], f: Int => Boolean) =>
ci.find(f) === ci.deleteFirst(f).map(_._1)
ci.find(f) should === (ci.deleteFirst(f).map(_._1))
}
}

test("filterNot element and then contains should be false") {
forAll { (ci: Catenable[Int], i: Int) =>
ci.filterNot(_ === i).contains(i) === false
ci.filterNot(_ === i).contains(i) should === (false)
}
}

test("Always nonempty after cons") {
forAll { (ci: Catenable[Int], i: Int) =>
(i +: ci).nonEmpty === true
(i +: ci).nonEmpty should === (true)
}
}

test("fromSeq . toVector is id") {
forAll { (ci: Catenable[Int]) =>
Catenable.fromSeq(ci.toVector) === ci
Catenable.fromSeq(ci.toVector) should === (ci)
}
}

test("fromSeq . toList . iterator is id") {
forAll { (ci: Catenable[Int]) =>
Catenable.fromSeq(ci.iterator.toList) === ci
Catenable.fromSeq(ci.iterator.toList) should === (ci)
}
}

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

test("groupBy consistent with List#groupBy") {
forAll { (cs: Catenable[String], f: String => Int) =>
cs.groupBy(f).map { case (k, v) => (k, v.toList) }.toMap should === (cs.toList.groupBy(f).toMap)
}
}

test("reverse . reverse is id") {
forAll { (ci: Catenable[Int]) =>
ci.reverse.reverse should === (ci)
}
}

test("reverse consistent with List#reverse") {
forAll { (ci: Catenable[Int]) =>
ci.reverse.toList should === (ci.toList.reverse)
}
}
}