Skip to content

Commit

Permalink
Merge branch 'main' into add-scaladoc-links
Browse files Browse the repository at this point in the history
  • Loading branch information
reardonj authored Feb 21, 2023
2 parents ce1cb53 + 52c58d2 commit 0e57d12
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 180 deletions.
11 changes: 3 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,9 @@ The documentation for this website is stored alongside the source, in the [docs

### Generating the Site

The microsite generation requires a specific scala version that might
differ from the project's one. We strongly suggest to check the CI's
workflow to discover the proper version:
[CI scala-version matrix](https://github.com/typelevel/cats/blob/main/.github/workflows/ci.yml#L230) and [CI makeMicrosite
command](https://github.com/typelevel/cats/blob/main/.github/workflows/ci.yml#L281)

At the moment the command is:
`sbt ++2.12.15 docs/tlSite`
The command is: `sbt docs/tlSite`

We suggest checking the CI's workflow to discover any changes at [CI site job](https://github.com/typelevel/cats/blob/v2.9.0/.github/workflows/ci.yml#L418).

### Previewing the site

Expand Down
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ lazy val docs = project
import laika.rewrite.link._

laikaConfig.value.withRawContent
.withConfigValue("version", mdocVariables.value("VERSION"))
.withConfigValue(
LinkConfig(apiLinks =
List(
Expand Down
125 changes: 125 additions & 0 deletions core/src/main/scala/cats/NonEmptyReducible.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats

import cats.Foldable.Source
import cats.data.NonEmptyList

/**
* This class defines a `Reducible[F]` in terms of a `Foldable[G]`
* together with a `split` method, `F[A]` => `(A, G[A])`.
*
* This class can be used on any type where the first value (`A`) and
* the "rest" of the values (`G[A]`) can be easily found.
*
* This class is only a helper, does not define a typeclass and should not be used outside of Cats.
* Also see the discussion: PR #3541 and issue #3069.
*/
abstract class NonEmptyReducible[F[_], G[_]](implicit G: Foldable[G]) extends Reducible[F] {
def split[A](fa: F[A]): (A, G[A])

def foldLeft[A, B](fa: F[A], b: B)(f: (B, A) => B): B = {
val (a, ga) = split(fa)
G.foldLeft(ga, f(b, a))(f)
}

def foldRight[A, B](fa: F[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
Always(split(fa)).flatMap { case (a, ga) =>
f(a, G.foldRight(ga, lb)(f))
}

def reduceLeftTo[A, B](fa: F[A])(f: A => B)(g: (B, A) => B): B = {
val (a, ga) = split(fa)
G.foldLeft(ga, f(a))((b, a) => g(b, a))
}

def reduceRightTo[A, B](fa: F[A])(f: A => B)(g: (A, Eval[B]) => Eval[B]): Eval[B] = {
def loop(now: A, source: Source[A]): Eval[B] =
source.uncons match {
case Some((next, s)) => g(now, Eval.defer(loop(next, s.value)))
case None => Eval.later(f(now))
}

Always(split(fa)).flatMap { case (a, ga) =>
Eval.defer(loop(a, Foldable.Source.fromFoldable(ga)))
}
}

override def size[A](fa: F[A]): Long = {
val (_, tail) = split(fa)
1 + G.size(tail)
}

override def get[A](fa: F[A])(idx: Long): Option[A] =
if (idx == 0L) Some(split(fa)._1) else G.get(split(fa)._2)(idx - 1L)

override def fold[A](fa: F[A])(implicit A: Monoid[A]): A = {
val (a, ga) = split(fa)
A.combine(a, G.fold(ga))
}

override def foldM[H[_], A, B](fa: F[A], z: B)(f: (B, A) => H[B])(implicit H: Monad[H]): H[B] = {
val (a, ga) = split(fa)
H.flatMap(f(z, a))(G.foldM(ga, _)(f))
}

override def find[A](fa: F[A])(f: A => Boolean): Option[A] = {
val (a, ga) = split(fa)
if (f(a)) Some(a) else G.find(ga)(f)
}

override def exists[A](fa: F[A])(p: A => Boolean): Boolean = {
val (a, ga) = split(fa)
p(a) || G.exists(ga)(p)
}

override def forall[A](fa: F[A])(p: A => Boolean): Boolean = {
val (a, ga) = split(fa)
p(a) && G.forall(ga)(p)
}

override def toList[A](fa: F[A]): List[A] = {
val (a, ga) = split(fa)
a :: G.toList(ga)
}

override def toNonEmptyList[A](fa: F[A]): NonEmptyList[A] = {
val (a, ga) = split(fa)
NonEmptyList(a, G.toList(ga))
}

override def filter_[A](fa: F[A])(p: A => Boolean): List[A] = {
val (a, ga) = split(fa)
val filteredTail = G.filter_(ga)(p)
if (p(a)) a :: filteredTail else filteredTail
}

override def takeWhile_[A](fa: F[A])(p: A => Boolean): List[A] = {
val (a, ga) = split(fa)
if (p(a)) a :: G.takeWhile_(ga)(p) else Nil
}

override def dropWhile_[A](fa: F[A])(p: A => Boolean): List[A] = {
val (a, ga) = split(fa)
if (p(a)) G.dropWhile_(ga)(p) else a :: G.toList(ga)
}
}
100 changes: 0 additions & 100 deletions core/src/main/scala/cats/Reducible.scala
Original file line number Diff line number Diff line change
Expand Up @@ -435,103 +435,3 @@ object Reducible {
object nonInheritedOps extends ToReducibleOps

}

/**
* This class defines a `Reducible[F]` in terms of a `Foldable[G]`
* together with a `split` method, `F[A]` => `(A, G[A])`.
*
* This class can be used on any type where the first value (`A`) and
* the "rest" of the values (`G[A]`) can be easily found.
*
* This class is only a helper, does not define a typeclass and should not be used outside of Cats.
* Also see the discussion: PR #3541 and issue #3069.
*/
abstract class NonEmptyReducible[F[_], G[_]](implicit G: Foldable[G]) extends Reducible[F] {
def split[A](fa: F[A]): (A, G[A])

def foldLeft[A, B](fa: F[A], b: B)(f: (B, A) => B): B = {
val (a, ga) = split(fa)
G.foldLeft(ga, f(b, a))(f)
}

def foldRight[A, B](fa: F[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
Always(split(fa)).flatMap { case (a, ga) =>
f(a, G.foldRight(ga, lb)(f))
}

def reduceLeftTo[A, B](fa: F[A])(f: A => B)(g: (B, A) => B): B = {
val (a, ga) = split(fa)
G.foldLeft(ga, f(a))((b, a) => g(b, a))
}

def reduceRightTo[A, B](fa: F[A])(f: A => B)(g: (A, Eval[B]) => Eval[B]): Eval[B] = {
def loop(now: A, source: Source[A]): Eval[B] =
source.uncons match {
case Some((next, s)) => g(now, Eval.defer(loop(next, s.value)))
case None => Eval.later(f(now))
}

Always(split(fa)).flatMap { case (a, ga) =>
Eval.defer(loop(a, Foldable.Source.fromFoldable(ga)))
}
}

override def size[A](fa: F[A]): Long = {
val (_, tail) = split(fa)
1 + G.size(tail)
}

override def get[A](fa: F[A])(idx: Long): Option[A] =
if (idx == 0L) Some(split(fa)._1) else G.get(split(fa)._2)(idx - 1L)

override def fold[A](fa: F[A])(implicit A: Monoid[A]): A = {
val (a, ga) = split(fa)
A.combine(a, G.fold(ga))
}

override def foldM[H[_], A, B](fa: F[A], z: B)(f: (B, A) => H[B])(implicit H: Monad[H]): H[B] = {
val (a, ga) = split(fa)
H.flatMap(f(z, a))(G.foldM(ga, _)(f))
}

override def find[A](fa: F[A])(f: A => Boolean): Option[A] = {
val (a, ga) = split(fa)
if (f(a)) Some(a) else G.find(ga)(f)
}

override def exists[A](fa: F[A])(p: A => Boolean): Boolean = {
val (a, ga) = split(fa)
p(a) || G.exists(ga)(p)
}

override def forall[A](fa: F[A])(p: A => Boolean): Boolean = {
val (a, ga) = split(fa)
p(a) && G.forall(ga)(p)
}

override def toList[A](fa: F[A]): List[A] = {
val (a, ga) = split(fa)
a :: G.toList(ga)
}

override def toNonEmptyList[A](fa: F[A]): NonEmptyList[A] = {
val (a, ga) = split(fa)
NonEmptyList(a, G.toList(ga))
}

override def filter_[A](fa: F[A])(p: A => Boolean): List[A] = {
val (a, ga) = split(fa)
val filteredTail = G.filter_(ga)(p)
if (p(a)) a :: filteredTail else filteredTail
}

override def takeWhile_[A](fa: F[A])(p: A => Boolean): List[A] = {
val (a, ga) = split(fa)
if (p(a)) a :: G.takeWhile_(ga)(p) else Nil
}

override def dropWhile_[A](fa: F[A])(p: A => Boolean): List[A] = {
val (a, ga) = split(fa)
if (p(a)) G.dropWhile_(ga)(p) else a :: G.toList(ga)
}
}
Loading

0 comments on commit 0e57d12

Please sign in to comment.