Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jdegoes committed Jun 12, 2020
1 parent 25c795a commit 598090c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 25 deletions.
11 changes: 6 additions & 5 deletions src/main/scala/net/degoes/zio/04-concurrency.scala
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,14 @@ object StmDiningPhilosophers extends App {
): STM[Nothing, (Fork, Fork)] =
???

/**
* EXERCISE
*
* Using STM, implement the logic of a philosopher to release both forks.
*/
def putForks(left: TRef[Option[Fork]], right: TRef[Option[Fork]])(
tuple: (Fork, Fork)
) = {
val (leftFork, rightFork) = tuple

right.set(Some(rightFork)) *> left.set(Some(leftFork))
}
): STM[Nothing, Unit] = ???

def setupTable(size: Int): ZIO[Any, Nothing, Roundtable] = {
val makeFork = TRef.make[Option[Fork]](Some(Fork))
Expand Down
67 changes: 48 additions & 19 deletions src/main/scala/net/degoes/zio/05-environment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object ProvideEnvironment extends App {
val getServer: ZIO[Config, Nothing, String] =
ZIO.access[Config](_.server)

def useDatabaseConnection: ZIO[DatabaseConnection, Throwable, Int] =
val useDatabaseConnection: ZIO[DatabaseConnection, Throwable, Int] =
ZIO.accessM[DatabaseConnection](_.query("SELECT * FROM USERS"))

/**
Expand Down Expand Up @@ -90,10 +90,10 @@ object CakeEnvironment extends App {
}

val effect =
(for {
for {
file <- Files.read("build.sbt")
_ <- Logging.log(file)
} yield ())
} yield ()

/**
* EXERCISE
Expand All @@ -108,42 +108,70 @@ object CakeEnvironment extends App {
.exitCode
}

object HasMap extends App {
trait Logging
object Logging extends Logging

trait Database
object Database extends Database

trait Cache
object Cache extends Cache

val hasLogging = Has(Logging: Logging)

val hasDatabase = Has(Database: Database)

val hasCache = Has(Cache: Cache)

val allThree = hasLogging ++ hasDatabase ++ hasCache

val logging = allThree.get[Logging]
val database = allThree.get[Database]
val cache = allThree.get[Cache]

def run(args: List[String]) = ???
}

object LayerEnvironment extends App {
import zio.console._
import java.io.IOException
import zio.blocking._

type MyFx = Logging with Files

type Logging = Has[Logging.Service]
object Logging {
type Files = Has[Files.Service]
object Files {
trait Service {
def log(line: String): UIO[Unit]
def read(file: String): IO[IOException, String]
}

/**
* Using `ZLayer.fromFunction`, create a layer that requires `Console`
* and uses the console to provide a logging service.
* EXERCISE
*
* Using `ZLayer.succeed`, create a layer that implements the `Files`
* service.
*/
val live: ZLayer[Console, Nothing, Logging] = ???
val live: ZLayer[Blocking, Nothing, Files] = ???

def log(line: String) = ZIO.accessM[Logging](_.get.log(line))
def read(file: String) = ZIO.accessM[Files](_.get.read(file))
}

type Files = Has[Files.Service]
object Files {
type Logging = Has[Logging.Service]
object Logging {
trait Service {
def read(file: String): IO[IOException, String]
def log(line: String): UIO[Unit]
}

/**
* EXERCISE
*
* Using `ZLayer.succeed`, create a layer that implements the `Files`
* service.
* Using `ZLayer.fromFunction`, create a layer that requires `Console`
* and uses the console to provide a logging service.
*/
val live: ZLayer[Any, Nothing, Files] = ???
val live: ZLayer[Console, Nothing, Logging] = ???

def read(file: String) = ZIO.accessM[Files](_.get.read(file))
def log(line: String) = ZIO.accessM[Logging](_.get.log(line))
}

val effect =
Expand All @@ -152,7 +180,7 @@ object LayerEnvironment extends App {
_ <- Logging.log(file)
} yield ())

def run(args: List[String]) = {
def run(args: List[String]): ZIO[ZEnv, Nothing, ExitCode] = {

/**
* EXERCISE
Expand All @@ -161,7 +189,8 @@ object LayerEnvironment extends App {
* You will have to build a value (the environment) of the required type
* (`Files with Logging`).
*/
val env: ZLayer[Console, Nothing, Files with Logging] = ???
val env: ZLayer[Console, Nothing, Files with Logging] =
???

effect
.provideCustomLayer(env)
Expand Down
16 changes: 15 additions & 1 deletion src/main/scala/net/degoes/zio/08-advanced.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ object Sharding extends App {
object parallel_web_crawler {
import zio.blocking._
import zio.console._
import zio.duration._
import zio.clock._

type Web = Has[Web.Service]
object Web {
Expand Down Expand Up @@ -140,7 +142,19 @@ object parallel_web_crawler {
seeds: Set[URL],
router: URL => Set[URL],
processor: (URL, String) => IO[E, Unit]
): ZIO[Web, Nothing, List[E]] = ???
): ZIO[Web with Clock, Nothing, List[E]] = {
val emptySet = ZIO.succeed(Set.empty[URL])

def loop(seeds: Set[URL], ref: Ref[CrawlState[E]]): ZIO[Web with Clock, Nothing, Unit] =
if (seeds.isEmpty) ZIO.unit
else ???

for {
ref <- Ref.make[CrawlState[E]](CrawlState(seeds, Nil))
_ <- loop(seeds, ref)
state <- ref.get
} yield state.errors
}

/**
* A data structure representing a structured URL, with a smart constructor.
Expand Down

0 comments on commit 598090c

Please sign in to comment.