Skip to content

Commit

Permalink
refactor: trying to compile with pipelining
Browse files Browse the repository at this point in the history
  • Loading branch information
etorreborre committed Jun 1, 2024
1 parent 38567f8 commit d3f6d42
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 57 deletions.
7 changes: 5 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ lazy val specs2Settings = Seq(
),
specs2ShellPrompt,
ThisBuild / crossScalaVersions := Seq(Scala3),
ThisBuild / scalaVersion := Scala3
ThisBuild / scalaVersion := Scala3,
ThisBuild / usePipelining := true
)

lazy val rootSettings =
Expand Down Expand Up @@ -311,7 +312,9 @@ lazy val compilationOptions = Seq(
"-Xcheck-macros",
"-deprecation:true",
"-unchecked",
"-feature"
"-feature",
"-explain-cyclic",
"-explain",
)

lazy val testSettings = Seq(
Expand Down
7 changes: 3 additions & 4 deletions common/jvm/src/main/scala/org/specs2/reflect/Classes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ package reflect
import scala.reflect.{ClassTag, NameTransformer}
import scala.util.control.NonFatal
import java.lang.reflect.Constructor
import control.*
import fp.syntax.*
import org.specs2.control.*
import ClassName.*

/** This trait provides functions to instantiate classes
Expand All @@ -25,7 +24,7 @@ trait Classes extends ClassOperations:
def createInstance[T <: AnyRef](className: String, loader: ClassLoader, defaultInstances: =>List[AnyRef] = Nil)(using
m: ClassTag[T]
): Operation[T] =
loadClass(className, loader) >>= { (klass: Class[T]) =>
loadClass(className, loader).flatMap { (klass: Class[T]) =>
createInstanceFromClass(klass, loader, defaultInstances)
}

Expand All @@ -52,7 +51,7 @@ trait Classes extends ClassOperations:
loader: ClassLoader,
defaultInstances: =>List[AnyRef] = Nil
)(using m: ClassTag[T]): Operation[Throwable Either T] =
loadClassEither(className, loader) >>= { (tc: Throwable Either Class[T]) =>
loadClassEither(className, loader).flatMap { (tc: Throwable Either Class[T]) =>
tc match
case Left(t) =>
Operation.ok(Left(t))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.specs2
package collection

import org.specs2.fp.*
import org.specs2.fp.{Foldable, Monoid}
import scala.collection.mutable.ListBuffer

/** This trait provides additional methods on Seqs and nested Seqs
Expand Down
17 changes: 7 additions & 10 deletions common/shared/src/main/scala/org/specs2/control/Action.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.specs2
package control

import fp.*, syntax.*
import org.specs2.fp.{Monoid, FunctorSyntax, Id}
import org.specs2.fp.{Monad}
import org.specs2.fp.{Applicative}
import org.specs2.fp.syntax.void
import concurrent.{ExecutionEnv, *}
import scala.concurrent.*
import scala.concurrent.duration.*
Expand Down Expand Up @@ -145,13 +148,7 @@ object Action:
fa.flatMap(f)

override def ap[A, B](fa: =>Action[A])(ff: =>Action[A => B]): Action[B] =
Action(
runNow = { ee =>
given ExecutionContext = ee.executionContext
ff.runNow(ee).zip(fa.runNow(ee)).map { case (f, a) => f(a) }
},
last = fa.last ++ ff.last
)
ActionApplicative.ap(fa)(ff)

override def toString: String =
"Monad[Action]"
Expand All @@ -172,8 +169,8 @@ object Action:
override def toString: String =
"Applicative[Action]"

given NaturalTransformation[Id, Action] =
NaturalTransformation.naturalId[Action]
given org.specs2.fp.NaturalTransformation[Id, Action] =
org.specs2.fp.NaturalTransformation.naturalId[Action]

given FinalizedAction: Safe[Action] with
def finalizeWith[A](fa: Action[A], f: Finalizer): Action[A] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package control
import io.FilePath
import scala.sys.process.ProcessLogger
import Operation.*
import fp.syntax.*
import org.specs2.fp.FunctorSyntax.void

/** Execute external commands
*/
Expand Down
6 changes: 3 additions & 3 deletions common/shared/src/main/scala/org/specs2/control/Logger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.specs2
package control

import io.*
import fp.*, syntax.*

/** specs2 logger support
*/
Expand All @@ -13,15 +12,16 @@ trait Logger:

// derived operations
def warnAndFail[A](warnMessage: String, failureMessage: String, doIt: Boolean = true): Operation[A] =
warn(warnMessage, doIt) >> Operation.fail[A](failureMessage)
warn(warnMessage, doIt).flatMap { _ => Operation.fail[A](failureMessage) }

/** Logger implementation directing messages to the console
*/
case class ConsoleLogger() extends Logger:

def exception(t: Throwable, verbose: Boolean = false): Operation[Unit] =
Operation.delayed(println("[ERROR] " + t.getMessage)) >>
Operation.delayed(println("[ERROR] " + t.getMessage)).flatMap { _ =>
(if verbose then Operation.delayed(t.printStackTrace) else Operation.unit)
}

def warn(message: String, doIt: Boolean = true): Operation[Unit] =
if doIt then Operation.delayed((println("[WARN] " + message)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.specs2
package control

import fp.*, syntax.*
import org.specs2.fp.{Monoid, Monad}
import execute.*
import scala.annotation.tailrec
import scala.util.control.NonFatal
Expand Down Expand Up @@ -152,7 +152,7 @@ object Operation:
override def toString: String =
"Monad[Operation]"

given OperationApplicative: Applicative[Operation[*]] with
given OperationApplicative: org.specs2.fp.Applicative[Operation[*]] with
def point[A](a: =>A): Operation[A] =
Operation(() => Right(a))

Expand All @@ -162,7 +162,7 @@ object Operation:
override def toString: String =
"Applicative[Operation]"

given operationToAction: NaturalTransformation[Operation, Action] with
given operationToAction: org.specs2.fp.NaturalTransformation[Operation, Action] with
def apply[A](operation: Operation[A]): Action[A] =
operation.toAction

Expand Down
24 changes: 12 additions & 12 deletions common/shared/src/main/scala/org/specs2/control/origami/Fold.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package org.specs2
package control
package origami

import org.specs2.fp.*
import org.specs2.fp.syntax.*
import org.specs2.fp.{Foldable, Monoid, Monad, ~>, Applicative, Id, FunctorSyntax}
import org.specs2.fp.syntax.{map, toList, MonadOps, as}

/** A Fold is a "left fold" over a data structure with:
* - a 'start' value
Expand Down Expand Up @@ -53,7 +53,7 @@ trait Fold[M[_], A, B]:

def start = self.start
def fold = self.fold
def end(s: S) = self.end(s).flatMap(f.run1)
def end(s: S) = monad.bind(self.end(s))(f.run1)

/** parallel composition */
def ***[V, W](f: Fold[M, V, W]): Fold[M, (A, V), (B, W)] =
Expand Down Expand Up @@ -155,7 +155,7 @@ trait Fold[M[_], A, B]:
val monad: Monad[M] = self.monad

def start = monad.tuple2(self.start, sink.start)
def fold = (s: S, a: A) => self.fold(s._1, a).flatMap(next => sink.fold(s._2, (a, next)).map((next, _)))
def fold = (s: S, a: A) => monad.bind(self.fold(s._1, a))(next => sink.fold(s._2, (a, next)).map((next, _)))
def end(s: S) = monad.tuple2(self.end(s._1), sink.end(s._2)).map(_._1)

/** alias for observeWithNextState */
Expand All @@ -169,7 +169,7 @@ trait Fold[M[_], A, B]:
val monad: Monad[M] = self.monad

def start = monad.tuple2(self.start, sink.start)
def fold = (s: S, a: A) => self.fold(s._1, a).flatMap(next => sink.fold(s._2, next).map((next, _)))
def fold = (s: S, a: A) => monad.bind(self.fold(s._1, a))(next => sink.fold(s._2, next).map((next, _)))
def end(s: S) = monad.tuple2(self.end(s._1), sink.end(s._2)).map(_._1)

/** alias for observeNextState */
Expand All @@ -184,7 +184,7 @@ trait Fold[M[_], A, B]:
/** run over one element
*/
def run1(a: A): M[B] =
start.flatMap(s => fold(s, a).flatMap(end))
monad.bind(start)(s => fold(s, a).flatMap(end))

/** pipe the output of this fold into another fold */
def compose[C](f2: Fold[M, B, C]): Fold[M, A, C] =
Expand All @@ -195,7 +195,7 @@ trait Fold[M[_], A, B]:
def start = monad.tuple2(self.start, f2.start)

def fold = (s, a) =>
self.fold(s._1, a).flatMap(self.end).flatMap((u: B) => monad.tuple2(self.fold(s._1, a), f2.fold(s._2, u)))
monad.bind(self.fold(s._1, a))(self.end).flatMap((u: B) => monad.tuple2(self.fold(s._1, a), f2.fold(s._2, u)))

def end(s: S) =
f2.end(s._2)
Expand Down Expand Up @@ -225,7 +225,7 @@ trait Fold[M[_], A, B]:
type S = self.S
val monad: Monad[M] = self.monad

def start = action >> self.start
def start = monad.bind(action)(_ => self.start)
def fold = (s, a) => self.fold(s, a)
def end(s: S) = self.end(s)

Expand All @@ -236,7 +236,7 @@ trait Fold[M[_], A, B]:

def start = self.start
def fold = (s, a) => self.fold(s, a)
def end(s: S) = self.end(s).flatMap(b => action.as(b))
def end(s: S) = monad.bind(self.end(s))(b => action.as(b))

def into[M1[_]](using nat: M ~> M1, m: Monad[M1]) =
monadic[M1](using nat, m)
Expand Down Expand Up @@ -264,9 +264,9 @@ object Fold:
lazy val s2_ = s2
type S = (s1.S, s2_.S)

def start = s1.start.flatMap(s1s => s2_.start.map(s2s => (s1s, s2s)))
def fold = (s: S, a: A) => s1.fold(s._1, a).flatMap(s11 => s2_.fold(s._2, a).map(s22 => (s11, s22)))
def end(s: S) = s1.end(s._1) >> s2_.end(s._2)
def start = monad.bind(s1.start)(s1s => s2_.start.map(s2s => (s1s, s2s)))
def fold = (s: S, a: A) => monad.bind(s1.fold(s._1, a))(s11 => s2_.fold(s._2, a).map(s22 => (s11, s22)))
def end(s: S) = monad.bind(s1.end(s._1))(_ => s2_.end(s._2))
}

/** Applicative instance
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.specs2
package control

import fp.*
import org.specs2.fp.Id

package object origami:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.specs2.control
package producer

import org.specs2.*
import fp.*, syntax.*
import org.specs2.control.Action.{given}
import org.specs2.fp.{Foldable, Monad, Monoid, FunctorSyntax}
import org.specs2.fp.syntax.{MonadOps, MonadFlattenOps, suml, as}
import origami.*
import Producer.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.specs2
package data

import main.Arguments
import org.specs2.fp.*
import org.specs2.fp.Monoid
import org.specs2.collection.canEqualAny

/** A tag with names.
Expand Down
4 changes: 2 additions & 2 deletions common/shared/src/main/scala/org/specs2/data/Trees.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.specs2
package data

import fp.*
import Tree.*
import org.specs2.fp.{Tree, TreeLoc}
import org.specs2.fp.Tree.*
import scala.annotation.*
import collection.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package execute

import ResultExecution.*
import Results.{given}
import fp.*, syntax.{given, *}
import org.specs2.fp.Monoid
import org.specs2.fp.syntax.foldMap

/** Typeclass trait for anything that can be transformed to a Result
*/
Expand Down
8 changes: 4 additions & 4 deletions common/shared/src/main/scala/org/specs2/execute/Result.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ package execute
import control.Throwablex
import control.Throwablex.*
import main.Arguments
import org.specs2.fp.*
import org.specs2.fp.syntax.*
import org.specs2.fp.Monoid
import org.specs2.fp.syntax.{suml, foldMap}
import text.Message.concat
import text.Sentences.*
import text.NotNullStrings.*
import text.Sentences.negateSentence
import text.NotNullStrings.notNull

/** The result of an execution, either:
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package execute
import Result.ResultFailureMonoid
import text.Quote.*
import text.Plural.*
import org.specs2.fp.syntax.*
import control.*

/** This trait adds some implicits to easily fold sequences of results
*/
Expand Down Expand Up @@ -39,7 +37,7 @@ trait ResultImplicits extends ResultLogicalCombinators:
*/
def foreach(seq: Traversable[T]): Result =
if seq.isEmpty then StandardResults.success
else seq.drop(1).foldLeft(t.applied(seq.head)) { (res, cur) => AsResult(res) |+| t.applied(cur) }
else seq.drop(1).foldLeft(t.applied(seq.head)) { (res, cur) => ResultFailureMonoid.append(AsResult(res), t.applied(cur)) }

/** @return
* success if at least one result is a success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io

import java.io.{BufferedInputStream, File, FileInputStream}

import fp.*, syntax.*
import control.*
import producer.*
import text.MD5
Expand Down
7 changes: 3 additions & 4 deletions common/shared/src/main/scala/org/specs2/main/Arguments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package org.specs2
package main

import main.{SystemProperties as sysProperties}
import org.specs2.fp.*
import control.*
import text.*
import scala.Predef.*
import org.specs2.fp.Monoid
import control.StackTraceFilter
import text.Colors
import scala.concurrent.duration.FiniteDuration

/** This class holds all the options that are relevant for specs2 execution and reporting.
Expand Down
2 changes: 1 addition & 1 deletion common/shared/src/main/scala/org/specs2/main/Extract.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.specs2.text.FromString
import reflect.Classes

import scala.reflect.ClassTag
import org.specs2.fp.Memo.*
import org.specs2.fp.Memo.immutableHashMapMemo

/** Extraction functions for command line parameters
*/
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ addSbtPlugin("io.chrisdavenport" % "sbt-mima-version-check" % "0.1.2")
resolvers += Resolver.url("sonatype", new URL("https://oss.sonatype.org/content/repositories/releases"))(
Resolver.ivyStylePatterns
)
resolvers += Resolver.sonatypeRepo("snapshots")
resolvers ++= Resolver.sonatypeOssRepos("snapshots")

0 comments on commit d3f6d42

Please sign in to comment.