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
More tests
  • Loading branch information
Luka Jacobowitz committed Aug 8, 2018
commit 68544a6986ace14cdb373045b31894259719dab8
65 changes: 60 additions & 5 deletions core/src/main/scala/cats/data/Catenable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,50 @@ sealed abstract class Catenable[+A] {
}

/** Collect `B` from this for which `f` is defined */
final def collect[B](f: PartialFunction[A, B]): Catenable[B] = {
val predicate = f.lift
final def collect[B](pf: PartialFunction[A, B]): Catenable[B] =
foldLeft(Catenable.empty: Catenable[B]) { (acc, a) =>
predicate(a).fold(acc)(b => acc :+ b)
// trick from TraversableOnce, used to avoid calling both isDefined and apply (or calling lift)
val x = pf.applyOrElse(a, sentinel)
if (x.asInstanceOf[AnyRef] ne sentinel) acc :+ x.asInstanceOf[B]
else acc
}

/** Remove elements not matching the predicate */
final def filter(f: A => Boolean): Catenable[A] =
collect { case a if f(a) => a }

/** Remove elements matching the predicate */
final def filterNot(f: A => Boolean): Catenable[A] =
filter(a => !f(a))

/** Find the first element matching the predicate, if one exists */
final def find(f: A => Boolean): Option[A] = {
var result: Option[A] = Option.empty[A]
foreachUntil { a =>
val b = f(a)
if (b) result = Option(a)
b
}
result
}

/** Check whether at least one element satisfies the predicate */
def exists(f: A => Boolean): Boolean = {
var result: Boolean = false
foreachUntil { a =>
val b = f(a)
if (b) result = true
b
}
result
}

/** Check whether all elements satisfy the predicate */
def forall(f: A => Boolean): Boolean =
exists(a => !f(a))



/**
* Yields to Some(a, Catenable[A]) with `a` removed where `f` holds for the first time,
* otherwise yields None, if `a` was not found
Expand All @@ -109,7 +146,11 @@ sealed abstract class Catenable[+A] {
}

/** Applies the supplied function to each element, left to right. */
private final def foreach(f: A => Unit): Unit = {
private final def foreach(f: A => Unit): Unit =
foreachUntil { a => f(a); false }

/** Applies the supplied function to each element, left to right, but stops when true is returned */
private final def foreachUntil(f: A => Boolean): Unit = {
var c: Catenable[A] = this
val rights = new collection.mutable.ArrayBuffer[Catenable[A]]
// scalastyle:off null
Expand All @@ -123,7 +164,8 @@ sealed abstract class Catenable[+A] {
rights.trimEnd(1)
}
case Singleton(a) =>
f(a)
val b = f(a)
if (b) return ();
c =
if (rights.isEmpty) Empty
else rights.reduceLeft((x, y) => Append(y, x))
Expand All @@ -134,6 +176,16 @@ sealed abstract class Catenable[+A] {
// scalastyle:on null
}

/** Returns the number of elements in this structure */
final def length: Int = {
var i: Int = 0
foreach(_ => i += 1)
i
}

/** Alias for length */
final def size: Int = length


/** Converts to a list. */
final def toList: List[A] = {
Expand Down Expand Up @@ -170,6 +222,9 @@ sealed abstract class Catenable[+A] {
}

object Catenable extends CatenableInstances {

private val sentinel: Function1[Any, Any] = new scala.runtime.AbstractFunction1[Any, Any]{ def apply(a: Any) = this }

private[data] final case object Empty extends Catenable[Nothing] {
def isEmpty: Boolean = true
}
Expand Down
30 changes: 30 additions & 0 deletions tests/src/test/scala/cats/tests/CatenableSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,34 @@ class CatenableSuite extends CatsSuite {
l.show should === (l.toString)
}
}

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

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

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

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

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