-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make the NaturalTransformation lazy in order to not duplicate ef…
…fects for Id ~> Action
- Loading branch information
1 parent
3383b3a
commit 846d0a4
Showing
5 changed files
with
40 additions
and
6 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
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,27 @@ | ||
package user | ||
|
||
import org.specs2.* | ||
import org.specs2.concurrent.* | ||
import org.specs2.control.* | ||
import org.specs2.control.origami.* | ||
import org.specs2.control.producer.* | ||
import org.specs2.fp.* | ||
|
||
class FoldSpec(ee: ExecutionEnv) extends Specification { | ||
def is = s2""" | ||
|
||
A correct implementation of NaturalTransformation from Id to Action must not duplicate effects (see issue #1277) $dontDuplicateEffects | ||
|
||
""" | ||
|
||
def dontDuplicateEffects = { | ||
val p = Producer.emitAll[Action, Int](1, 2, 3) | ||
|
||
// The NaturalTransformation from Id (which is Id[X] X) to Action must | ||
// make sure to not run side effects of X twice. | ||
// This tests guarantees that during the evaluation of `zip` and the various folds | ||
// we don't run the action for collection elements into a mutable map more than necessary. | ||
val vs = p.fold(Folds.list[Int].into[Action] `zip` Folds.list[Int].into[Action]) | ||
vs.run(ee) === (List(1, 2, 3), List(1, 2, 3)) | ||
} | ||
} |
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,10 +1,10 @@ | ||
package org.specs2.fp | ||
|
||
trait NaturalTransformation[-F[_], +G[_]]: | ||
def apply[A](fa: F[A]): G[A] | ||
def apply[A](fa: =>F[A]): G[A] | ||
|
||
object NaturalTransformation: | ||
|
||
given naturalId[M[_]: Monad]: NaturalTransformation[Id, M] with | ||
def apply[A](fa: Id[A]): M[A] = | ||
def apply[A](fa: =>Id[A]): M[A] = | ||
summon[Monad[M]].point(fa) |
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