Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 25, 2024 10:18
Show Gist options
  • Save dacr/4656df99550ae4e22da4d2a4548ff05e to your computer and use it in GitHub Desktop.
Save dacr/4656df99550ae4e22da4d2a4548ff05e to your computer and use it in GitHub Desktop.
ZIO learning - playing with streams - stream of random longs coming from linux secured rng / published by https://github.com/dacr/code-examples-manager #443264aa-9413-4557-9612-053294fdb42b/854f1c9da902ada29d87b7a9ef8d42a1ecca8e5d
// summary : ZIO learning - playing with streams - stream of random longs coming from linux secured rng
// keywords : scala, zio, learning, streams, @testable
// publish : gist
// authors : zio
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 443264aa-9413-4557-9612-053294fdb42b
// created-on : 2021-10-30T23:09:55+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-streams:2.0.13"
//> using dep "fr.janalyse::zio-worksheet:2.0.13.0"
// ---------------------
import zio.*, zio.stream.*, zio.worksheet.*
import java.nio.file.Paths
val randomNumberStream =
ZStream
.fromPath(Paths.get("/dev/random"))
.grouped(4)
.map(chunk => BigInt(chunk.toArray).toLong)
.filter(_ > 0)
val logic =
for {
randomNumbers <- randomNumberStream.take(500).runCollect // We limit the stream to a finite size...
_ <- ZIO.foreach(randomNumbers)(number => Console.printLine(number))
} yield ()
logic.unsafeRun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment