Welcome to the Guessing Game project written in Scala 3, designed as a practical demonstration for presentations on Free Monads. This project serves as a learning tool to showcase the creation of a Free Monad, the DSL, and interpreting it with different interpreters. It was originally used as a live coding example during a presentation to illustrate these concepts.
The project is divided into the following main components:
- DSL: Defines the domain-specific language for the guessing game.
- Free Monad: Implements the Free Monad pattern.
- Interpreters: Provides different interpreters for the DSL, including a console interpreter and a WebSocket interpreter.
- Http4s Server: Sets up an HTTP server using Http4s to handle WebSocket connections.
- Cats: A library which provides abstractions for functional programming in Scala.
- Http4s: A library for building type-safe, functional HTTP services.
- Fs2: A library for functional streams in Scala.
You can use the provided flake.nix
file to create a development environment with Nix and
Flake. To do this, run:
nix develop
To run the console version of the guessing game, use:
sbt "runMain com.lvitaly.fm.cats.consoleGame"
After starting the console version, you can interact with the game using the console. In the console, you will be prompted to enter your guesses. The game will provide feedback on whether your guess is correct or not. In this version, the game is available only only for a single player.
To start the WebSocket based version of the guessing game, use:
sbt "runMain com.lvitaly.fm.cats.WebSocketGame"
After starting the server, you can connect to it using a WebSocket client. For example, you can use the websocat:
websocat -E ws://localhost:8080/ws
In the WebSocket version, multiple players can connect to the server and play the game simultaneously. The server will keep track of the players' progress and provide feedback on the guesses. So the user experience will be similar to the console version.
- Free monads in Scala, Rock the JVM, 2022
- An Intuitive Guide to Combining Free Monad and Free Applicative, Cameron Joannidis, Typelevel, 2018
- Free monad or tagless final? How not to commit to a monad too early, Scala IO FR, Adam Warski, 2017
- Why the free Monad isn't free, Kelley Robinson, Scala Days NY 2016
- The debatably Free monad, Nicolas Rinaudo, 2023
- Free Monad, Rock the JVM, 2022
- Free monads - what? and why?, Adam Warski, 2015