Last active
May 27, 2023 06:29
-
-
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/52360a27b341a6971d8ea40bb1a25500885b65f5
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 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.3.0" | |
//> 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