Skip to content

Commit

Permalink
Add mr Hyde using standard library
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-adam committed Jan 15, 2020
1 parent e491e7c commit 1a2d750
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package pl.writeonly.scala.hyde.common.states.api

import pl.writeonly.scala.hyde.common.url._
import pl.writeonly.scala.hyde.common.url.urls._
import pl.writeonly.scala.hyde.sourcepage.std._

abstract class EitherAPIState(data: UrlsWithThrowableList)(implicit d: Domain) extends AbstractAPIState(data) {
override protected type HP = SourcePageEither

protected def nextData(set: SourcePageEitherSet): UrlsWithThrowableList = {

val partitioned = set.partition(_.isRight)

val newWrappedUrls: WrappedUrlSet = partitioned._1
.flatMap(EitherAPIState.sourcePageEitherToWrappedUrlSet)

val newThrowableList: ThrowableList = partitioned._2.toList
.flatMap(_.left.toOption.toList)

val newUrls = NewUrls(newWrappedUrls)

data.next(newUrls, newThrowableList)
}
}

object EitherAPIState {
val sourcePageEitherToWrappedUrlSet: SourcePageEither => WrappedUrlSet =
_.right.map(_.getWrappedUrlSet).right.toOption.toSet.flatten
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package pl.writeonly.scala.hyde.common.states

import scala.concurrent.Future

package object api {
type ParallelStateFuture = Future[EitherAPIState]
}
29 changes: 29 additions & 0 deletions hyde/jvm/src/main/scala/pl/writeonly/scala/hyde/impl/Hyde.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import scala.util.Try
import pl.writeonly.scala.hyde.impl.oo.oo1.StandardExceptionHyde
import pl.writeonly.scala.hyde.impl.oo.oo2.WrappingExceptionHyde
import pl.writeonly.scala.hyde.impl.oo.oo3.TryHyde
import pl.writeonly.scala.hyde.impl.std.std1.EitherHyde
import pl.writeonly.scala.hyde.impl.std.std2.EitherBeginHyde
import pl.writeonly.scala.hyde.impl.std.std3.EitherEndHyde
import pl.writeonly.scala.hyde.impl.std.std4.FutureHyde
import slogging._

object Hyde {
Expand All @@ -18,6 +22,8 @@ object Hyde {

oo

std()

println("end of program")
}

Expand All @@ -36,6 +42,29 @@ object Hyde {
println(tryHyde.throwableList.size.toString)
}

def std(): Unit = {

val eitherState = time("EitherHyde") {
EitherHyde.apply()
}
println(eitherState.throwableList.size.toString)

val eitherBeginState = time("EitherBeginHyde") {
EitherBeginHyde.apply()
}
println(eitherBeginState.throwableList.size.toString)

val eitherEndState = time("EitherEndHyde") {
EitherEndHyde.apply()
}
println(eitherEndState.throwableList.size.toString)

val futureState = time("FutureHyde") {
FutureHyde.apply()
}
println(futureState.throwableList.size.toString)
}

def time[R](name: String)(block: => R): R = {
println(name)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pl.writeonly.scala.hyde.impl.std.std1

import pl.writeonly.scala.hyde.common.states.notexception.AbstractNextState

object EitherHyde {

private val domain = "https://www.writeonly.pl"

def main(args: Array[String]): Unit = apply().showResult()

def apply(): AbstractNextState = EitherState(domain)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pl.writeonly.scala.hyde.impl.std.std1

import pl.writeonly.scala.hyde.common.states.api.EitherAPIState
import pl.writeonly.scala.hyde.common.states.notexception._
import pl.writeonly.scala.hyde.common.url.Domain
import pl.writeonly.scala.hyde.common.url.urls.UrlsWithThrowableList
import pl.writeonly.scala.hyde.sourcepage.std.SourcePageEitherFromInternalUrl

class EitherState(data: UrlsWithThrowableList)(implicit d: Domain) extends EitherAPIState(data) with AbstractFunctionState {

override protected def nextState(data: UrlsWithThrowableList): AbstractNextState = new EitherState(data)

override protected def impureFunction: HPFromInternalUrl = SourcePageEitherFromInternalUrl.apply
}

object EitherState extends AbstractNextStateObject {

override protected def fromDomain(implicit d: Domain): AbstractNextState = new EitherState(UrlsWithThrowableList.fromDomain)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pl.writeonly.scala.hyde.impl.std.std2

import scala.concurrent.ExecutionContext.Implicits.global

import pl.writeonly.scala.hyde.common.states.notexception.AbstractNextState

object EitherBeginHyde {

private val domain = "https://www.writeonly.pl"

def main(args: Array[String]): Unit = apply().showResult()

def apply(): AbstractNextState = EitherBeginState(domain)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package pl.writeonly.scala.hyde.impl.std.std2

import scala.concurrent._
import scala.concurrent.duration._

import pl.writeonly.scala.hyde.common.states.api.EitherAPIState
import pl.writeonly.scala.hyde.common.states.notexception._
import pl.writeonly.scala.hyde.common.url.Domain
import pl.writeonly.scala.hyde.common.url.urls.UrlsWithThrowableList
import pl.writeonly.scala.hyde.sourcepage.std._
import scalaz.Scalaz._

class EitherBeginState(data: UrlsWithThrowableList)(implicit d: Domain, ec: ExecutionContext) extends EitherAPIState(data) with AbstractNewSetState {

override protected def nextState(data: UrlsWithThrowableList): AbstractNextState = new EitherBeginState(data)

override protected def newSet: SourcePageEitherSet = {

val set: Set[Future[SourcePageEither]] = nextUrls
.map(SourcePageFutureFromInternalUrl.apply)

val future: Future[SourcePageEitherSet] = Future.sequence(set)

Await.result(future, 1.minute)
}
}

object EitherBeginState {

def apply(domain: String)(implicit ec: ExecutionContext): AbstractNextState = fromDomain(new Domain(domain)) |> AbstractNextState.run

private def fromDomain(domain: Domain)(implicit ec: ExecutionContext): AbstractNextState = fromDomainAllImplicit(domain, ec)

private def fromDomainAllImplicit(implicit d: Domain, ec: ExecutionContext): AbstractNextState =
new EitherBeginState(UrlsWithThrowableList.fromDomain)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pl.writeonly.scala.hyde.impl.std.std3

import scala.concurrent.ExecutionContext.Implicits.global

import pl.writeonly.scala.hyde.common.states.notexception.AbstractNextState

object EitherEndHyde {

private val domain = "https://www.writeonly.pl"

def main(args: Array[String]): Unit = apply().showResult()

def apply(): AbstractNextState = EitherEndState(domain)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package pl.writeonly.scala.hyde.impl.std.std3

import scala.concurrent._
import scala.concurrent.duration._

import pl.writeonly.scala.hyde.common.states.api.EitherAPIState
import pl.writeonly.scala.hyde.common.states.notexception._
import pl.writeonly.scala.hyde.common.url.Domain
import pl.writeonly.scala.hyde.common.url.urls.UrlsWithThrowableList
import pl.writeonly.scala.hyde.sourcepage.std._
import scalaz.Scalaz._

class EitherEndState(data: UrlsWithThrowableList)(implicit d: Domain, ec: ExecutionContext) extends EitherAPIState(data) with AbstractNextState {

override protected def nextState(data: UrlsWithThrowableList): AbstractNextState = new EitherEndState(data)

override def next: AbstractNextState = {

val set: Set[Future[SourcePageEither]] = nextUrls
.map(SourcePageFutureFromInternalUrl.apply)

val monad: Future[NextState] = Future
.sequence(set)
.map(newState)

Await.result(monad, 1.minute)
}
}

object EitherEndState {

def apply(domain: String)(implicit ec: ExecutionContext): AbstractNextState = fromDomain(new Domain(domain)) |> AbstractNextState.run

private def fromDomain(domain: Domain)(implicit ec: ExecutionContext): AbstractNextState = fromDomainAllImplicit(domain, ec)

private def fromDomainAllImplicit(implicit d: Domain, ec: ExecutionContext): AbstractNextState =
new EitherEndState(UrlsWithThrowableList.fromDomain)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pl.writeonly.scala.hyde.impl.std.std4

import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

import pl.writeonly.scala.hyde.common.states.api._

object FutureHyde {

private val domain = "https://www.writeonly.pl"

def main(args: Array[String]): Unit = apply().showResult()

def apply(): EitherAPIState = Await.result(applyFuture(), 1.minute)

def applyFuture(): ParallelStateFuture = FutureState(domain)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package pl.writeonly.scala.hyde.impl.std.std4

import scala.concurrent._

import pl.writeonly.scala.hyde.common.states.api._
import pl.writeonly.scala.hyde.common.url.Domain
import pl.writeonly.scala.hyde.common.url.urls.UrlsWithThrowableList
import pl.writeonly.scala.hyde.sourcepage.std._
import scalaz.Scalaz._

class FutureState(data: UrlsWithThrowableList)(implicit d: Domain, ec: ExecutionContext) extends EitherAPIState(data) {

override protected def nextState(data: UrlsWithThrowableList): NextState = new FutureState(data)

override type NextState = FutureState

def nextMonad: Future[FutureState] = {

val set: Set[Future[SourcePageEither]] = nextUrls
.map(SourcePageFutureFromInternalUrl.apply)

val monad: Future[SourcePageEitherSet] = Future
.sequence(set)

monad.map(newState)
}
}

object FutureState {

def apply(domain: String)(implicit ec: ExecutionContext): ParallelStateFuture = fromDomain(new Domain(domain)) |> run

private def fromDomain(domain: Domain)(implicit ec: ExecutionContext): FutureState = fromDomainAllImplicit(domain, ec)

private def fromDomainAllImplicit(implicit d: Domain, ec: ExecutionContext): FutureState =
new FutureState(UrlsWithThrowableList.fromDomain)

private def run(state: FutureState)(implicit executor: ExecutionContext): Future[EitherAPIState] =
if (state.isEmptyNextInternalUrls) Future.successful(state) else state.nextMonad.flatMap(run)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pl.writeonly.scala.hyde.sourcepage.std

import scala.util.control.Exception.nonFatalCatch

import pl.writeonly.scala.hyde.common.url.typed.InternalUrl
import pl.writeonly.scala.hyde.sourcepage.InternalUrlTo
import pl.writeonly.scala.hyde.sourcepage.oo.SourcePageFromInternalUrl
import scalaz.Scalaz._

object SourcePageEitherFromInternalUrl extends InternalUrlTo[SourcePageEither] {

override def apply(internalUrl: InternalUrl): SourcePageEither = applyWithThrowable(internalUrl).left.map(internalUrl.toException)

private def applyWithThrowable(internalUrl: InternalUrl) = nonFatalCatch either { internalUrl |> SourcePageFromInternalUrl.apply }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pl.writeonly.scala.hyde.sourcepage.std

import scala.concurrent._

import pl.writeonly.scala.hyde.common.url.typed.InternalUrl
import scalaz.Scalaz._

object SourcePageFutureFromInternalUrl {

def apply(internalUrl: InternalUrl)(implicit ec: ExecutionContext): SourcePageFuture =
Future { internalUrl |> SourcePageEitherFromInternalUrl.apply }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pl.writeonly.scala.hyde.sourcepage

import scala.concurrent.Future

import pl.writeonly.scala.hyde.common.url.exception.UrlException

package object std {
type SourcePageEither = Either[UrlException, SourcePage]

type SourcePageEitherSet = Set[SourcePageEither]

type SourcePageFuture = Future[SourcePageEither]

}

0 comments on commit 1a2d750

Please sign in to comment.