JavaScript micro library - POC
Very simple library that, I think implement a monad in Javascript and add some funtionaly to functions that belong to orignal object wraped by Monadic Monad.
Functions wraped by Monadic, are automaticaly chainable if they don't return a value. Functions are executed in the context of orignal object. Functions that modify an object modify local copy of that object that can be retrieve at the end of monadic chain.
Monadic function wrap you original object and add new methods add
and get
var o = {
baz: 2,
foo: function(a) {
this.baz += a;
},
bar: function(a) {
this.baz = a * a;
}
};
Monadic(o).bar(10).foo(10).add('baz', function() {
this.baz += 10;
return this;
}).baz().get().baz;
you will get 120. In orignal o object baz will still equal 2.
You can add monadic
method to Object prototype using:
Monad.install();
so all objects will have monadic
method that will create Monadic Monad from
that object.
Your wrapped functions inside Monadic have these methods (methods that accept a function will return new function to get monadic object you need to call self).
You can chain these methods
console = console.monadic({monad: false}).log.when(function() {
if (!debug) {
return false;
}
}).log.before(function(str) {
if (str.match(/LOG/)) {
// save log in the file
}
return str;
});
When you pass false to monadic method it will keep context of orignal object,
it will not create new object each time you call a method
(it will not be real Monad then), so you can pass log
as callback.
[1,2,3,4].forEach(console.monadic({monad: false}).log.limit(1));
In this example foo
function is modified after it return it's value so
foo
is always returning 10. Argument to after
function is a value returned
by a function.
Monadic({ foo: function(a) { return 10/a; } }).foo.before(function(a) {
if (a == 0) {
throw 'You should not divide by 0, you know';
} else {
return a;
}
}).self().foo(0);
NOTE: fuction methods are not creating new Monad so they modify original, current Monad object
Copyright (C) 2012 Jakub Jankiewicz <http://jcubic.pl>
Licensed under GNU GPL Version 3 license