-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add index related helpers to Traverse #1761
Merged
kailuowang
merged 7 commits into
typelevel:master
from
andyscott:feature/stateTraverse-and-indexed
Jul 19, 2017
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
50ba29c
Add indexed to Traverse
andyscott 4413058
Test .indexed with a smaller collection so builds aren't terribly slow
andyscott 83a0883
Add mapWithIndex to Traverse; implement indexed in terms of mapWithIndex
andyscott df74985
Add .traverseWithIndex to Traverse
andyscott f6d9ade
Rename indexed to zipWithIndex; Improve tests; add faster overrides
andyscott e0109a7
Force testing of overridden traverse methods for list/vector/stream i…
andyscott 9789c2d
Rename traverseWithIndex to traverseWithIndexM; Change .view to .iter…
andyscott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package cats | ||
package tests | ||
|
||
import org.scalatest.prop.PropertyChecks | ||
import org.scalacheck.Arbitrary | ||
|
||
import cats.instances.all._ | ||
|
||
abstract class TraverseCheck[F[_]: Traverse](name: String)(implicit ArbFInt: Arbitrary[F[Int]]) extends CatsSuite with PropertyChecks { | ||
|
||
test(s"Traverse[$name].zipWithIndex") { | ||
forAll { (fa: F[Int]) => | ||
fa.zipWithIndex.toList should === (fa.toList.zipWithIndex) | ||
} | ||
} | ||
|
||
test(s"Traverse[$name].mapWithIndex") { | ||
forAll { (fa: F[Int], fn: ((Int, Int)) => Int) => | ||
fa.mapWithIndex((a, i) => fn((a, i))).toList should === (fa.toList.zipWithIndex.map(fn)) | ||
} | ||
} | ||
|
||
test(s"Traverse[$name].traverseWithIndexM") { | ||
forAll { (fa: F[Int], fn: ((Int, Int)) => (Int, Int)) => | ||
val left = fa.traverseWithIndexM((a, i) => fn((a, i))).map(_.toList) | ||
val (xs, values) = fa.toList.zipWithIndex.map(fn).unzip | ||
left should === ((xs.combineAll, values)) | ||
} | ||
} | ||
|
||
} | ||
|
||
object TraverseCheck { | ||
// forces testing of the underlying implementation (avoids overridden methods) | ||
abstract class Underlying[F[_]: Traverse](name: String)(implicit ArbFInt: Arbitrary[F[Int]]) | ||
extends TraverseCheck(s"$name (underlying)")(proxyTraverse[F], ArbFInt) | ||
|
||
// proxies a traverse instance so we can test default implementations | ||
// to achieve coverage using default datatype instances | ||
private def proxyTraverse[F[_]: Traverse]: Traverse[F] = new Traverse[F] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is nice. |
||
def foldLeft[A, B](fa: F[A], b: B)(f: (B, A) => B): B = | ||
Traverse[F].foldLeft(fa, b)(f) | ||
def foldRight[A, B](fa: F[A], lb: cats.Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = | ||
Traverse[F].foldRight(fa, lb)(f) | ||
def traverse[G[_]: Applicative, A, B](fa: F[A])(f: A => G[B]): G[F[B]] = | ||
Traverse[F].traverse(fa)(f) | ||
} | ||
} | ||
|
||
class TraverseListCheck extends TraverseCheck[List]("List") | ||
class TraverseStreamCheck extends TraverseCheck[Stream]("Stream") | ||
class TraverseVectorCheck extends TraverseCheck[Vector]("Vector") | ||
|
||
class TraverseListCheckUnderlying extends TraverseCheck.Underlying[List]("List") | ||
class TraverseStreamCheckUnderlying extends TraverseCheck.Underlying[Stream]("Stream") | ||
class TraverseVectorCheckUnderlying extends TraverseCheck.Underlying[Vector]("Vector") | ||
|
||
class TraverseTestsAdditional extends CatsSuite { | ||
|
||
def checkZipWithIndexedStackSafety[F[_]](fromRange: Range => F[Int])(implicit F: Traverse[F]): Unit = { | ||
F.zipWithIndex(fromRange(1 to 70000)) | ||
() | ||
} | ||
|
||
test("Traverse[List].zipWithIndex stack safety") { | ||
checkZipWithIndexedStackSafety[List](_.toList) | ||
} | ||
|
||
test("Traverse[Stream].zipWithIndex stack safety") { | ||
checkZipWithIndexedStackSafety[Stream](_.toStream) | ||
} | ||
|
||
test("Traverse[Vector].zipWithIndex stack safety") { | ||
checkZipWithIndexedStackSafety[Vector](_.toVector) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we override this for
List
,Vector
andStream
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing