Game loop using the State monad in JavaScript

github repo :

This is a short article on how to create a game loop using the State monad. Unfortunately i am not going to cover the theory of State monad in this article but hopefully i would do this in a subsequent article.

using the State we can pass a state along with a value. We are going to use this to build a simple loop. It’s a simple string parsing algorithm. This simple game is an haskell example and goes like this :

Passes a string of dictionary {a,b,c}
Game is to produce a number from the string.
By default the game is off, a C toggles the
game on and off. A 'a' gives +1 and a b gives -1.
'ab' = 0
'ca' = 1
'cabca' = 0

the implementation is as follows

the idea is that we pattern match on the array

Run This

— — — — — — — — — — — — — — — — — — — — — — — —

Excerpt from :Functional Programming in Javascript

Functional Programming in Javascript

More than 14 years’ experience as full stack developer and Software Architect .