Skip to content

About

A few fs2+zio examples (streaming file io, moving average, queue example, echo server, REPL)

Topics

Resources

Stars

8 stars

Watchers

1 watching

Forks

Latest commit

 

History

16 Commits

Folders and files

Repository files navigation

🌊 FS2 examples (And ZIO too!)

A few fs2/zio examples

📚 Tutorial

Tutorial

▶️ Running

Run from the repository root, since the examples read testdata/ with relative paths:

sbt "runMain Converter1"              # fs2 version
sbt "runMain zioVersion.Converter1"   # ZIO version

The other examples run the same way: WindowedAverage, QueueExample, REPL, EchoServer, each with a zioVersion. counterpart.

🛠️ Built with Scala 3, fs2 3.13 (Cats Effect 3.7) and ZIO 2.1.

🧪 Tests

sbt test                           # everything
sbt "testOnly zioVersion.*"        # just the ZIO specs
  • fs2 examples: munit-cats-effect suites in src/test/scala, with TestControl for virtual time and a small FakeConsole
  • ZIO examples: zio-test specs in src/test/scala/zioVersion, using TestClock and TestConsole
  • The EchoServer tests use real sockets on ports 5556 (fs2) and 5557 (ZIO), so a server on 5555 can keep running

🌡️ Converter1 (Also a ZIO version)

Converts from Fahrenheit file to stdout in Celsius

  • 📂 Parses multiple input files
  • 🧹 Handles empty lines, comments, non doubles, missing files
  • 🔢 Formats to 2 decimal places
  • 🏷️ Prepends with a header line

📈 WindowedAverage (Also a ZIO version)

  • 🪟 Emits a windowed running average of the last 5 values

📬 QueueExample (Also a ZIO version)

  • 🏭 Creates stream that emits incrementing int every 100ms to a queue
  • 🍽️ Creates stream that reads int from queue and adds int to ref
  • ⏱️ Runs both at the same time, terminating after 1 second, printing out the ref sum

📡 EchoServer (Also a ZIO version)

  • 🔌 Listens for telnet to port 5555, 2 concurrent connections
  • 🔁 Echos commands back to user
  • 👋 "EXIT" will say "Byebye!" and terminate their connection
  • 🛑 "KILLSERVER" will terminate the server
  • ⌨️ Also spins up a REPL, so we can kill server cleanly from console, via SignallingRef (fs2) or Promise (ZIO)
  • 🛡️ A client that disconnects abruptly only ends its own connection, not the server

💬 REPL (Also a ZIO version)

  • 🐣 A simple REPL example
  • 🔁 Echos in the input from stdin
  • 🚪 Exits loop upon "KILLSERVER" or EOF signal

About

A few fs2+zio examples (streaming file io, moving average, queue example, echo server, REPL)

Topics

Resources

Stars

8 stars

Watchers

1 watching

Forks

Contributors

Languages