Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

Monad and Monad Maybe
  • Loading branch information...
commit e374e66915b57a59fcee2a7a4c424817378eb0eb 1 parent 2f54f61
@kentaromiura authored
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)
Please sign in to comment.
Something went wrong with that request. Please try again.