|
| 1 | +/* |
| 2 | + * Copyright (c) 2015 Typelevel |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | + * this software and associated documentation files (the "Software"), to deal in |
| 6 | + * the Software without restriction, including without limitation the rights to |
| 7 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 8 | + * the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | + * subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 16 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 17 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 18 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package cats |
| 23 | + |
| 24 | +import cats.Foldable.Source |
| 25 | +import cats.data.NonEmptyList |
| 26 | + |
| 27 | +/** |
| 28 | + * This class defines a `Reducible[F]` in terms of a `Foldable[G]` |
| 29 | + * together with a `split` method, `F[A]` => `(A, G[A])`. |
| 30 | + * |
| 31 | + * This class can be used on any type where the first value (`A`) and |
| 32 | + * the "rest" of the values (`G[A]`) can be easily found. |
| 33 | + * |
| 34 | + * This class is only a helper, does not define a typeclass and should not be used outside of Cats. |
| 35 | + * Also see the discussion: PR #3541 and issue #3069. |
| 36 | + */ |
| 37 | +abstract class NonEmptyReducible[F[_], G[_]](implicit G: Foldable[G]) extends Reducible[F] { |
| 38 | + def split[A](fa: F[A]): (A, G[A]) |
| 39 | + |
| 40 | + def foldLeft[A, B](fa: F[A], b: B)(f: (B, A) => B): B = { |
| 41 | + val (a, ga) = split(fa) |
| 42 | + G.foldLeft(ga, f(b, a))(f) |
| 43 | + } |
| 44 | + |
| 45 | + def foldRight[A, B](fa: F[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = |
| 46 | + Always(split(fa)).flatMap { case (a, ga) => |
| 47 | + f(a, G.foldRight(ga, lb)(f)) |
| 48 | + } |
| 49 | + |
| 50 | + def reduceLeftTo[A, B](fa: F[A])(f: A => B)(g: (B, A) => B): B = { |
| 51 | + val (a, ga) = split(fa) |
| 52 | + G.foldLeft(ga, f(a))((b, a) => g(b, a)) |
| 53 | + } |
| 54 | + |
| 55 | + def reduceRightTo[A, B](fa: F[A])(f: A => B)(g: (A, Eval[B]) => Eval[B]): Eval[B] = { |
| 56 | + def loop(now: A, source: Source[A]): Eval[B] = |
| 57 | + source.uncons match { |
| 58 | + case Some((next, s)) => g(now, Eval.defer(loop(next, s.value))) |
| 59 | + case None => Eval.later(f(now)) |
| 60 | + } |
| 61 | + |
| 62 | + Always(split(fa)).flatMap { case (a, ga) => |
| 63 | + Eval.defer(loop(a, Foldable.Source.fromFoldable(ga))) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + override def size[A](fa: F[A]): Long = { |
| 68 | + val (_, tail) = split(fa) |
| 69 | + 1 + G.size(tail) |
| 70 | + } |
| 71 | + |
| 72 | + override def get[A](fa: F[A])(idx: Long): Option[A] = |
| 73 | + if (idx == 0L) Some(split(fa)._1) else G.get(split(fa)._2)(idx - 1L) |
| 74 | + |
| 75 | + override def fold[A](fa: F[A])(implicit A: Monoid[A]): A = { |
| 76 | + val (a, ga) = split(fa) |
| 77 | + A.combine(a, G.fold(ga)) |
| 78 | + } |
| 79 | + |
| 80 | + override def foldM[H[_], A, B](fa: F[A], z: B)(f: (B, A) => H[B])(implicit H: Monad[H]): H[B] = { |
| 81 | + val (a, ga) = split(fa) |
| 82 | + H.flatMap(f(z, a))(G.foldM(ga, _)(f)) |
| 83 | + } |
| 84 | + |
| 85 | + override def find[A](fa: F[A])(f: A => Boolean): Option[A] = { |
| 86 | + val (a, ga) = split(fa) |
| 87 | + if (f(a)) Some(a) else G.find(ga)(f) |
| 88 | + } |
| 89 | + |
| 90 | + override def exists[A](fa: F[A])(p: A => Boolean): Boolean = { |
| 91 | + val (a, ga) = split(fa) |
| 92 | + p(a) || G.exists(ga)(p) |
| 93 | + } |
| 94 | + |
| 95 | + override def forall[A](fa: F[A])(p: A => Boolean): Boolean = { |
| 96 | + val (a, ga) = split(fa) |
| 97 | + p(a) && G.forall(ga)(p) |
| 98 | + } |
| 99 | + |
| 100 | + override def toList[A](fa: F[A]): List[A] = { |
| 101 | + val (a, ga) = split(fa) |
| 102 | + a :: G.toList(ga) |
| 103 | + } |
| 104 | + |
| 105 | + override def toNonEmptyList[A](fa: F[A]): NonEmptyList[A] = { |
| 106 | + val (a, ga) = split(fa) |
| 107 | + NonEmptyList(a, G.toList(ga)) |
| 108 | + } |
| 109 | + |
| 110 | + override def filter_[A](fa: F[A])(p: A => Boolean): List[A] = { |
| 111 | + val (a, ga) = split(fa) |
| 112 | + val filteredTail = G.filter_(ga)(p) |
| 113 | + if (p(a)) a :: filteredTail else filteredTail |
| 114 | + } |
| 115 | + |
| 116 | + override def takeWhile_[A](fa: F[A])(p: A => Boolean): List[A] = { |
| 117 | + val (a, ga) = split(fa) |
| 118 | + if (p(a)) a :: G.takeWhile_(ga)(p) else Nil |
| 119 | + } |
| 120 | + |
| 121 | + override def dropWhile_[A](fa: F[A])(p: A => Boolean): List[A] = { |
| 122 | + val (a, ga) = split(fa) |
| 123 | + if (p(a)) G.dropWhile_(ga)(p) else a :: G.toList(ga) |
| 124 | + } |
| 125 | +} |
0 commit comments