diff --git a/each/src/main/scala-2.13/com/thoughtworks/each/macrocompat.scala b/each/src/main/scala-2.13/com/thoughtworks/each/macrocompat.scala index 2c8bf11..370975a 100644 --- a/each/src/main/scala-2.13/com/thoughtworks/each/macrocompat.scala +++ b/each/src/main/scala-2.13/com/thoughtworks/each/macrocompat.scala @@ -4,4 +4,4 @@ import scala.annotation.StaticAnnotation private[each] object macrocompat { class bundle extends StaticAnnotation -} \ No newline at end of file +} diff --git a/each/src/main/scala/com/thoughtworks/each/Monadic.scala b/each/src/main/scala/com/thoughtworks/each/Monadic.scala index 2117185..cd1793e 100644 --- a/each/src/main/scala/com/thoughtworks/each/Monadic.scala +++ b/each/src/main/scala/com/thoughtworks/each/Monadic.scala @@ -12,7 +12,7 @@ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/ + */ package com.thoughtworks.each @@ -27,12 +27,11 @@ import scalaz._ import scalaz.effect.MonadCatchIO import scalaz.syntax.{FoldableOps, MonadPlusOps, TraverseOps} -/** - * @author 杨博 (Yang Bo) <pop.atry@gmail.com> +/** @author + * 杨博 (Yang Bo) <pop.atry@gmail.com> */ object Monadic { - @inline implicit final class ToMonadicLoopOps[F[_], A](underlying: F[A]) { @@ -41,7 +40,8 @@ object Monadic { } @inline - implicit def getUnderlying[F[_], A](monadicLoop: MonadicLoop[F, A]): F[A] = monadicLoop.underlying + implicit def getUnderlying[F[_], A](monadicLoop: MonadicLoop[F, A]): F[A] = + monadicLoop.underlying object MonadicLoop { @@ -79,7 +79,8 @@ object Monadic { } def flatMap(f: Tree)(traverse: Tree, bind: Tree): Tree = { - val q"$monadicLoop.flatMap[$b]($f)($traverse, $bind)" = c.macroApplication + val q"$monadicLoop.flatMap[$b]($f)($traverse, $bind)" = + c.macroApplication val monadicLoopName = TermName(c.freshName("monadicLoop")) q""" val $monadicLoopName = $monadicLoop @@ -94,7 +95,8 @@ object Monadic { } def filter(f: Tree)(traverse: Tree, monadPlus: Tree): Tree = { - val q"$monadicLoop.${TermName("filter" | "withFilter")}($f)($traverse, $monadPlus)" = c.macroApplication + val q"$monadicLoop.${TermName("filter" | "withFilter")}($f)($traverse, $monadPlus)" = + c.macroApplication val monadicLoopName = TermName(c.freshName("monadicLoop")) q""" val $monadicLoopName = $monadicLoop @@ -110,17 +112,23 @@ object Monadic { } @inline - implicit def toFoldableOps[F[_] : Foldable, A](monadicLoop: MonadicLoop[F, A]): FoldableOps[F, A] = { + implicit def toFoldableOps[F[_]: Foldable, A]( + monadicLoop: MonadicLoop[F, A] + ): FoldableOps[F, A] = { scalaz.syntax.foldable.ToFoldableOps(monadicLoop.underlying) } @inline - implicit def toTraverseOps[F[_] : Traverse, A](monadicLoop: MonadicLoop[F, A]): TraverseOps[F, A] = { + implicit def toTraverseOps[F[_]: Traverse, A]( + monadicLoop: MonadicLoop[F, A] + ): TraverseOps[F, A] = { scalaz.syntax.traverse.ToTraverseOps(monadicLoop.underlying) } @inline - implicit def toMonadPlusOps[F[_] : MonadPlus, A](monadicLoop: MonadicLoop[F, A]): MonadPlusOps[F, A] = { + implicit def toMonadPlusOps[F[_]: MonadPlus, A]( + monadicLoop: MonadicLoop[F, A] + ): MonadPlusOps[F, A] = { scalaz.syntax.monadPlus.ToMonadPlusOps(monadicLoop.underlying) } @@ -131,7 +139,8 @@ object Monadic { Use `@monadic[X] def f = { ... }` instead of `monadic[X] { ... }`. Note that you can remove `.monadicLoop` in `@monadic` methods. """, - since = "1.0.1") + since = "1.0.1" + ) final class MonadicLoop[F0[_], A](val underlying: F0[A]) { type F[X] = F0[X] @@ -139,33 +148,52 @@ object Monadic { type Element = A @inline - def toFoldableOps(implicit foldable: Foldable[F]) = scalaz.syntax.foldable.ToFoldableOps(underlying) + def toFoldableOps(implicit foldable: Foldable[F]) = + scalaz.syntax.foldable.ToFoldableOps(underlying) @inline - def toTraverseOps(implicit traverse: Traverse[F]) = scalaz.syntax.traverse.ToTraverseOps(underlying) + def toTraverseOps(implicit traverse: Traverse[F]) = + scalaz.syntax.traverse.ToTraverseOps(underlying) - def foreach[U](f: A => U)(implicit foldable: Foldable[F]): Unit = macro MonadicLoop.MacroBundle.foreach + def foreach[U](f: A => U)(implicit foldable: Foldable[F]): Unit = + macro MonadicLoop.MacroBundle.foreach - def map[B](f: A => B)(implicit traverse: Traverse[F]): MonadicLoop[F, B] = macro MonadicLoop.MacroBundle.map + def map[B](f: A => B)(implicit traverse: Traverse[F]): MonadicLoop[F, B] = + macro MonadicLoop.MacroBundle.map - def flatMap[B](f: A => F[B])(implicit traverse: Traverse[F], bind: Bind[F]): MonadicLoop[F, B] = macro MonadicLoop.MacroBundle.flatMap + def flatMap[B]( + f: A => F[B] + )(implicit traverse: Traverse[F], bind: Bind[F]): MonadicLoop[F, B] = + macro MonadicLoop.MacroBundle.flatMap - def filter(f: A => Boolean)(implicit traverse: Traverse[F], monadPlus: MonadPlus[F]): MonadicLoop[F, A] = macro MonadicLoop.MacroBundle.filter + def filter(f: A => Boolean)(implicit + traverse: Traverse[F], + monadPlus: MonadPlus[F] + ): MonadicLoop[F, A] = macro MonadicLoop.MacroBundle.filter - def withFilter(f: A => Boolean)(implicit traverse: Traverse[F], monadPlus: MonadPlus[F]): MonadicLoop[F, A] = macro MonadicLoop.MacroBundle.filter + def withFilter(f: A => Boolean)(implicit + traverse: Traverse[F], + monadPlus: MonadPlus[F] + ): MonadicLoop[F, A] = macro MonadicLoop.MacroBundle.filter } - /** - * An implicit view to enable `for` `yield` comprehension for a monadic value. + /** An implicit view to enable `for` `yield` comprehension for a monadic + * value. * - * @param v the monadic value. - * @param F0 a helper to infer types. - * @tparam FA type of the monadic value. - * @return the temporary wrapper that contains the `each` method. + * @param v + * the monadic value. + * @param F0 + * a helper to infer types. + * @tparam FA + * type of the monadic value. + * @return + * the temporary wrapper that contains the `each` method. */ @inline - implicit def toMonadicLoopOpsUnapply[FA](v: FA)(implicit F0: Unapply[Foldable, FA]) = { + implicit def toMonadicLoopOpsUnapply[FA]( + v: FA + )(implicit F0: Unapply[Foldable, FA]) = { new ToMonadicLoopOps[F0.M, F0.A](F0(v)) } @@ -191,12 +219,14 @@ object Monadic { } - /** - * The temporary wrapper that contains the `each` method. + /** The temporary wrapper that contains the `each` method. * - * @param underlying the underlying monadic value. - * @tparam M0 the higher kinded type of the monadic value. - * @tparam A0 the element type of of the monadic value. + * @param underlying + * the underlying monadic value. + * @tparam M0 + * the higher kinded type of the monadic value. + * @tparam A0 + * the element type of of the monadic value. */ final case class EachOps[M0[_], A0](underlying: M0[A0]) { @@ -204,49 +234,54 @@ object Monadic { type A = A0 - /** - * Semantically, returns the result in the monadic value. + /** Semantically, returns the result in the monadic value. * - * This macro must be inside a `monadic` - * or a `catchIoMonadic` block. + * This macro must be inside a `monadic` or a `catchIoMonadic` block. * - * This is not a real method, thus it will never actually execute. - * Instead, the call to this method will be transformed to a monadic expression. - * The actually result is passing as a parameter to some [[scalaz.Monad#bind]] and [[scalaz.Monad#point]] calls - * instead of as a return value. + * This is not a real method, thus it will never actually execute. Instead, + * the call to this method will be transformed to a monadic expression. The + * actually result is passing as a parameter to some [[scalaz.Monad#bind]] + * and [[scalaz.Monad#point]] calls instead of as a return value. * - * @return the result in the monadic value. + * @return + * the result in the monadic value. */ def each: A = macro EachOps.MacroBundle.each } - /** - * An implicit view to enable `.each` for a monadic value. + /** An implicit view to enable `.each` for a monadic value. * - * @param v the monadic value. - * @param F0 a helper to infer types. - * @tparam FA type of the monadic value. - * @return the temporary wrapper that contains the `each` method. + * @param v + * the monadic value. + * @param F0 + * a helper to infer types. + * @tparam FA + * type of the monadic value. + * @return + * the temporary wrapper that contains the `each` method. */ @inline - implicit def toEachOpsUnapply[FA](v: FA)(implicit F0: Unapply[Bind, FA]): EachOps[F0.M, F0.A] = new EachOps[F0.M, F0.A](F0(v)) + implicit def toEachOpsUnapply[FA](v: FA)(implicit + F0: Unapply[Bind, FA] + ): EachOps[F0.M, F0.A] = new EachOps[F0.M, F0.A](F0(v)) - /** - * An implicit view to enable `.each` for a monadic value. + /** An implicit view to enable `.each` for a monadic value. * - * @param v the monadic value. - * @return the temporary wrapper that contains the `each` method. + * @param v + * the monadic value. + * @return + * the temporary wrapper that contains the `each` method. */ @inline implicit def toEachOps[F[_], A](v: F[A]): EachOps[F, A] = new EachOps(v) @bundle - final class AnnotationBundle(context: whitebox.Context) extends Preprocessor(context) { + final class AnnotationBundle(context: whitebox.Context) + extends Preprocessor(context) { import c.universe._ - private def macroTransform(m: Tree, annottees: Seq[Tree]): Tree = { val (f, tc) = c.macroApplication match { @@ -261,8 +296,10 @@ object Monadic { val eachOpsName = TermName(c.freshName("eachOps")) val toEachOpsName = TermName(c.freshName("ToEachOps")) - replaceDefBody(annottees, { body => - q""" + replaceDefBody( + annottees, + { body => + q""" _root_.com.thoughtworks.sde.core.MonadicFactory[ $m, $f @@ -277,11 +314,15 @@ object Monadic { ${(new ComprehensionTransformer).transform(body)} }($tc) """ - }) + } + ) } def throwableMonadic(annottees: Tree*): Tree = { - macroTransform(tq"_root_.com.thoughtworks.each.Monadic.MonadThrowable", annottees) + macroTransform( + tq"_root_.com.thoughtworks.each.Monadic.MonadThrowable", + annottees + ) } def monadic(annottees: Tree*): Tree = { @@ -294,15 +335,19 @@ object Monadic { } - /** - * @usecase def monadic[F[_]](body: AnyRef)(implicit monad: Monad[F]): F[body.type] = ??? + /** @usecase def monadic[F[_]](body: AnyRef)(implicit monad: Monad[F]): F[body.type] = ??? * - * Captures all the result in the `body` and converts them into a `F`. + * Captures all the result in the `body` and converts them into a `F`. * - * Note that `body` must not contain any `try` / `catch` / `throw` expressions. - * @tparam F the higher kinded type of the monadic expression. - * @param body the imperative style expressions that will be transform to monadic style. - * @param monad the monad that executes expressions in `body`. + * Note that `body` must not contain any `try` / `catch` / `throw` + * expressions. + * @tparam F + * the higher kinded type of the monadic expression. + * @param body + * the imperative style expressions that will be transform to monadic + * style. + * @param monad + * the monad that executes expressions in `body`. * @return */ @inline @@ -313,15 +358,18 @@ object Monadic { def macroTransform(annottees: Any*): Any = macro AnnotationBundle.monadic } - /** - * @usecase def catchIoMonadic[F[_]](body: AnyRef)(implicit monad: MonadCatchIO[F]): F[body.type] = ??? + /** @usecase def catchIoMonadic[F[_]](body: AnyRef)(implicit monad: MonadCatchIO[F]): F[body.type] = ??? * - * Captures all the result in the `body` and converts them into a `F`. + * Captures all the result in the `body` and converts them into a `F`. * - * Note that `body` may contain any `try` / `catch` / `throw` expressions. - * @tparam F the higher kinded type of the monadic expression. - * @param body the imperative style expressions that will be transform to monadic style. - * @param monad the monad that executes expressions in `body`. + * Note that `body` may contain any `try` / `catch` / `throw` expressions. + * @tparam F + * the higher kinded type of the monadic expression. + * @param body + * the imperative style expressions that will be transform to monadic + * style. + * @param monad + * the monad that executes expressions in `body`. * @return */ @inline @@ -329,43 +377,50 @@ object Monadic { @compileTimeOnly("enable macro paradise to expand macro annotations") final class catchIoMonadic[F[_]] extends StaticAnnotation { - def macroTransform(annottees: Any*): Any = macro AnnotationBundle.catchIoMonadic + def macroTransform(annottees: Any*): Any = + macro AnnotationBundle.catchIoMonadic } // TODO: create Unapply instead @inline - implicit def eitherTMonadThrowable[F[_], G[_[_], _]](implicit F0: Monad[({type g[y] = G[F, y]})#g]): MonadThrowable[ - ({type f[x] = EitherT[({type g[y] = G[F, y]})#g, Throwable, x]})#f - ] = { - EitherT.eitherTMonadError[({type g[y] = G[F, y]})#g, Throwable] + implicit def eitherTMonadThrowable[F[_], G[_[_], _]](implicit + F0: Monad[({ type g[y] = G[F, y] })#g] + ): MonadThrowable[ + ({ type f[x] = EitherT[({ type g[y] = G[F, y] })#g, Throwable, x] })#f + ] = { + EitherT.eitherTMonadError[({ type g[y] = G[F, y] })#g, Throwable] } @inline - implicit def lazyEitherTMonadThrowable[F[_], G[_[_], _]](implicit F0: Monad[({type g[y] = G[F, y]})#g]): MonadThrowable[ - ({type f[x] = LazyEitherT[({type g[y] = G[F, y]})#g, Throwable, x]})#f - ] = { - LazyEitherT.lazyEitherTMonadError[({type g[y] = G[F, y]})#g, Throwable] + implicit def lazyEitherTMonadThrowable[F[_], G[_[_], _]](implicit + F0: Monad[({ type g[y] = G[F, y] })#g] + ): MonadThrowable[ + ({ type f[x] = LazyEitherT[({ type g[y] = G[F, y] })#g, Throwable, x] })#f + ] = { + LazyEitherT.lazyEitherTMonadError[({ type g[y] = G[F, y] })#g, Throwable] } - - /** - * A [[scalaz.Monad]] that supports exception handling. + /** A [[scalaz.Monad]] that supports exception handling. * * Note this is a simplified version of [[scalaz.MonadError]]. * - * @tparam F the higher kinded type of the monad. + * @tparam F + * the higher kinded type of the monad. */ type MonadThrowable[F[_]] = MonadError[F, Throwable] - /** - * @usecase def throwableMonadic[F[_]](body: AnyRef)(implicit monad: MonadThrowable[F]): F[body.type] = ??? + /** @usecase def throwableMonadic[F[_]](body: AnyRef)(implicit monad: MonadThrowable[F]): F[body.type] = ??? * - * Captures all the result in the `body` and converts them into a `F`. + * Captures all the result in the `body` and converts them into a `F`. * - * Note that `body` may contain any `try` / `catch` / `throw` expressions. - * @tparam F the higher kinded type of the monadic expression. - * @param body the imperative style expressions that will be transform to monadic style. - * @param monad the monad that executes expressions in `body`. + * Note that `body` may contain any `try` / `catch` / `throw` expressions. + * @tparam F + * the higher kinded type of the monadic expression. + * @param body + * the imperative style expressions that will be transform to monadic + * style. + * @param monad + * the monad that executes expressions in `body`. * @return */ @inline @@ -373,7 +428,8 @@ object Monadic { @compileTimeOnly("enable macro paradise to expand macro annotations") final class throwableMonadic[F[_]] extends StaticAnnotation { - def macroTransform(annottees: Any*): Any = macro AnnotationBundle.throwableMonadic + def macroTransform(annottees: Any*): Any = + macro AnnotationBundle.throwableMonadic } } diff --git a/each/src/main/scala/com/thoughtworks/each/package.scala b/each/src/main/scala/com/thoughtworks/each/package.scala index 24edb9b..f7546bb 100644 --- a/each/src/main/scala/com/thoughtworks/each/package.scala +++ b/each/src/main/scala/com/thoughtworks/each/package.scala @@ -1,12 +1,12 @@ package com.thoughtworks -/** - * @author 杨博 (Yang Bo) <pop.atry@gmail.com> +/** @author + * 杨博 (Yang Bo) <pop.atry@gmail.com> */ package object each { - /** - * Contains implicit methods to work with types that support `for`/`yield` comprehension. + /** Contains implicit methods to work with types that support `for`/`yield` + * comprehension. */ val ComprehensionImplicits = sde.comprehensionMonad.ComprehensionMonad diff --git a/each/src/test/scala-2.11/com/thoughtworks/each/ReportingTest.scala b/each/src/test/scala-2.11/com/thoughtworks/each/ReportingTest.scala index 40777dc..57981ce 100644 --- a/each/src/test/scala-2.11/com/thoughtworks/each/ReportingTest.scala +++ b/each/src/test/scala-2.11/com/thoughtworks/each/ReportingTest.scala @@ -9,14 +9,14 @@ import scalaz._ import scalaz.std.list._ import scalaz.syntax.traverse._ - class ReportingTest { private trait AppAction[A] private case object GetEmailList extends AppAction[Throwable \/ List[String]] - private case class GetContactNameByEmail(email: String) extends AppAction[Throwable \/ String] + private case class GetContactNameByEmail(email: String) + extends AppAction[Throwable \/ String] private type FreeCommand[A] = Free[AppAction, A] @@ -28,7 +28,9 @@ class ReportingTest { import scala.language.{higherKinds, implicitConversions} - private implicit def cast[From, To](from: Script[From])(implicit view: From => To): Script[To] = { + private implicit def cast[From, To]( + from: Script[From] + )(implicit view: From => To): Script[To] = { Monad[Script].map[From, To](from)(view) } @@ -37,11 +39,11 @@ class ReportingTest { { - (for { - email: String <- emailList.monadicLoop - if email.matches( """[a-z.\-_]+@[a-z.\-_]+""") - } yield { - + (for { + email: String <- emailList.monadicLoop + if email.matches("""[a-z.\-_]+@[a-z.\-_]+""") + } yield { + @@ -49,18 +51,19 @@ class ReportingTest { {email} - }).toList - }
{toScript(GetContactNameByEmail(email)).each}
+ }).toList + } } private def rawScript: Script[xml.Elem] = { toScript(GetEmailList).flatMap { emailList => - emailList.traverseM[Script, xml.Elem] { email => - toScript(GetContactNameByEmail(email)).map { name => - if (email.matches( """[^@]+@[^@]+""")) { - List( + emailList + .traverseM[Script, xml.Elem] { email => + toScript(GetContactNameByEmail(email)).map { name => + if (email.matches("""[^@]+@[^@]+""")) { + List( {name} @@ -68,19 +71,20 @@ class ReportingTest { {email} ) - } else { - Nil + } else { + Nil + } } } - }.map { trs => - + .map { trs => + {trs}
- } + } } } @@ -97,23 +101,32 @@ class ReportingTest { case GetEmailList => { \/-(Data.keys.toList) } - case GetContactNameByEmail(email) =>Data.get(email) match { - case None => { - -\/(new NoSuchElementException) + case GetContactNameByEmail(email) => + Data.get(email) match { + case None => { + -\/(new NoSuchElementException) + } + case Some(name) => { + \/-(name) + } } - case Some(name) => { - \/-(name) - } - } } } } val rawHtml = - xml.Xhtml.toXhtml(xml.Utility.trim(rawScript.run.foldMap(interpreter).fold(throw _, identity))) + xml.Xhtml.toXhtml( + xml.Utility.trim( + rawScript.run.foldMap(interpreter).fold(throw _, identity) + ) + ) val eachHtml = - xml.Xhtml.toXhtml(xml.Utility.trim(eachScript.run.foldMap(interpreter).fold(throw _, identity))) + xml.Xhtml.toXhtml( + xml.Utility.trim( + eachScript.run.foldMap(interpreter).fold(throw _, identity) + ) + ) Assert.assertEquals(rawHtml, eachHtml) @@ -132,14 +145,15 @@ class ReportingTest { case GetEmailList => { Future.successful(\/-(Data.keys.toList)) } - case GetContactNameByEmail(email) => Future.successful(Data.get(email) match { - case None => { - -\/(new NoSuchElementException) - } - case Some(name) => { - \/-(name) - } - }) + case GetContactNameByEmail(email) => + Future.successful(Data.get(email) match { + case None => { + -\/(new NoSuchElementException) + } + case Some(name) => { + \/-(name) + } + }) } } } @@ -148,10 +162,22 @@ class ReportingTest { import scalaz.std.scalaFuture._ val rawHtml = - xml.Xhtml.toXhtml(xml.Utility.trim(Await.result(rawScript.run.foldMap(interpreter), Duration.Inf).fold(throw _, identity))) + xml.Xhtml.toXhtml( + xml.Utility.trim( + Await + .result(rawScript.run.foldMap(interpreter), Duration.Inf) + .fold(throw _, identity) + ) + ) val eachHtml = - xml.Xhtml.toXhtml(xml.Utility.trim(Await.result(eachScript.run.foldMap(interpreter), Duration.Inf).fold(throw _, identity))) + xml.Xhtml.toXhtml( + xml.Utility.trim( + Await + .result(eachScript.run.foldMap(interpreter), Duration.Inf) + .fold(throw _, identity) + ) + ) Assert.assertEquals(rawHtml, eachHtml) diff --git a/each/src/test/scala-2.11/com/thoughtworks/each/TraverseComprehensionTest211.scala b/each/src/test/scala-2.11/com/thoughtworks/each/TraverseComprehensionTest211.scala index ae43060..2a73998 100644 --- a/each/src/test/scala-2.11/com/thoughtworks/each/TraverseComprehensionTest211.scala +++ b/each/src/test/scala-2.11/com/thoughtworks/each/TraverseComprehensionTest211.scala @@ -17,8 +17,8 @@ class TraverseComprehensionTest211 { i <- List(300, 20).monadicLoop if i > 100 } yield { - i + n.each - }).underlying + i + n.each + }).underlying } Assert.assertEquals(Some(List(4300)), result) } @@ -33,8 +33,8 @@ class TraverseComprehensionTest211 { if i > n.each - 3900 a = i + j } yield { - a + n.each * k.length - }).underlying + a + n.each * k.length + }).underlying } Assert.assertEquals(Some(List(66300, 612300)), result) diff --git a/each/src/test/scala/com/thoughtworks/each/AnnotationTest.scala b/each/src/test/scala/com/thoughtworks/each/AnnotationTest.scala index 0a9d31f..a9dbe1e 100644 --- a/each/src/test/scala/com/thoughtworks/each/AnnotationTest.scala +++ b/each/src/test/scala/com/thoughtworks/each/AnnotationTest.scala @@ -12,7 +12,7 @@ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/ + */ package com.thoughtworks.each @@ -21,15 +21,16 @@ import org.junit.{Assert, Test} import scalaz.std.option._ import scalaz.std.list._ -/** - * @author 杨博 (Yang Bo) <pop.atry@gmail.com> +/** @author + * 杨博 (Yang Bo) <pop.atry@gmail.com> */ class AnnotationTest { @Test def testForeach(): Unit = { val n = Some(10) - @monadic[Option] val result = { + @monadic[Option] + val result = { var count = 1 for (i <- List(300, 20)) { count += i * n.each @@ -42,7 +43,8 @@ class AnnotationTest { @Test def testMap(): Unit = { val n = Some(4000) - @monadic[Option] val result = { + @monadic[Option] + val result = { for (i <- List(300, 20)) yield { i + n.each } @@ -53,7 +55,8 @@ class AnnotationTest { @Test def testFlatMap(): Unit = { val n = Some(4000) - @monadic[Option] val result = { + @monadic[Option] + val result = { for { i <- List(300, 20) j <- List(50000, 600000) @@ -68,7 +71,8 @@ class AnnotationTest { def testFilter(): Unit = { val n = Some(4000) - @monadic[Option] val result = { + @monadic[Option] + val result = { for { i <- List(300, 20) if i > 100 diff --git a/each/src/test/scala/com/thoughtworks/each/ComprehensionImplicitsTest.scala b/each/src/test/scala/com/thoughtworks/each/ComprehensionImplicitsTest.scala index 816cfcd..ce0a4a4 100644 --- a/each/src/test/scala/com/thoughtworks/each/ComprehensionImplicitsTest.scala +++ b/each/src/test/scala/com/thoughtworks/each/ComprehensionImplicitsTest.scala @@ -12,7 +12,7 @@ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/ + */ package com.thoughtworks.each @@ -23,28 +23,41 @@ class ComprehensionImplicitsTest { @Test def testListPoint(): Unit = { val seqApplicative = ComprehensionImplicits.comprehensionMonad[List] - Assert.assertEquals(List("hello, applicative"), seqApplicative.point("hello, applicative")) + Assert.assertEquals( + List("hello, applicative"), + seqApplicative.point("hello, applicative") + ) } @Test def testSeqPoint(): Unit = { val seqApplicative = ComprehensionImplicits.comprehensionMonad[Seq] - Assert.assertEquals(Seq("hello, applicative"), seqApplicative.point("hello, applicative")) + Assert.assertEquals( + Seq("hello, applicative"), + seqApplicative.point("hello, applicative") + ) } @Test def testSeqAp(): Unit = { val seqApplicative = ComprehensionImplicits.comprehensionMonad[Seq] - Assert.assertEquals(Seq("Hello1!", "Hello2!", "Hello1?", "Hello2?"), seqApplicative.ap(Seq("Hello1", "Hello2"))(Seq((_: String) + "!", (_: String) + "?"))) + Assert.assertEquals( + Seq("Hello1!", "Hello2!", "Hello1?", "Hello2?"), + seqApplicative.ap(Seq("Hello1", "Hello2"))( + Seq((_: String) + "!", (_: String) + "?") + ) + ) } @Test def testOptionMap(): Unit = { val optionBind = ComprehensionImplicits.comprehensionMonad[Option] Assert.assertEquals( - Option("hello, applicative"), optionBind.map(Option("hello, ")) { a => + Option("hello, applicative"), + optionBind.map(Option("hello, ")) { a => a + "applicative" - }) + } + ) } @Test @@ -54,19 +67,21 @@ class ComprehensionImplicitsTest { Option("hello, applicative"), optionBind.bind(Option("hello, ")) { a => Option(a + "applicative") - }) + } + ) } @Test def testSeqMap(): Unit = { val seqBind = ComprehensionImplicits.comprehensionMonad[Seq] Assert.assertEquals( - Seq("hello, applicative"), seqBind.map(Seq("hello, ")) { a => + Seq("hello, applicative"), + seqBind.map(Seq("hello, ")) { a => a + "applicative" - }) + } + ) } - @Test def testSeqBind(): Unit = { val seqBind = ComprehensionImplicits.comprehensionMonad[Seq] @@ -74,7 +89,8 @@ class ComprehensionImplicitsTest { Seq("hello, applicative"), seqBind.bind(Seq("hello, ")) { a => Seq(a + "applicative") - }) + } + ) } } diff --git a/each/src/test/scala/com/thoughtworks/each/Issue38.scala b/each/src/test/scala/com/thoughtworks/each/Issue38.scala index 5cdf3a9..5db434d 100644 --- a/each/src/test/scala/com/thoughtworks/each/Issue38.scala +++ b/each/src/test/scala/com/thoughtworks/each/Issue38.scala @@ -6,12 +6,11 @@ import Monadic._ final class Issue38 { def shouldNotWarning = { monadic[Option] { - Option( - { - val mm = 1 - mm - }).each; + Option({ + val mm = 1 + mm + }).each; 1 } } -} \ No newline at end of file +} diff --git a/each/src/test/scala/com/thoughtworks/each/MonadicErrorTest.scala b/each/src/test/scala/com/thoughtworks/each/MonadicErrorTest.scala index 0091ac3..c31d13c 100644 --- a/each/src/test/scala/com/thoughtworks/each/MonadicErrorTest.scala +++ b/each/src/test/scala/com/thoughtworks/each/MonadicErrorTest.scala @@ -40,7 +40,9 @@ class MonadicErrorTest { var count = 0 import scala.language.implicitConversions - implicit def cast[From, To](from: OptionScript[From])(implicit view: From => To): OptionScript[To] = { + implicit def cast[From, To]( + from: OptionScript[From] + )(implicit view: From => To): OptionScript[To] = { Monad[OptionScript].map[From, To](from)(view) } @@ -78,7 +80,9 @@ class MonadicErrorTest { var count = 0 import scala.language.implicitConversions - implicit def cast[From, To](from: OptionScript[From])(implicit view: From => To): OptionScript[To] = { + implicit def cast[From, To]( + from: OptionScript[From] + )(implicit view: From => To): OptionScript[To] = { Monad[OptionScript].map[From, To](from)(view) } @@ -114,9 +118,11 @@ class MonadicErrorTest { private type Script[A] = EitherT[FreeCommand, Throwable, A] - private val randomInt: Script[Int] = EitherT[FreeCommand, Throwable, Int](Free.liftF(RandomInt)) + private val randomInt: Script[Int] = + EitherT[FreeCommand, Throwable, Int](Free.liftF(RandomInt)) - private def count(delta: Int): Script[Int] = EitherT[FreeCommand, Throwable, Int](Free.liftF(Count(delta))) + private def count(delta: Int): Script[Int] = + EitherT[FreeCommand, Throwable, Int](Free.liftF(Count(delta))) private case object MyException extends Exception @@ -156,26 +162,30 @@ class MonadicErrorTest { _ > 100 }, { count(600000).flatMap { _ => - implicitly[MonadThrowable[Script]].raiseError(MyException) flatMap { _: Nothing => + implicitly[MonadThrowable[Script]] + .raiseError(MyException) flatMap { _: Nothing => count(7000000).map { _ => 789 } } } }, { - Monad[Script].ifM(randomInt map { - _ > 100 - }, { - count(1).map { _ => - 123 - } - }, { - count(20).flatMap { _ => - implicitly[MonadThrowable[Script]].raiseError(new IOException) map { x: Nothing => - x: Int + Monad[Script].ifM( + randomInt map { + _ > 100 + }, { + count(1).map { _ => + 123 + } + }, { + count(20).flatMap { _ => + implicitly[MonadThrowable[Script]] + .raiseError(new IOException) map { x: Nothing => + x: Int + } } } - }) + ) } ) } { @@ -238,7 +248,10 @@ class MonadicErrorTest { } } }) - Assert.assertEquals(\/.fromTryCatchNonFatal(noScript(() => throw MyException)), result) + Assert.assertEquals( + \/.fromTryCatchNonFatal(noScript(() => throw MyException)), + result + ) Assert.assertEquals(-\/(MyException), result) Assert.assertEquals(54000, count) } @@ -260,7 +273,10 @@ class MonadicErrorTest { } } }) - Assert.assertEquals(\/.fromTryCatchNonFatal(noScript(() => throw new IOException)), result) + Assert.assertEquals( + \/.fromTryCatchNonFatal(noScript(() => throw new IOException)), + result + ) Assert.assertEquals(\/-(456), result) Assert.assertEquals(54300, count) } diff --git a/each/src/test/scala/com/thoughtworks/each/MonadicTest.scala b/each/src/test/scala/com/thoughtworks/each/MonadicTest.scala index 2ccab8a..4870ae9 100755 --- a/each/src/test/scala/com/thoughtworks/each/MonadicTest.scala +++ b/each/src/test/scala/com/thoughtworks/each/MonadicTest.scala @@ -12,7 +12,7 @@ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/ + */ package com.thoughtworks.each @@ -59,13 +59,19 @@ class MonadicTest { @Test def testPow(): Unit = { val pow = monadic[Seq](math.pow(2.0, (0 to 10).each)) - Assert.assertEquals(Seq(1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0, 512.0, 1024.0), pow) + Assert.assertEquals( + Seq(1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0, 512.0, 1024.0), + pow + ) } @Test def testMultiply(): Unit = { val result = monadic[Seq]((0 to 3).each * (10 to 13).each) - Assert.assertEquals(Seq(0, 0, 0, 0, 10, 11, 12, 13, 20, 22, 24, 26, 30, 33, 36, 39), result) + Assert.assertEquals( + Seq(0, 0, 0, 0, 10, 11, 12, 13, 20, 22, 24, 26, 30, 33, 36, 39), + result + ) } @Test @@ -100,7 +106,6 @@ class MonadicTest { Assert.assertEquals(0, count) } - @Test def testIf(): Unit = { val ifOption = monadic[Option] { @@ -112,7 +117,6 @@ class MonadicTest { Assert.assertEquals(Some(11), ifOption) } - @Test def testReturn(): Unit = { @@ -186,10 +190,13 @@ class MonadicTest { s.each.length } - Assert.assertEquals(Monad[Option].map { - def s = Option(Nil) - s - }(_.length), lengthOption) + Assert.assertEquals( + Monad[Option].map { + def s = Option(Nil) + s + }(_.length), + lengthOption + ) } @Test @@ -242,15 +249,21 @@ class MonadicTest { val list1 = Seq("foo", "bar", "baz") val list2 = Seq("Hello", "World!") - val concatSeq = monadic[Seq](list1.each.substring(0, 2) + " " + list2.each.substring(1, 4)) + val concatSeq = monadic[Seq]( + list1.each.substring(0, 2) + " " + list2.each.substring(1, 4) + ) Assert.assertEquals( for { string1 <- list1 string2 <- list2 } yield (string1.substring(0, 2) + " " + string2.substring(1, 4)), - concatSeq) - Assert.assertEquals(Seq("fo ell", "fo orl", "ba ell", "ba orl", "ba ell", "ba orl"), concatSeq) + concatSeq + ) + Assert.assertEquals( + Seq("fo ell", "fo orl", "ba ell", "ba orl", "ba ell", "ba orl"), + concatSeq + ) } @Test @@ -259,15 +272,21 @@ class MonadicTest { val list1 = Set("foo", "bar", "baz") val list2 = Set("Hello", "World!") - val concatSet = monadic[Set](list1.each.substring(0, 2) + " " + list2.each.substring(1, 4)) + val concatSet = monadic[Set]( + list1.each.substring(0, 2) + " " + list2.each.substring(1, 4) + ) Assert.assertEquals( for { string1 <- list1 string2 <- list2 } yield (string1.substring(0, 2) + " " + string2.substring(1, 4)), - concatSet) - Assert.assertEquals(Set("fo ell", "fo orl", "ba ell", "ba orl", "ba ell", "ba orl"), concatSet) + concatSet + ) + Assert.assertEquals( + Set("fo ell", "fo orl", "ba ell", "ba orl", "ba ell", "ba orl"), + concatSet + ) } @Test @@ -363,21 +382,32 @@ class MonadicTest { Assert.assertEquals(12, io(-1).unsafePerformIO()) val state = { - IndexedStateT.stateTMonadState[Int, IO].ifM( - IndexedStateT.stateTMonadState[Int, IO].get.map(_ == 0), - IndexedStateT.stateTMonadState[Int, IO].put(1), - IndexedStateT.stateTMonadState[Int, IO].put(2) - ).flatMap { _ => - IndexedStateT.stateTMonadState[Int, IO].get - }.flatMap { v => - IndexedStateT.stateTMonadState[Int, IO].put(v + 10) - }.flatMap { _ => - IndexedStateT.stateTMonadState[Int, IO].get - } + IndexedStateT + .stateTMonadState[Int, IO] + .ifM( + IndexedStateT.stateTMonadState[Int, IO].get.map(_ == 0), + IndexedStateT.stateTMonadState[Int, IO].put(1), + IndexedStateT.stateTMonadState[Int, IO].put(2) + ) + .flatMap { _ => + IndexedStateT.stateTMonadState[Int, IO].get + } + .flatMap { v => + IndexedStateT.stateTMonadState[Int, IO].put(v + 10) + } + .flatMap { _ => + IndexedStateT.stateTMonadState[Int, IO].get + } } - Assert.assertEquals(state.eval(0).unsafePerformIO(), io(0).unsafePerformIO()) - Assert.assertEquals(state.eval(-1).unsafePerformIO(), io(-1).unsafePerformIO()) + Assert.assertEquals( + state.eval(0).unsafePerformIO(), + io(0).unsafePerformIO() + ) + Assert.assertEquals( + state.eval(-1).unsafePerformIO(), + io(-1).unsafePerformIO() + ) } @Test @@ -415,7 +445,6 @@ class MonadicTest { Assert.assertEquals(300, count) } - @Test def testDoWhile(): Unit = { def s = Option("123") @@ -474,13 +503,16 @@ class MonadicTest { @Test def testAnnotation(): Unit = { val selector = Seq(1, 2, 3) - Assert.assertEquals(Some(Seq(1, 2, 3)), monadic[Option] { - (selector: @unchecked) match { - case s: Seq[String@unchecked] => { - s + Assert.assertEquals( + Some(Seq(1, 2, 3)), + monadic[Option] { + (selector: @unchecked) match { + case s: Seq[String @unchecked] => { + s + } } } - }) + ) } @Test @@ -491,11 +523,11 @@ class MonadicTest { {someFoo.each} } - Assert.assertEquals(Some( - + Assert.assertEquals( + Some( - ), result) + ), + result + ) } } - - \ No newline at end of file diff --git a/each/src/test/scala/com/thoughtworks/each/TraverseComprehensionTest.scala b/each/src/test/scala/com/thoughtworks/each/TraverseComprehensionTest.scala index 5aacaa2..0a502dc 100644 --- a/each/src/test/scala/com/thoughtworks/each/TraverseComprehensionTest.scala +++ b/each/src/test/scala/com/thoughtworks/each/TraverseComprehensionTest.scala @@ -46,7 +46,6 @@ class TraverseComprehensionTest { Assert.assertEquals(Some(List(4300, 4020)), result) } - @Test def testMapWithAnotherType(): Unit = { val result = monadic[Option] { @@ -65,8 +64,8 @@ class TraverseComprehensionTest { i <- List(300, 20).monadicLoop j <- List(50000, 600000).monadicLoop } yield { - i + j + n.each - }).underlying + i + j + n.each + }).underlying } Assert.assertEquals(Some(List(54300, 604300, 54020, 604020)), result) } diff --git a/sonatypeResolver.sbt b/sonatypeResolver.sbt index e996b76..5c15f71 100644 --- a/sonatypeResolver.sbt +++ b/sonatypeResolver.sbt @@ -1 +1,4 @@ -resolvers in ThisBuild ++= Seq(Opts.resolver.sonatypeSnapshots, Opts.resolver.sonatypeStaging) +resolvers in ThisBuild ++= Seq( + Opts.resolver.sonatypeSnapshots, + Opts.resolver.sonatypeStaging +)