Skip to content

Commit ab74f33

Browse files
committed
Merge branch 'main' into sam-instances
2 parents 132b4c3 + 3c98865 commit ab74f33

File tree

18 files changed

+204
-22
lines changed

18 files changed

+204
-22
lines changed

core/src/main/scala/cats/ApplicativeError.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ trait ApplicativeError[F[_], E] extends Applicative[F] {
247247
def catchNonFatal[A](a: => A)(implicit ev: Throwable <:< E): F[A] =
248248
try pure(a)
249249
catch {
250-
case NonFatal(e) => raiseError(e)
250+
case e if NonFatal(e) => raiseError(e)
251251
}
252252

253253
/**
@@ -257,7 +257,7 @@ trait ApplicativeError[F[_], E] extends Applicative[F] {
257257
def catchNonFatalEval[A](a: Eval[A])(implicit ev: Throwable <:< E): F[A] =
258258
try pure(a.value)
259259
catch {
260-
case NonFatal(e) => raiseError(e)
260+
case e if NonFatal(e) => raiseError(e)
261261
}
262262

263263
/**

core/src/main/scala/cats/TraverseFilter.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ trait TraverseFilter[F[_]] extends FunctorFilter[F] {
5656
*/
5757
def traverseFilter[G[_], A, B](fa: F[A])(f: A => G[Option[B]])(implicit G: Applicative[G]): G[F[B]]
5858

59+
/**
60+
* A combined [[traverse]] and [[collect]].
61+
*
62+
* scala> import cats.implicits._
63+
* scala> val m: Map[Int, String] = Map(1 -> "one", 2 -> "two")
64+
* scala> val l: List[Int] = List(1, 2, 3, 4)
65+
* scala> def asString: PartialFunction[Int, Eval[Option[String]]] = { case n if n % 2 == 0 => Now(m.get(n)) }
66+
* scala> val result: Eval[List[Option[String]]] = l.traverseCollect(asString)
67+
* scala> result.value
68+
* res0: List[Option[String]] = List(Some(two), None)
69+
*/
70+
def traverseCollect[G[_], A, B](fa: F[A])(f: PartialFunction[A, G[B]])(implicit G: Applicative[G]): G[F[B]] = {
71+
val optF = f.lift
72+
traverseFilter(fa)(a => Traverse[Option].sequence(optF(a)))
73+
}
74+
5975
/**
6076
* {{{
6177
* scala> import cats.implicits._

core/src/main/scala/cats/data/NonEmptyList.scala

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,37 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) extends NonEmptyCollec
133133
def prepend[AA >: A](a: AA): NonEmptyList[AA] =
134134
NonEmptyList(a, head :: tail)
135135

136+
/**
137+
* Alias for [[prependList]]
138+
*
139+
* {{{
140+
* scala> import cats.data.NonEmptyList
141+
* scala> val nel = NonEmptyList.of(1, 2, 3)
142+
* scala> val list = List(-1, 0)
143+
* scala> list ++: nel
144+
* res0: cats.data.NonEmptyList[Int] = NonEmptyList(-1, 0, 1, 2, 3)
145+
* }}}
146+
*/
147+
def ++:[AA >: A](other: List[AA]): NonEmptyList[AA] =
148+
prependList(other)
149+
150+
/**
151+
* Prepend another `List`
152+
*
153+
* {{{
154+
* scala> import cats.data.NonEmptyList
155+
* scala> val nel = NonEmptyList.of(1, 2, 3)
156+
* scala> val list = List(-1, 0)
157+
* scala> nel.prependList(list)
158+
* res0: cats.data.NonEmptyList[Int] = NonEmptyList(-1, 0, 1, 2, 3)
159+
* }}}
160+
*/
161+
def prependList[AA >: A](other: List[AA]): NonEmptyList[AA] =
162+
other match {
163+
case Nil => this
164+
case head :: tail => NonEmptyList(head, tail ::: toList)
165+
}
166+
136167
/**
137168
* Alias for append
138169
*
@@ -149,6 +180,19 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) extends NonEmptyCollec
149180
def append[AA >: A](a: AA): NonEmptyList[AA] =
150181
NonEmptyList(head, tail :+ a)
151182

183+
/**
184+
* Alias for [[concat]]
185+
*
186+
* {{{
187+
* scala> import cats.data.NonEmptyList
188+
* scala> val nel = NonEmptyList.of(1, 2, 3)
189+
* scala> nel.appendList(List(4, 5))
190+
* res0: cats.data.NonEmptyList[Int] = NonEmptyList(1, 2, 3, 4, 5)
191+
* }}}
192+
*/
193+
def appendList[AA >: A](other: List[AA]): NonEmptyList[AA] =
194+
concat(other)
195+
152196
/**
153197
* Alias for concatNel
154198
*

core/src/main/scala/cats/data/NonEmptySeq.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ final class NonEmptySeq[+A] private (val toSeq: Seq[A]) extends AnyVal with NonE
121121
*/
122122
def concat[AA >: A](other: Seq[AA]): NonEmptySeq[AA] = new NonEmptySeq(toSeq ++ other)
123123

124+
/**
125+
* Append another `Seq` to this, producing a new `NonEmptySeq`.
126+
*/
127+
def appendSeq[AA >: A](other: Seq[AA]): NonEmptySeq[AA] = concat(other)
128+
124129
/**
125130
* Append another `NonEmptySeq` to this, producing a new `NonEmptySeq`.
126131
*/

core/src/main/scala/cats/data/NonEmptyVector.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A])
124124
*/
125125
def concat[AA >: A](other: Vector[AA]): NonEmptyVector[AA] = new NonEmptyVector(toVector ++ other)
126126

