Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homework_7 - Страхов Ярослав #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions homeworks/homework_7/src/main/scala/Breakfast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,25 @@ object Breakfast extends ZIOAppDefault {
def makeBreakfast(eggsFiringTime: Duration,
waterBoilingTime: Duration,
saladInfoTime: SaladInfoTime,
teaBrewingTime: Duration): ZIO[Any, Throwable, Map[String, LocalDateTime]] = ???
teaBrewingTime: Duration): ZIO[Any, Throwable, Map[String, LocalDateTime]] = for {
eggs <- (ZIO.sleep(eggsFiringTime) *> ZIO.succeed(LocalDateTime.now())).fork
water <- (ZIO.sleep(waterBoilingTime) *> ZIO.succeed(LocalDateTime.now())).fork
salad <- (ZIO.sleep(saladInfoTime.cucumberTime) *>
ZIO.sleep(saladInfoTime.tomatoTime) *>
ZIO.succeed(LocalDateTime.now())).fork
waterTime <- water.join
teaTime <- ZIO.sleep(teaBrewingTime) *> ZIO.succeed(LocalDateTime.now())
eggsTime <- eggs.join
saladTime <- salad.join
} yield Map(
"eggs" -> eggsTime,
"water" -> waterTime,
"saladWithSourCream" -> saladTime,
"tea" -> teaTime
)



override def run: ZIO[Any with ZIOAppArgs with Scope, Any, Any] = ZIO.succeed(println("Done"))

}
}
22 changes: 18 additions & 4 deletions homeworks/homework_7/src/main/scala/ResuourceTraining.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,23 @@ import java.io.{BufferedReader, BufferedWriter, FileReader, FileWriter}

object ResuourceTraining extends ZIOAppDefault {

def readData(filePath: String): IO[Throwable, String] = ???

def writeData(filePath: String, data: String): ZIO[Any, Nothing, Unit] = ???
def readData(filePath: String): IO[Throwable, String] = {
ZIO.acquireReleaseWith(ZIO.attempt(
new BufferedReader(new FileReader(filePath))
))(reader => ZIO.succeed(reader.close()))(
reader => ZIO.succeed(reader.readLine()))
}

def writeData(filePath: String, data: String): ZIO[Any, Nothing, Unit] = {
ZIO.acquireReleaseWith(ZIO.attempt(new BufferedWriter(new FileWriter(filePath))))(
writer => ZIO.attempt(writer.close()).orDie
)(
writer => ZIO.attempt {
writer.write(data)
writer.flush()
}
).orDie
}

override def run: ZIO[Any with ZIOAppArgs with Scope, Any, Any] = ZIO.succeed("Done")
}
}