Skip to content
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
4 changes: 4 additions & 0 deletions alleycats-core/src/main/scala/alleycats/std/set.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ trait SetInstances {
override def empty[A]: Set[A] = Set.empty

override def combineK[A](x: Set[A], y: Set[A]): Set[A] = x | y

override def prependK[A](a: A, fa: Set[A]): Set[A] = fa + a

override def appendK[A](fa: Set[A], a: A): Set[A] = fa + a
}

// Since iteration order is not guaranteed for sets, folds and other
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala-2.12/cats/instances/stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ trait StreamInstances extends cats.kernel.instances.StreamInstances {

def combineK[A](x: Stream[A], y: Stream[A]): Stream[A] = x #::: y

override def prependK[A](a: A, fa: Stream[A]): Stream[A] = a #:: fa

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For LazyList you overrode both prependK and appendK, why only prependK for Stream, are the implementations different?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, this is because appendK for LazyList utilizes LazyList#appended which is optimized for LazyList. But appended/prepended methods were first introduced in Scala 2.13 and Stream in Scala 2.12 does not have such methods yet. Therefore seems the best we can do here is to call

def appendK[A](fa: Stream[A], a: A) = fa #::: Stream(a)

but it is exactly what combineK (along with pure) already does.

@satorg satorg Nov 27, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

On the second thought, Stream in Scala 2.13 does have the prepended/appended methods, but as I can see they're simply come from Seq and aren't optimized for Stream (unlike those in LazyList).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Makes sense, thanks for explaining!


def pure[A](x: A): Stream[A] = Stream(x)

override def map[A, B](fa: Stream[A])(f: A => B): Stream[B] =
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/scala-2.13+/cats/instances/arraySeq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ private[cats] object ArraySeqInstances {
def combineK[A](xs: ArraySeq[A], ys: ArraySeq[A]): ArraySeq[A] =
xs.concat(ys)

override def prependK[A](a: A, fa: ArraySeq[A]): ArraySeq[A] = fa.prepended(a)

override def appendK[A](fa: ArraySeq[A], a: A): ArraySeq[A] = fa.appended(a)

override def algebra[A]: Monoid[ArraySeq[A]] =
new cats.kernel.instances.ArraySeqInstances.ArraySeqMonoid

Expand Down
4 changes: 4 additions & 0 deletions core/src/main/scala-2.13+/cats/instances/lazyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ trait LazyListInstances extends cats.kernel.instances.LazyListInstances {

def combineK[A](x: LazyList[A], y: LazyList[A]): LazyList[A] = x.lazyAppendedAll(y)

override def prependK[A](a: A, fa: LazyList[A]): LazyList[A] = fa.prepended(a)

override def appendK[A](fa: LazyList[A], a: A): LazyList[A] = fa.appended(a)

def pure[A](x: A): LazyList[A] = LazyList(x)

override def map[A, B](fa: LazyList[A])(f: A => B): LazyList[B] =
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala-2.13+/cats/instances/stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ trait StreamInstances extends cats.kernel.instances.StreamInstances {

def combineK[A](x: Stream[A], y: Stream[A]): Stream[A] = x #::: y

override def prependK[A](a: A, fa: Stream[A]): Stream[A] = a #:: fa

def pure[A](x: A): Stream[A] = Stream(x)

override def map[A, B](fa: Stream[A])(f: A => B): Stream[B] =
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/scala/cats/instances/list.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ trait ListInstances extends cats.kernel.instances.ListInstances {
new Traverse[List] with Alternative[List] with Monad[List] with CoflatMap[List] with Align[List] {
def empty[A]: List[A] = Nil

def combineK[A](x: List[A], y: List[A]): List[A] = x ++ y
def combineK[A](x: List[A], y: List[A]): List[A] = x ::: y

override def prependK[A](a: A, fa: List[A]): List[A] = a :: fa

override def appendK[A](fa: List[A], a: A): List[A] = fa :+ a

def pure[A](x: A): List[A] = x :: Nil

Expand Down
5 changes: 4 additions & 1 deletion core/src/main/scala/cats/instances/option.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ trait OptionInstances extends cats.kernel.instances.OptionInstances {

def empty[A]: Option[A] = None

def combineK[A](x: Option[A], y: Option[A]): Option[A] = x.orElse(y)
def combineK[A](x: Option[A], y: Option[A]): Option[A] = if (x.isDefined) x else y

override def prependK[A](a: A, fa: Option[A]): Option[A] = Some(a)
override def appendK[A](fa: Option[A], a: A): Option[A] = if (fa.isDefined) fa else Some(a)

override def combineKEval[A](x: Option[A], y: Eval[Option[A]]): Eval[Option[A]] =
x match {
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/scala/cats/instances/queue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ trait QueueInstances extends cats.kernel.instances.QueueInstances {

def combineK[A](x: Queue[A], y: Queue[A]): Queue[A] = x ++ y

override def prependK[A](a: A, fa: Queue[A]): Queue[A] = a +: fa

override def appendK[A](fa: Queue[A], a: A): Queue[A] = fa.enqueue(a)

def pure[A](x: A): Queue[A] = Queue(x)

override def map[A, B](fa: Queue[A])(f: A => B): Queue[B] =
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/scala/cats/instances/seq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ trait SeqInstances extends cats.kernel.instances.SeqInstances {

def combineK[A](x: Seq[A], y: Seq[A]): Seq[A] = x ++ y

override def prependK[A](a: A, fa: Seq[A]): Seq[A] = a +: fa

override def appendK[A](fa: Seq[A], a: A): Seq[A] = fa :+ a

def pure[A](x: A): Seq[A] = Seq(x)

override def map[A, B](fa: Seq[A])(f: A => B): Seq[B] =
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/scala/cats/instances/vector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ trait VectorInstances extends cats.kernel.instances.VectorInstances {

def combineK[A](x: Vector[A], y: Vector[A]): Vector[A] = x ++ y

override def prependK[A](a: A, fa: Vector[A]): Vector[A] = a +: fa

override def appendK[A](fa: Vector[A], a: A): Vector[A] = fa :+ a

def pure[A](x: A): Vector[A] = Vector(x)

override def map[A, B](fa: Vector[A])(f: A => B): Vector[B] =
Expand Down