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 - Кочергин Глеб #200

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
20 changes: 18 additions & 2 deletions homeworks/homework_7/src/main/scala/Breakfast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,24 @@ 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 {
startTime <- ZIO.succeed(LocalDateTime.now())
waterFiber <- ZIO.sleep(waterBoilingTime).as("water" -> startTime.plus(waterBoilingTime)).fork
eggsFiber <- ZIO.sleep(eggsFiringTime).as("eggs" -> startTime.plus(eggsFiringTime)).fork
saladFiber <- ZIO.sleep(saladInfoTime.cucumberTime.plus(saladInfoTime.tomatoTime))
.as("saladWithSourCream" -> startTime.plus(saladInfoTime.cucumberTime).plus(saladInfoTime.tomatoTime)).fork
water <- waterFiber.join
tea <- ZIO.sleep(teaBrewingTime).as("tea" -> water._2.plus(teaBrewingTime))
eggs <- eggsFiber.join
salad <- saladFiber.join
} yield Map(
eggs,
water,
salad,
tea
)
}


override def run: ZIO[Any with ZIOAppArgs with Scope, Any, Any] = ZIO.succeed(println("Done"))
Expand Down
39 changes: 36 additions & 3 deletions homeworks/homework_7/src/main/scala/ResuourceTraining.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,42 @@ 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] = {
val acquire = ZIO.attempt(new BufferedReader(new FileReader(filePath)))

def release(reader: BufferedReader) = ZIO.attempt(reader.close()).orDie

def read(reader: BufferedReader) = ZIO.attempt {
val strBuilder = new StringBuilder()
var line: String = null
if ( {
line = reader.readLine();
line != null
}) {
do {
strBuilder.append(line).append(System.lineSeparator())
} while ( {
line = reader.readLine();
line != null
})
}
strBuilder.toString().stripTrailing()
}
ZIO.acquireReleaseWith(acquire)(release)(read)
}

def writeData(filePath: String, data: String): ZIO[Any, Nothing, Unit] = {
val acquire = ZIO.attempt(new BufferedWriter(new FileWriter(filePath)))

def release(writer: BufferedWriter) = ZIO.attempt(writer.close()).orDie

def write(writer: BufferedWriter) = ZIO.attempt {
writer.write(data)
writer.flush()
}.orDie

ZIO.acquireReleaseWith(acquire)(release)(write).orElse(ZIO.unit)
}

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