Skip to content

Commit

Permalink
Merge pull request #12 from earldouglas/readert
Browse files Browse the repository at this point in the history
Add ReaderT
  • Loading branch information
earldouglas committed Aug 22, 2015
2 parents 2528b90 + b3c5cc3 commit 47b773d
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "teep",
"description": "A JavaScript library for functional programming.",
"version": "1.0.0",
"version": "1.1.0",
"homepage": "https://github.com/earldouglas/teep",
"author": {
"name": "James Earl Douglas",
Expand Down
51 changes: 39 additions & 12 deletions src/teep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module edc {
put: (A, B) => void;
}

class DumbCache<A,B> implements Cache<A,B> {
class DumbCache<A,B> {
cache = {};
get = (k) => { return this.cache[k]; };
put = (k,v) => { this.cache[k] = v; };
Expand Down Expand Up @@ -78,7 +78,12 @@ module edc {
},
};

export interface Option<A> {
export interface Monad<A> {
map: <B>(f: (A) => B) => Monad<B>;
flatMap: <B>(f: (A) => Monad<B>) => Monad<B>;
}

export interface Option<A> extends Monad<A> {
empty : boolean;
map : <B>(f: (A) => B) => Option<B>;
flatMap : <B>(f: (A) => Option<B>) => Option<B>;
Expand Down Expand Up @@ -115,7 +120,7 @@ module edc {
}
}

export interface Validation<A> {
export interface Validation<A> extends Monad<A> {
valid : boolean;
map : <B>(f: (A) => B) => Validation<B>;
flatMap : <B>(f: (A) => Validation<B>) => Validation<B>;
Expand Down Expand Up @@ -151,7 +156,7 @@ module edc {
invalid: (errors: Array<String>) => { return new Invalid(errors); },
};

export interface List<A> {
export interface List<A> extends Monad<A> {
length : number;
map : <B>(f: (A) => B) => List<B>;
flatMap : <B>(f: (A) => List<B>) => List<B>;
Expand Down Expand Up @@ -273,15 +278,38 @@ module edc {

var future = <A>(f: (A) => any) => { return new Future(f); }

class ReaderT<A,B> {
f: (A) => Monad<B>;
constructor(f: (A) => Monad<B>) {
this.f = f;
}
apply(a: A): Monad<B> {
return this.f(a);
};
map<C>(g: (B) => C): ReaderT<A,C> {
return new ReaderT((a) => {
return this.f(a).map(g);
});
};
flatMap<C>(g: (B) => ReaderT<A,C>): ReaderT<A,C> {
return new ReaderT((a) => {
return this.f(a).map(g).flatMap((r) => { return r.apply(a); });
});
};
}

var readerT = <A,B>(f: (A) => Monad<B>) => { return new ReaderT(f); }

export var teep = {
array: array,
fn: fn,
option: option,
array: array,
fn: fn,
option: option,
validation: validation,
list: list,
promise: promise,
reader: reader,
future: future,
list: list,
promise: promise,
reader: reader,
future: future,
readerT: readerT,
};

var setExports = function () {
Expand All @@ -296,4 +324,3 @@ module edc {
!exports || setExports();

}

28 changes: 27 additions & 1 deletion teep.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,31 @@ var edc;
return Future;
})();
var future = function (f) { return new Future(f); };
var ReaderT = (function () {
function ReaderT(f) {
this.f = f;
}
ReaderT.prototype.apply = function (a) {
return this.f(a);
};
;
ReaderT.prototype.map = function (g) {
var _this = this;
return new ReaderT(function (a) {
return _this.f(a).map(g);
});
};
;
ReaderT.prototype.flatMap = function (g) {
var _this = this;
return new ReaderT(function (a) {
return _this.f(a).map(g).flatMap(function (r) { return r.apply(a); });
});
};
;
return ReaderT;
})();
var readerT = function (f) { return new ReaderT(f); };
edc.teep = {
array: array,
fn: fn,
Expand All @@ -261,7 +286,8 @@ var edc;
list: list,
promise: promise,
reader: reader,
future: future
future: future,
readerT: readerT
};
var setExports = function () {
for (var i in edc.teep) {
Expand Down
47 changes: 46 additions & 1 deletion test/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ describe('examples', function () {

});


describe('future', function () {
var async = function(x) {
return function (k) {
Expand Down Expand Up @@ -337,4 +336,50 @@ describe('examples', function () {

});

describe('readerT', function () {

var db = {
answer: 42,
};

var getAnswer = function (db) {
return teep.future(function (k) {
return k(db.answer);
});
};

function addToAnswer(x) {
return teep.readerT(function (db) {
return teep.future(function (k) {
return k(db.answer + x);
});
});
};

function verify(x, done) {
return function (y) {
if (x === y) {
done();
}
};
};

it('apply', function (done) {
teep.readerT(getAnswer).apply(db).apply(verify(42, done));
});

it('map', function (done) {
teep.readerT(getAnswer).map(function (x) {
return x + 1;
}).apply(db).apply(verify(43, done));
});

it('flatMap', function (done) {
teep.readerT(getAnswer).flatMap(function (x) {
return addToAnswer(x + 1);
}).apply(db).apply(verify(85, done));
});

});

});

0 comments on commit 47b773d

Please sign in to comment.