Skip to main content

Meetup 14: Monopoly in OCaml continued - part 3

· One min read
Manas Jayanth
Managing Partner at Dining Philosophers, LLP.

@theteachr showed us the minimal TUI he wrote for the game and explained the game loop.

Highlights

  1. We discussed how game loop had to be kept a pure function of game state and player moves
  2. Possible improvements with a Seq modules

Next Meetup

Checkout Upcoming Meetups

Stay in touch with us


Twitter: https://x.com/ReasonBangalore

Discord: https://discord.com/invite/Ytr36fRC4C

Meetup 13: Final: Dream webserver tutorial

· One min read
Manas Jayanth
Managing Partner at Dining Philosophers, LLP.

We discussed the last pieces essential for a web framework - routers. Last session, we had written a router that worked only in the client. We ported for server side rendering.

Highlights

  1. We discussed CSS rendering that occurs on the server on different routes
  2. We discussed the possibility of writing static site generators with the knowledge so far

Next Meetup

Checkout Upcoming Meetups

Stay in touch with us


Twitter: https://x.com/ReasonBangalore

Discord: https://discord.com/invite/Ytr36fRC4C

Meetup 12: Monopoly in OCaml continued - part 2

· One min read
Manas Jayanth
Managing Partner at Dining Philosophers, LLP.

We discussed the gameplay, possible UI for the game, and went over the stack to implement the game.

Highlights

  1. I setup Melange for @theteachr's repository. You can find the pull request here
  2. @theteachr explained the codebase and explained interesting abstractions used.
  3. Possiblilty of use of Lenses and how it improves abstractions
  4. Release of new winget package for opam - OCaml.opam

Next Meetup

Checkout Upcoming Meetups

Stay in touch with us


Twitter: https://x.com/ReasonBangalore

Discord: https://discord.com/invite/Ytr36fRC4C

Meetup 11: HTML5 Canvas game continued - part 3

· One min read
Manas Jayanth
Managing Partner at Dining Philosophers, LLP.

AC Sreedhar Reddy led the session on HTML5 canvas tutorial in Reason. You can find the code (and specifically the commit) here. Follow the readme in the repo to setup the project.

Other topics that were discussed included,

  1. The recent modular explicits PR upstream
  2. Pure FP graphics API
  3. Persistent datastructures
  4. Avoiding invalid states (ghost types etc)

Next Meetup

Checkout Upcoming Meetups

Stay in touch with us


Twitter: https://x.com/ReasonBangalore

Discord: https://discord.com/invite/Ytr36fRC4C

Meetup 10: Router for server rendered React components without Node.js - part 1

· One min read
Manas Jayanth
Managing Partner at Dining Philosophers, LLP.

We continued the development of our Dream powered Server Side rendered React components (without node.js) with routes. This meet-up, it was client side only. In a future meetup, we plan to make the router work with both server and client.

There was interesting discussion on SIMD/Vector programming and Mirage OS distribution that doesn't use libc.

Next Meetup

We will be continuing the Canvas tutorial, led by AC Sreedhar. More details here

Stay in touch with us


Twitter: https://x.com/ReasonBangalore

Discord: https://discord.com/invite/Ytr36fRC4C

Meetup 9: CSS for server rendered React components without Node.js (continued)

· One min read
Manas Jayanth
Managing Partner at Dining Philosophers, LLP.

We continued the development of our Dream powered Server Side rendered React components (without node.js) with styled-ppx

styled-ppx working only on Client side

Commit

We used Melange compiler to compile Reason to JS, which is fed to webpack.

Making sure it works on server side.

Commit

We use CssJs.render_style_tag() to generated server side rendered CSS to inject into the style tag.

Dynamic CSS props

Commit

This was uncharted territory. Thankfully, @davesnx had given a heads-up! The CSS rendering, render_style_tag must be called only after React components are rendered.

Join us on Discord if you'd like to learn more about this.


We contributed some documentation too! You can find the pull request here

Next Meetup

We learn how to use a Router for this very same server Learn more about it here

Stay in touch with us


Twitter: https://x.com/ReasonBangalore

Discord: https://discord.com/invite/Ytr36fRC4C

Meetup 8: HTML5 Canvas game (continued)

· 3 min read
Manas Jayanth
Managing Partner at Dining Philosophers, LLP.

We planned to continue the HTML5 canvas tutorial from Mozilla Developer Network, but discussed some fundamentals on abstractions first.

Abstractions that can break the OCaml typechecker

Patatruder shared the following cool snippet

let f1 x f = f x x
let f2 x = f1 (f1 x)
let f3 x = f2 (f2 x)
let f4 x = f3 (f3 x)
let f5 x = f4 (f4 x)
let f6 x = f5 (f5 x)

Abstractions that turn functions into lego blocks

As we started with the tutorial, we ran into the option type. Some of us were immediately reminded of monads. We decided to digress and revisit the topic. Here' what came out of it. (Thank you, Patatruder for the snippet).

let ( let* ) = Result.bind
let ( and* ) r1 r2 =
match r1, r2 with
| Ok r1, Ok r2 -> Ok (r1, r2)
| Error e, _ | _, Error e -> Error e

let split s =
match String.split_on_char ' ' s with
| [first; second] -> Ok (first, second)
| _ -> Error ["split"]

