Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed the Indexable implementation to be derivable from Traverse, t…
…his way more types get Indexable behaviors for free.
- Loading branch information
1 parent
0117e5b
commit b219956
Showing
5 changed files
with
47 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,47 @@ | ||
package checklist | ||
|
||
import scala.language.higherKinds | ||
|
||
trait Indexable[S[_]] { | ||
def zipWithIndex[A](values: S[A]): S[(A, Int)] | ||
def mapWithIndex[A, B](values: S[A])(f: (A, Int) => B): S[B] | ||
def zipWithIndex[A](values: S[A]): S[(A, Int)] = mapWithIndex(values)((a, i) => (a, i)) | ||
} | ||
|
||
object Indexable { | ||
object Indexable extends LowPriorityIndexable { | ||
def apply[S[_]](implicit indexable: Indexable[S]): Indexable[S] = | ||
indexable | ||
|
||
implicit val listIndexable: Indexable[List] = | ||
new Indexable[List] { | ||
def zipWithIndex[A](values: List[A]): List[(A, Int)] = | ||
def mapWithIndex[A, B](values: List[A])(f: (A, Int) => B) = values.zipWithIndex.map { case (a, i) => f(a, i) } | ||
override def zipWithIndex[A](values: List[A]): List[(A, Int)] = | ||
values.zipWithIndex | ||
} | ||
|
||
implicit val vectorIndexable: Indexable[Vector] = | ||
new Indexable[Vector] { | ||
def zipWithIndex[A](values: Vector[A]): Vector[(A, Int)] = | ||
def mapWithIndex[A, B](values: Vector[A])(f: (A, Int) => B) = values.zipWithIndex.map { case (a, i) => f(a, i) } | ||
override def zipWithIndex[A](values: Vector[A]): Vector[(A, Int)] = | ||
values.zipWithIndex | ||
} | ||
|
||
implicit val streamIndexable: Indexable[Stream] = | ||
new Indexable[Stream] { | ||
def zipWithIndex[A](values: Stream[A]): Stream[(A, Int)] = | ||
def mapWithIndex[A, B](values: Stream[A])(f: (A, Int) => B) = values.zipWithIndex.map { case (a, i) => f(a, i) } | ||
override def zipWithIndex[A](values: Stream[A]): Stream[(A, Int)] = | ||
values.zipWithIndex | ||
} | ||
} | ||
} | ||
|
||
trait LowPriorityIndexable { | ||
import cats.Traverse | ||
// Most of the stuff below is stolen from cats 1.0.0-MF's Traverse. Once this lib is on cats 1.0.0, Indexable can disappear because traverse does all of this. | ||
implicit def indexableFromTraverse[S[_]: Traverse]: Indexable[S] = { | ||
new Indexable[S] { | ||
import cats.data.State | ||
import cats.implicits._ | ||
def mapWithIndex[A, B](values: S[A])(f: (A, Int) => B): S[B] = | ||
values.traverse(a => State((s: Int) => (s + 1, f(a, s)))).runA(0).value | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package checklist | ||
|
||
import cats.implicits._ | ||
import cats.{Applicative, Traverse} | ||
|
||
object IndexableSyntax extends IndexableSyntax | ||
|
||
trait IndexableSyntax { | ||
implicit class IndexableOps[S[_], A](sa: S[A])(implicit indexable: Indexable[S]) { | ||
def zipWithIndex: S[(A, Int)] = indexable.zipWithIndex(sa) | ||
def mapWithIndex[B](f: (A, Int) => B): S[B] = indexable.mapWithIndex(sa)(f) | ||
|
||
def traverseWithIndex[F[_]: Applicative, B](f: (A, Int) => F[B])(implicit traverse: Traverse[S]): F[S[B]] = | ||
indexable.mapWithIndex(sa)(f).sequence | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package checklist | ||
|
||
object syntax extends Rule1Syntax with MessageSyntax with CheckedSyntax | ||
object syntax extends Rule1Syntax with MessageSyntax with CheckedSyntax with IndexableSyntax |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters