Option/Maybe, Either, and Future Monads in JavaScript, Python, Ruby, Swift, and Scala

Click for: original source

The comparison done by Alexey Karasev focusing on Monads in different programming languages. It gives a brief explanation of monads and shows how to implement the most useful Monads in five different programming languages.

A monad is an abstract interface that defines “pure” and “flatMap” functions. Pure lets you convert an ordinary value to monadic value. FlatMap allows functions with ordinary arguments to be applied to monadic arguments.

Using these monads you will get rid of a series of bugs like null-pointer exceptions, unhandled exceptions, and race conditions. This is what the article covers:

  • An introduction into Category Theory
  • The definition of a Monad
  • Implementations of the Option (“Maybe”) monad, Either monad, and Future monad, plus a sample program leveraging them, in JavaScript, Python, Ruby, Swift, and Scala

Category theory is a mathematical field that was actively developed in the middle of the 20th century. Now it is the basis of many functional programming concepts including the monad. The article provides a quick look into some category theory concepts, tuned for software development terminology.

Further reading and detailed code examples for 5 languages included. Nice one!

[Read More]

Tags swiftlang programming javascript scala code-refactoring