127+
/**
128+
* Append another `Vector` to this, producing a new `NonEmptyVector`
129+
*
130+
* {{{
131+
* scala> import cats.data.NonEmptyVector
132+
* scala> val nev = NonEmptyVector.of(1, 2, 3)
133+
* scala> nev.appendVector(Vector(4, 5))
134+
* res0: cats.data.NonEmptyVector[Int] = NonEmptyVector(1, 2, 3, 4, 5)
135+
* }}}
136+
*/
137+
def appendVector[AA >: A](other: Vector[AA]): NonEmptyVector[AA] = concat(other)
138+
127139
/**
128140
* Append another `NonEmptyVector` to this, producing a new `NonEmptyVector`.
129141
*/

core/src/main/scala/cats/data/Validated.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ private[data] trait ValidatedFunctions {
11251125
try {
11261126
valid(f)
11271127
} catch {
1128-
case scala.util.control.NonFatal(t) => invalid(t)
1128+
case t if scala.util.control.NonFatal(t) => invalid(t)
11291129
}
11301130

11311131
/**

core/src/main/scala/cats/instances/try.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ trait TryInstances extends TryInstances1 {
109109
ta match {
110110
case Success(a) => bind(a); case Failure(e) => recover(e)
111111
}
112-
catch { case NonFatal(e) => Failure(e) }
112+
catch { case e if NonFatal(e) => Failure(e) }
113113

114114
override def recover[A](ta: Try[A])(pf: PartialFunction[Throwable, A]): Try[A] =
115115
ta.recover(pf)

core/src/main/scala/cats/syntax/either.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ final class EitherObjectOps(private val either: Either.type) extends AnyVal {
390390
try {
391391
right(f)
392392
} catch {
393-
case scala.util.control.NonFatal(t) => left(t)
393+
case t if scala.util.control.NonFatal(t) => left(t)
394394
}
395395

396396
/**

core/src/main/scala/cats/syntax/traverseFilter.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ trait TraverseFilterSyntax extends TraverseFilter.ToTraverseFilterOps
2727
private[syntax] trait TraverseFilterSyntaxBinCompat0 {
2828
implicit def toSequenceFilterOps[F[_], G[_], A](fgoa: F[G[Option[A]]]): SequenceFilterOps[F, G, A] =
2929
new SequenceFilterOps(fgoa)
30+
31+
implicit def toTraverseFilterOps[F[_], G[_], A](fa: F[A]): TraverseFilterOps[F, G, A] =
32+
new TraverseFilterOps(fa)
3033
}
3134

3235
final class SequenceFilterOps[F[_], G[_], A](private val fgoa: F[G[Option[A]]]) extends AnyVal {
@@ -41,3 +44,12 @@ final class SequenceFilterOps[F[_], G[_], A](private val fgoa: F[G[Option[A]]])
4144
*/
4245
def sequenceFilter(implicit F: TraverseFilter[F], G: Applicative[G]): G[F[A]] = F.sequenceFilter(fgoa)
4346
}
47+
48+
final class TraverseFilterOps[F[_], G[_], A](private val fa: F[A]) extends AnyVal {
49+
50+
def traverseCollect[B](f: PartialFunction[A, G[B]])(implicit
51+
F: TraverseFilter[F],
52+
G: Applicative[G]
53+
): G[F[B]] =
54+
F.traverseCollect(fa)(f)
55+
}

kernel-laws/shared/src/main/scala/cats/kernel/laws/SerializableLaws.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ object SerializableLaws {
6161
ois.close()
6262
Result(status = Proof)
6363
} catch {
64-
case NonFatal(t) =>
64+
case t if NonFatal(t) =>
6565
Result(status = Exception(t))
6666
} finally {
6767
oos.close()

0 commit comments

Comments
 (0)