Created
September 30, 2013 13:49
-
-
Save tarnacious/6764089 to your computer and use it in GitHub Desktop.
While writing the same parser in JavaScript and Haskell I found these two snippets illustrated to me why monads and do syntax are awesome.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
piece :: Parser Piece | |
piece = do | |
many space | |
t <- pieceType | |
c <- pieceColour | |
return $ Piece t c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
piece = _do(many(space), function(_) { | |
return _do(pieceType, function(t) { | |
return _do(colour, function(c) { | |
return ret(new Piece(t,c)); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment