-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Override Foldable methods #1532
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -229,7 +229,6 @@ private[data] sealed trait NonEmptyVectorInstances { | |
def traverse[G[_], A, B](fa: NonEmptyVector[A])(f: (A) => G[B])(implicit G: Applicative[G]): G[NonEmptyVector[B]] = | ||
G.map2Eval(f(fa.head), Always(Traverse[Vector].traverse(fa.tail)(f)))(NonEmptyVector(_, _)).value | ||
|
||
|
||
override def foldLeft[A, B](fa: NonEmptyVector[A], b: B)(f: (B, A) => B): B = | ||
fa.foldLeft(b)(f) | ||
|
||
|
@@ -251,6 +250,21 @@ private[data] sealed trait NonEmptyVectorInstances { | |
NonEmptyVector.fromVectorUnsafe(buf.result()) | ||
} | ||
|
||
override def fold[A](fa: NonEmptyVector[A])(implicit A: Monoid[A]): A = | ||
fa.reduce | ||
|
||
override def foldM[G[_], A, B](fa: NonEmptyVector[A], z: B)(f: (B, A) => G[B])(implicit G: Monad[G]): G[B] = | ||
Foldable.iteratorFoldM(fa.toVector.toIterator, z)(f) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. code path is untested. |
||
|
||
override def find[A](fa: NonEmptyVector[A])(f: A => Boolean): Option[A] = | ||
fa.find(f) | ||
|
||
override def forall[A](fa: NonEmptyVector[A])(p: A => Boolean): Boolean = | ||
fa.forall(p) | ||
|
||
override def exists[A](fa: NonEmptyVector[A])(p: A => Boolean): Boolean = | ||
fa.exists(p) | ||
|
||
override def toList[A](fa: NonEmptyVector[A]): List[A] = fa.toVector.toList | ||
|
||
override def toNonEmptyList[A](fa: NonEmptyVector[A]): NonEmptyList[A] = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -294,6 +294,7 @@ private[data] sealed abstract class ValidatedInstances extends ValidatedInstance | |
fab.leftMap(f) | ||
} | ||
|
||
// scalastyle:off method.length | ||
implicit def catsDataInstancesForValidated[E](implicit E: Semigroup[E]): Traverse[Validated[E, ?]] with ApplicativeError[Validated[E, ?], E] = | ||
new Traverse[Validated[E, ?]] with ApplicativeError[Validated[E, ?], E] { | ||
def traverse[F[_]: Applicative, A, B](fa: Validated[E, A])(f: A => F[B]): F[Validated[E, B]] = | ||
|
@@ -323,7 +324,40 @@ private[data] sealed abstract class ValidatedInstances extends ValidatedInstance | |
case v @ Validated.Valid(_) => v | ||
} | ||
def raiseError[A](e: E): Validated[E, A] = Validated.Invalid(e) | ||
|
||
override def reduceLeftToOption[A, B](fa: Validated[E, A])(f: A => B)(g: (B, A) => B): Option[B] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. next 5 methods untested. |
||
fa.map(f).toOption | ||
|
||
override def reduceRightToOption[A, B](fa: Validated[E, A])(f: A => B)(g: (A, Eval[B]) => Eval[B]): Eval[Option[B]] = | ||
Now(fa.map(f).toOption) | ||
|
||
override def reduceLeftOption[A](fa: Validated[E, A])(f: (A, A) => A): Option[A] = | ||
fa.toOption | ||
|
||
override def reduceRightOption[A](fa: Validated[E, A])(f: (A, Eval[A]) => Eval[A]): Eval[Option[A]] = | ||
Now(fa.toOption) | ||
|
||
override def size[A](fa: Validated[E, A]): Long = | ||
fa.fold(_ => 0L, _ => 1L) | ||
|
||
override def foldMap[A, B](fa: Validated[E, A])(f: A => B)(implicit B: Monoid[B]): B = | ||
fa.fold(_ => B.empty, f) | ||
|
||
override def find[A](fa: Validated[E, A])(f: A => Boolean): Option[A] = | ||
fa.toOption.filter(f) | ||
|
||
override def exists[A](fa: Validated[E, A])(p: A => Boolean): Boolean = | ||
fa.exists(p) | ||
|
||
override def forall[A](fa: Validated[E, A])(p: A => Boolean): Boolean = | ||
fa.forall(p) | ||
|
||
override def toList[A](fa: Validated[E, A]): List[A] = | ||
fa.fold(_ => Nil, _ :: Nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. untested |
||
|
||
override def isEmpty[A](fa: Validated[E, A]): Boolean = fa.isInvalid | ||
} | ||
// scalastyle:on method.length | ||
} | ||
|
||
private[data] sealed abstract class ValidatedInstances1 extends ValidatedInstances2 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,12 +83,48 @@ trait EitherInstances extends cats.kernel.instances.EitherInstances { | |
|
||
override def attempt[B](fab: Either[A, B]): Either[A, Either[A, B]] = | ||
Right(fab) | ||
|
||
override def recover[B](fab: Either[A, B])(pf: PartialFunction[A, B]): Either[A, B] = | ||
fab recover pf | ||
|
||
override def recoverWith[B](fab: Either[A, B])(pf: PartialFunction[A, Either[A, B]]): Either[A, B] = | ||
fab recoverWith pf | ||
|
||
override def ensure[B](fab: Either[A, B])(error: => A)(predicate: B => Boolean): Either[A, B] = | ||
fab.ensure(error)(predicate) | ||
|
||
override def reduceLeftToOption[B, C](fab: Either[A, B])(f: B => C)(g: (C, B) => C): Option[C] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. next 5 method untested. |
||
fab.right.map(f).toOption | ||
|
||
override def reduceRightToOption[B, C](fab: Either[A, B])(f: B => C)(g: (B, Eval[C]) => Eval[C]): Eval[Option[C]] = | ||
Now(fab.right.map(f).toOption) | ||
|
||
override def reduceLeftOption[B](fab: Either[A, B])(f: (B, B) => B): Option[B] = | ||
fab.right.toOption | ||
|
||
override def reduceRightOption[B](fab: Either[A, B])(f: (B, Eval[B]) => Eval[B]): Eval[Option[B]] = | ||
Now(fab.right.toOption) | ||
|
||
override def size[B](fab: Either[A, B]): Long = | ||
fab.fold(_ => 0L, _ => 1L) | ||
|
||
override def foldMap[B, C](fab: Either[A, B])(f: B => C)(implicit C: Monoid[C]): C = | ||
fab.fold(_ => C.empty, f) | ||
|
||
override def find[B](fab: Either[A, B])(f: B => Boolean): Option[B] = | ||
fab.fold(_ => None, r => if (f(r)) Some(r) else None) | ||
|
||
override def exists[B](fab: Either[A, B])(p: B => Boolean): Boolean = | ||
fab.right.exists(p) | ||
|
||
override def forall[B](fab: Either[A, B])(p: B => Boolean): Boolean = | ||
fab.right.forall(p) | ||
|
||
override def toList[B](fab: Either[A, B]): List[B] = | ||
fab.fold(_ => Nil, _ :: Nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. untested. |
||
|
||
override def isEmpty[B](fab: Either[A, B]): Boolean = | ||
fab.isLeft | ||
} | ||
// scalastyle:on method.length | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,12 +71,46 @@ trait OptionInstances extends cats.kernel.instances.OptionInstances { | |
override def filter[A](fa: Option[A])(p: A => Boolean): Option[A] = | ||
fa.filter(p) | ||
|
||
override def reduceLeftToOption[A, B](fa: Option[A])(f: A => B)(g: (B, A) => B): Option[B] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. next 5 methods untested. |
||
fa.map(f) | ||
|
||
override def reduceRightToOption[A, B](fa: Option[A])(f: A => B)(g: (A, Eval[B]) => Eval[B]): Eval[Option[B]] = | ||
Now(fa.map(f)) | ||
|
||
override def reduceLeftOption[A](fa: Option[A])(f: (A, A) => A): Option[A] = fa | ||
|
||
override def reduceRightOption[A](fa: Option[A])(f: (A, Eval[A]) => Eval[A]): Eval[Option[A]] = | ||
Now(fa) | ||
|
||
override def minimumOption[A](fa: Option[A])(implicit A: Order[A]): Option[A] = fa | ||
|
||
override def maximumOption[A](fa: Option[A])(implicit A: Order[A]): Option[A] = fa | ||
|
||
override def size[A](fa: Option[A]): Long = fa.fold(0L)(_ => 1L) | ||
|
||
override def foldMap[A, B](fa: Option[A])(f: A => B)(implicit B: Monoid[B]): B = | ||
fa.fold(B.empty)(f) | ||
|
||
override def find[A](fa: Option[A])(f: A => Boolean): Option[A] = | ||
fa.filter(f) | ||
|
||
override def exists[A](fa: Option[A])(p: A => Boolean): Boolean = | ||
fa.exists(p) | ||
|
||
override def forall[A](fa: Option[A])(p: A => Boolean): Boolean = | ||
fa.forall(p) | ||
|
||
override def toList[A](fa: Option[A]): List[A] = fa.toList | ||
|
||
override def filter_[A](fa: Option[A])(p: A => Boolean): List[A] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. next 5 methods untested. |
||
fa.filter(p).toList | ||
|
||
override def takeWhile_[A](fa: Option[A])(p: A => Boolean): List[A] = | ||
fa.filter(p).toList | ||
|
||
override def dropWhile_[A](fa: Option[A])(p: A => Boolean): List[A] = | ||
fa.filterNot(p).toList | ||
|
||
override def isEmpty[A](fa: Option[A]): Boolean = | ||
fa.isEmpty | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this method code path is untested