Last active
May 25, 2024 10:19
-
-
Save dacr/f5592a2692817d4c55708cd67d633f26 to your computer and use it in GitHub Desktop.
ZIO live session - simple live demo / published by https://github.com/dacr/code-examples-manager #e9ac3978-7758-4828-8497-2144664a9323/ee665e1a44de6c4d89bc823f2757c4982779d024
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : ZIO live session - Hello world | |
// keywords : scala, zio, live, demo, pure-functional, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 7d6e7858-f20b-43da-a914-50027a61af79 | |
// created-on : 2021-09-26T16:27:18+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "dev.zio::zio:2.0.13" | |
//> using dep "fr.janalyse::zio-worksheet:2.0.13.0" | |
// --------------------- | |
import zio.*, zio.worksheet.* | |
println("Hello world !") | |
// --------------------------- | |
val logic1 = ZIO.attempt(println("Hello world!")) | |
logic1.unsafeRun | |
// --------------------------- | |
val logic2 = Console.printLine("Hello world!") | |
logic2.unsafeRun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : ZIO live session - Make execution resilient | |
// keywords : scala, zio, live, demo, pure-functional, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : b437a3c2-257b-4346-89fd-f6632c5d2d60 | |
// created-on : 2021-09-26T16:27:18+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "dev.zio::zio:2.0.13" | |
//> using dep "fr.janalyse::zio-worksheet:2.0.13.0" | |
// --------------------- | |
import zio.*, zio.worksheet.*, scala.util.Random.nextInt, scala.util.Try | |
import scala.Console.{GREEN,RED,RESET} | |
// 42 / nextInt(2) | |
Try(42 / nextInt(2)) | |
val logic = ZIO.attempt(42 / nextInt(2)) | |
logic | |
.tapError(result => Console.printLine(s"1.${RED} FAILED$RESET")) | |
.tap(result => Console.printLine(s"1.${GREEN} SUCCESS$RESET")) | |
.ignore | |
.unsafeRun // ONLY FOR WORKSHEET PURPOSES | |
logic | |
.retry(Schedule.forever) | |
.tapError(result => Console.printLine(s"2.${GREEN} FAILED$RESET")) | |
.tap(result => Console.printLine(s"1.${GREEN} SUCCESS$RESET")) | |
.unsafeRun // ONLY FOR WORKSHEET PURPOSES |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : ZIO live session - Dealing with long running operation | |
// keywords : scala, zio, live, demo, pure-functional, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 0079acb0-25b3-4e0b-860e-b5d7d75d1699 | |
// created-on : 2021-09-26T16:27:18+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "dev.zio::zio:2.0.13" | |
//> using dep "fr.janalyse::zio-worksheet:2.0.13.0" | |
// --------------------- | |
import zio.*, zio.worksheet.*, scala.io.Source | |
Source.fromURL("https://www.mapland.fr/ip").mkString | |
val logic = ZIO.attemptBlocking(Source.fromURL("https://www.mapland.fr/ip").mkString) | |
logic | |
.tap(result => Console.printLine(result)) | |
.unsafeRun | |
logic | |
.timeout(Duration.fromMillis(100)) | |
.tap(resultOption => Console.printLine(resultOption.getOrElse("Nothing"))) | |
.unsafeRun | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : ZIO live session - Writing a simple application - legacy versus effect-based | |
// keywords : scala, zio, live, demo, pure-functional, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : b354e541-49ea-4c6c-aa60-a6497b0ac72f | |
// created-on : 2021-09-26T16:27:18+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "org.slf4j:slf4j-simple:2.0.7" | |
//> using dep "dev.zio::zio:2.0.13" | |
//> using dep "dev.zio::zio-nio:2.0.1" | |
//> using dep "fr.janalyse::zio-worksheet:2.0.13.0" | |
// --------------------- | |
// ========================================================================================= | |
// classic legacy code | |
{ | |
import org.slf4j.LoggerFactory | |
import scala.io.Source | |
def app() = | |
val logger = LoggerFactory.getLogger("default") | |
logger.info("Application starts") | |
val fileContent = Source.fromFile("live-04.sc").getLines() | |
val count = fileContent.size | |
logger.info("line count is {}", count) | |
logger.info("Application ends") | |
println(count) | |
println(app()) | |
} | |
// ========================================================================================= | |
// effect based programming | |
{ | |
import zio.*, zio.worksheet.* | |
import zio.nio.file.* | |
val app = for { | |
_ <- ZIO.log("Application starts") | |
lines <- Files.readAllLines(Path("live-04.sc")) | |
count = lines.size | |
_ <- ZIO.log(s"line count is $count") | |
_ <- ZIO.log("Application ends") | |
_ <- Console.printLine(count) | |
} yield () | |
app.unsafeRun | |
} | |
// - both are readable in the same way ! | |
// - but the second one is quite safer, the type signature tells us that it can fail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : ZIO live session - Writing a more complex application | |
// keywords : scala, zio, live, demo, pure-functional, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : fb874ad4-4adc-489a-95ba-5c16198a8782 | |
// created-on : 2021-09-26T16:27:18+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "dev.zio::zio:2.0.13" | |
//> using dep "com.softwaremill.sttp.client3::async-http-client-backend-zio:3.8.15" | |
//> using dep "com.softwaremill.sttp.client3::zio-json:3.8.15" | |
//> using dep "fr.janalyse::zio-worksheet:2.0.13.0" | |
//> using dep "org.slf4j:slf4j-simple:2.0.7" | |
// --------------------- | |
import zio.* | |
import zio.json.* | |
import zio.worksheet.* | |
import sttp.client3.ziojson.* | |
import sttp.client3.* | |
import sttp.client3.asynchttpclient.zio.* | |
case class ClientInfo(clientIP: String, userAgent: String) | |
object ClientInfo: | |
implicit val codec: JsonCodec[ClientInfo] = DeriveJsonCodec.gen | |
// ----------------------------------------------------------------------------------------- | |
val app = | |
for | |
_ <- ZIO.log("Application starts") | |
backend <- ZIO.service[SttpBackend[Task, Any]] | |
response <- backend.send(basicRequest.get(uri"https://mapland.fr/clientInfo").response(asJson[ClientInfo])) | |
clientInfo <- ZIO.from(response.body) | |
_ <- ZIO.log(s"my ip is ${clientInfo.clientIP}") | |
_ <- ZIO.log("Application ends") | |
yield clientInfo | |
app.provideLayer(AsyncHttpClientZioBackend.layer()).unsafeRun | |
// ----------------------------------------------------------------------------------------- | |
val stub = | |
AsyncHttpClientZioBackend.stub | |
.whenRequestMatches(_.uri.toString() == "https://mapland.fr/clientInfo") | |
.thenRespond("""{"clientIP":"127.0.0.1","userAgent":"curl/42"}""") | |
app.provideLayer(ZLayer.succeed(stub)).unsafeRun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
## summary : ZIO live session - simple live demo | |
## keywords : bash, zio, live, demo, pure-functional | |
## publish : gist | |
## authors : David Crosson | |
## license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
## id : e9ac3978-7758-4828-8497-2144664a9323 | |
## created-on : 2022-06-25T19:05:22+02:00 | |
## managed-by : https://github.com/dacr/code-examples-manager | |
## attachments : live-01.sc, live-02.sc, live-03.sc, live-04.sc, live-05.sc | |
scala-cli repl \ | |
--dep "fr.janalyse::zio-worksheet:2.0.13.0" \ | |
--dep "dev.zio::zio:2.0.13" \ | |
--dep "dev.zio::zio-streams:2.0.13" \ | |
--dep "dev.zio::zio-concurrent:2.0.13" \ | |
--dep "dev.zio::zio-test:2.0.13" \ | |
--dep "dev.zio::zio-test-scalacheck:2.0.13" \ | |
--dep "dev.zio::zio-nio:2.0.1" \ | |
--dep "dev.zio::zio-json:0.5.0" \ | |
--dep "com.softwaremill.sttp.client3::async-http-client-backend-zio:3.8.5" \ | |
--dep "com.softwaremill.sttp.client3::zio-json:3.8.5" \ | |
--dep "com.softwaremill.sttp.tapir::tapir-zio:1.3.0" \ | |
--dep "com.softwaremill.sttp.tapir::tapir-zio-http-server:1.3.0" \ | |
--dep "com.softwaremill.sttp.tapir::tapir-json-zio:1.3.0" \ | |
--dep "com.softwaremill.sttp.tapir::tapir-redoc-bundle:1.3.0" \ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment