Skip to content

lvitaly/free-monad

Repository files navigation

Made in Ukraine Cats Friendly Badge

Guessing Game with Free Monads

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.

Project Structure

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.

Dependencies

  • 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.

Getting Started

Prerequisites

You can use the provided flake.nix file to create a development environment with Nix and Flake. To do this, run:

nix develop

Building the Project

Console Version

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.

WebSocket Version

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.

Learn More

Talks

Articles

About

Free Monad example

Resources

Stars

Watchers

Forks