Browse files

Changed Monad to Monad.Maybe, now Monad is a proper Monad.

Monad and Monad Maybe
  • Loading branch information...
1 parent 2f54f61 commit e374e66915b57a59fcee2a7a4c424817378eb0eb @kentaromiura committed Jan 28, 2013
Showing with 32 additions and 7 deletions.
  1. +10 −1 README.md
  2. +15 −0 Source/Monad.Maybe.js
  3. +1 −1 Source/Monad.js
  4. +6 −5 Tests/Monad.js
View
11 README.md
@@ -1,4 +1,13 @@
Monad
=====
-monad. Js
+monad. Js
+
+I always wanted to make some experiments with monads,
+so, in order to start my new Query project, I though it was a good idea to implement it in pure javascript.
+
+I made two version o it, the Monad.js is a standard monad while the Monad.Maybe is the Maybe Monad implementation.
+
+In the Tests folder I check for the three axioms to pass.
+
+Enjoy ;)
View
15 Source/Monad.Maybe.js
@@ -0,0 +1,15 @@
+// Monad.Maybe.js MIT License, Cristian Carlesso @kentaromiura mykenta.blogspot.com
+
+function Monad(value){
+ if (value instanceof Monad) return value
+ this.value = function(){return value}
+}
+
+Monad.prototype.bind = function(f){
+ var value = this.value()
+ if (f === Monad || value === null || value === undefined) return this
+ if (typeof f === 'function') return new Monad(f(value))
+ return this
+}
+
+module.exports = Monad
View
2 Source/Monad.js
@@ -12,7 +12,7 @@ function Monad(value){
Monad.prototype.bind = function(f){
var value = this.value()
- if (f === Monad || value === null || value === undefined) return this
+ if (f === Monad) return this
if (typeof f === 'function') return new Monad(f(value))
return this
}
View
11 Tests/Monad.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js');
-var Monad = require('../Source/Monad.js');
+var expect = require('expect.js')
+var Monad = require('../Source/Monad.js')
+var Maybe = require('../Source/Monad.Maybe.js')
describe('Monad.js', function(){
@@ -44,15 +45,15 @@ describe('Monad.js', function(){
})
- it('should return the monad itself if no value are passed to the monad', function(){
+ it('should return the maybe monad itself if no value are passed to the monad', function(){
var undefined,
- monad = new Monad(null)
+ monad = new Maybe(null)
expect(monad.bind(add1)).to.be(monad)
expect(monad.bind(add1).value()).to.be(null)
- var umonad = new Monad(undefined)
+ var umonad = new Maybe(undefined)
expect(umonad.bind(add1)).to.be(umonad)
expect(umonad).to.not.be(monad)
expect(umonad.bind(add1).value()).to.be(undefined)

0 comments on commit e374e66

Please sign in to comment.