let single_convertion = function
| "one" -> Ok 1
| "two" -> Ok 2
| "three" -> Ok 3
| "four" -> Ok 4
| "five" -> Ok 5
| "six" -> Ok 6
| "seven" -> Ok 7
| "eight" -> Ok 8
| "nine" -> Ok 9
| "zero" -> Ok 0
| _ -> Error ["single_convertion"]

let catch cause result = Result.map_error (List.cons cause) result

let int_convertion (first, second) =
let result =
let* f = single_convertion first
and* s = single_convertion second in
Ok (f, s) in
catch "int_convertion" result

let division (a, b) =
if b = 0 || a mod b <> 0 then Error ["division"]
else Ok (a / b)

let add (x, y) = x + y

let pipeline s =
let result =
let* split = split s in
let* converted = int_convertion split in
let* divided = division converted in
let result = add (divided, 6) in
Ok result in
catch "pipeline" result

let result = pipeline "ninea eightt no"

Join us on Discord if you'd like to learn more about these examples.


We discussed package managers and dev tooling post the session.

Next Meetup

We learn how to style our server rendered React components (with a server implementation that doesn't need Node.js). Learn more about it here

Stay in touch with us


Twitter: https://x.com/ReasonBangalore

Discord: https://discord.com/invite/Ytr36fRC4C

Meetup 7: Building a Monopoly Deal Clone

· One min read
Nick
YouTuber
Manas Jayanth
Managing Partner at Dining Philosophers, LLP.

We discussed encoding the rules of the popular card game Monopoly Deal in OCaml's type system with @theteachr.

Highlights

  • Briefly discussed the gameplay.
  • Incrementally built modules around relevant data structures.
  • Explored whether constructors could clash.
    • Initially, the general understanding was that it wouldn't work. Either the compilation would fail, or one of them would not be usable. But found out that it will just need some extra [type] hinting on some occasions to get the correct type.
  • Focussed on the principal of making invalid states impossible to represent.
  • Created an incomplete game state just to start getting some feedback.

You can find the code on Github


Once again, the discussion after lasted an hour. Srijan Paul shared how dithering in images can improve GIF color depths.

Next Meetup

We will be continue last week's Canvas tutorial, lead by AC Sreedhar in a meetup. More details here

Stay in touch with us


Twitter: https://twitter.com/ReasonBangalore

Discord: https://discord.com/invite/RamP7SCKcU

Meetup 6: MDN Canvas tutorial in Reason with Melange

· One min read
Manas Jayanth
Managing Partner at Dining Philosophers, LLP.

The most interactive meetup in while! 30 minutes past the set meetup time, and discussions still did not end. We had to take it over to a Discord voice channel.

We had a very interesting mix of hobbyist OCamlers, static analysis engineers and newcomers discussing what OCaml is and how it compares to Java, Scala and others. As an organiser, I usually set aside 15-20 minutes, before and after the planned discussions, for free-form discussions. This one happened to be the most lively one. We shared important links on this Discord thread

We, then, proceeded to live-code the HTML5 Canvas game tutorial on Mozilla Developer Network but with Reason. The code can be found here.

Next Meetup

We couldn't cover the entire tutorial. So, it will be continued by AC Sreedhar in a future meetup. More details here

Next weekend, we'll be discussing Nikhil Bijapur's Monopoly Deal, a card game. We plan to learn how this game was built with OCaml and live-code with him. Details can be found here

Stay in touch with us


Twitter: https://twitter.com/ReasonBangalore

Discord: https://discord.com/invite/RamP7SCKcU

Meetup 5: High level overview of how Melange compiler works

· 2 min read
Manas Jayanth
Managing Partner at Dining Philosophers, LLP.

I lead this discussion. Here are the highlights.

High-level overview of the compilers internal pipelines.

I created the following drawing with Figma to help visualise.

OCaml compiler pipelines visualised

Compiler commands to produce JS artifacts (without a build system).

We need a sandbox with melange, reason and ocaml compiler.

{
"dependencies": {
"ocaml": "5.x",
"@opam/reason": "*",
"@opam/melange": "*",
"@opam/ocaml-lsp-server": "*",
"@opam/ocamlformat": "*",
"@opam/ocamlformat-rpc": "*",
"@opam/dot-merlin-reader": "*"
}
}

Simple hello.ml with just one print_endline can be compiled with

esy melc ./hello.ml

If Belt is added to the hello.ml, path to melange libaries need to be included with -I flag

esy melc -I /Users/username/.esy/3__________________________________________________________________/i/opam__s__melange-opam__c__4.0.0-51-9d2a6c24/lib/melange/belt/melange ./hello.ml

To compile Reason files, -pp refmt --print binar is needed

esy melc -pp 'refmt --print binary' -impl hello.re -o hello.js

If a package were to be imported, then ocamlfind would be used to figure the include path

How all this knowledge could be used to implement a webpack loader.

A webpack loader attempting this can be found here

This loader is used here on this website as a docusaurus plugin

Next Meetup

Next, we use the webpack loader and continue working on the bare bones game demo running here. No prior knowledge needed other than basic HTML5 Canvas. Not even this meetup's discussion.

Stay in touch with us


Twitter: https://twitter.com/ReasonBangalore

Discord: https://discord.com/invite/RamP7SCKcU