Skip to content

Commit

Permalink
Merge pull request #18 from earldouglas/v1.2
Browse files Browse the repository at this point in the history
Release v1.2
  • Loading branch information
earldouglas committed Aug 30, 2015
2 parents b23edb5 + dd3d5d6 commit c4dfcc1
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage/
npm-debug.log
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.1.0",
"version": "1.2.0",
"homepage": "https://github.com/earldouglas/teep",
"author": {
"name": "James Earl Douglas",
Expand Down
85 changes: 74 additions & 11 deletions src/teep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,49 @@ var exports;
module edc {

var array = {
contains: <A>(xs: Array<A>, x: A): boolean => {
foldr: <A,B>(xs: Array<A>, z: B, f: (a: A, b: B) => B): B => {
var b = z;
for (var i = 0; i < xs.length; i++) {
if (xs[i] === x) {
return true;
b = f(xs[i], b);
}
return b;
},
contains: <A>(xs: Array<A>, x: A): boolean => {
return array.foldr(xs, false, (a: A, b: boolean): boolean => {
return b || x === a;
});
},
flatten: <A>(xss: Array<Array<A>>): Array<A> => {
return array.foldr(xss, [], (a: Array<A>, b: Array<A>): Array<A> => {
return b.concat(a);
});
},
map: <A,B>(xs: Array<A>, f: (A) => B): Array<B> => {
return array.foldr(xs, [], (a: A, bs: Array<B>): Array<B> => {
bs.push(f(a));
return bs;
});
},
flatMap: <A,B>(xs: Array<A>, f: (A) => Array<B>): Array<B> => {
return array.flatten(array.map(xs, f));
},
filter: <A>(xs: Array<A>, f: (A) => boolean): Array<A> => {
return array.foldr(xs, [], (a: A, as: Array<A>): Array<A> => {
if (f(a)) {
as.push(a);
}
return as;
});
},
};

var object = {
keys: (o: Object): Array<any> => {
var ks = [];
for (var k in o) {
!o.hasOwnProperty(k) || ks.push(k);
}
return false;
return ks;
},
};

Expand Down Expand Up @@ -76,6 +112,31 @@ module edc {
};
};
},
throttle: (limit: number, period: number, interval: number,
f: () => any): () => void => {
var times = [];
var throttled = function () {
var now = (new Date()).getTime();
times = array.filter(times, function (time) {
return now - time < period;
});
var withinLimit = times.length < limit
if (withinLimit) {
var newest = Math.max.apply(null, times);
var afterInterval = (now - newest) > interval;
if (afterInterval) {
times.push(now);
f();
} else {
setTimeout(throttled, interval - (now - newest));
}
} else {
var oldest = Math.min.apply(null, times);
setTimeout(throttled, period - (now - oldest));
}
};
return throttled;
}
};

export interface Monad<A> {
Expand Down Expand Up @@ -233,6 +294,10 @@ module edc {

var reader = <A,B>(f: (A) => B) => { return new Reader(f); }

var read = new Reader(function (x) {
return x;
});

class Future<A> {
f: (k: (A) => any) => any;
constructor(f) {
Expand Down Expand Up @@ -308,18 +373,16 @@ module edc {
list: list,
promise: promise,
reader: reader,
read: read,
future: future,
readerT: readerT,
};

var setExports = function () {
for (var i in teep) {
var setExport = function () {
exports[i] = teep[i];
};
!teep.hasOwnProperty(i) || setExport();
}
}
array.map(object.keys(teep), (k) => {
exports[k] = teep[k];
});
};

!exports || setExports();

Expand Down
82 changes: 72 additions & 10 deletions teep.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,48 @@ var exports;
var edc;
(function (edc) {
var array = {
contains: function (xs, x) {
foldr: function (xs, z, f) {
var b = z;
for (var i = 0; i < xs.length; i++) {
if (xs[i] === x) {
return true;
b = f(xs[i], b);
}
return b;
},
contains: function (xs, x) {
return array.foldr(xs, false, function (a, b) {
return b || x === a;
});
},
flatten: function (xss) {
return array.foldr(xss, [], function (a, b) {
return b.concat(a);
});
},
map: function (xs, f) {
return array.foldr(xs, [], function (a, bs) {
bs.push(f(a));
return bs;
});
},
flatMap: function (xs, f) {
return array.flatten(array.map(xs, f));
},
filter: function (xs, f) {
return array.foldr(xs, [], function (a, as) {
if (f(a)) {
as.push(a);
}
return as;
});
}
};
var object = {
keys: function (o) {
var ks = [];
for (var k in o) {
!o.hasOwnProperty(k) || ks.push(k);
}
return false;
return ks;
}
};
var DumbCache = (function () {
Expand Down Expand Up @@ -68,6 +103,32 @@ var edc;
get: function () { return fMemo(x); }
};
};
},
throttle: function (limit, period, interval, f) {
var times = [];
var throttled = function () {
var now = (new Date()).getTime();
times = array.filter(times, function (time) {
return now - time < period;
});
var withinLimit = times.length < limit;
if (withinLimit) {
var newest = Math.max.apply(null, times);
var afterInterval = (now - newest) > interval;
if (afterInterval) {
times.push(now);
f();
}
else {
setTimeout(throttled, interval - (now - newest));
}
}
else {
var oldest = Math.min.apply(null, times);
setTimeout(throttled, period - (now - oldest));
}
};
return throttled;
}
};
var Some = (function () {
Expand Down Expand Up @@ -203,6 +264,9 @@ var edc;
return Reader;
})();
var reader = function (f) { return new Reader(f); };
var read = new Reader(function (x) {
return x;
});
var Future = (function () {
function Future(f) {
this.f = f;
Expand Down Expand Up @@ -286,16 +350,14 @@ var edc;
list: list,
promise: promise,
reader: reader,
read: read,
future: future,
readerT: readerT
};
var setExports = function () {
for (var i in edc.teep) {
var setExport = function () {
exports[i] = edc.teep[i];
};
!edc.teep.hasOwnProperty(i) || setExport();
}
array.map(object.keys(edc.teep), function (k) {
exports[k] = edc.teep[k];
});
};
!exports || setExports();
})(edc || (edc = {}));
75 changes: 75 additions & 0 deletions test/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,39 @@ describe('examples', function () {
};

describe('array', function () {
it('foldr', function () {
var sum = function (xs) {
return teep.array.foldr(xs, 0, function (z, x) {
return z + x;
});
};
assert.equal(10, sum([1,2,3,4]));
});
it('contains', function () {
var yep = teep.array.contains([1,2,3], 2); // true
var nope = teep.array.contains([1,2,3], 4); // false
assert.equal(yep, true);
assert.equal(nope, false);
});
it('flatten', function () {
var xs = teep.array.flatten([[1,2],[3,4],[5,6]]);
assert.deepEqual([1,2,3,4,5,6], xs);
});
it('map', function () {
var xs = teep.array.map([1,2,3], times(2));
assert.deepEqual([2,4,6], xs);
});
it('flatMap', function () {
var alsoPlusOne = function (x) { return [x, x + 1]; };
var xs = teep.array.flatMap([1,3,5], alsoPlusOne);
assert.deepEqual([1,2,3,4,5,6], xs);
});
it('filter', function () {
var xs = teep.array.filter([1,2,3,4,5,6], function (x) {
return x % 2 === 1 || x === 4;
});
assert.deepEqual([1,3,4,5], xs);
});
});

describe('fn', function () {
Expand Down Expand Up @@ -118,6 +145,41 @@ describe('examples', function () {
});
});

describe('fn', function () {
it('throttle', function (done) {
var xs = [];
var f = function () {
xs.push((new Date()).getTime());
};

var limit = 5;
var period = 50;
var interval = 5;
var count = 101;
var f2 = teep.fn.throttle(limit, period, interval, f);
for (var i = 0; i < count; i++) {
f2();
}

var check = function (above, below) {
if (xs.length <= above) {
done(Error(xs.length + ' should exceed ' + above));
} else if (xs.length > below) {
done(Error(xs.length + ' should not exceed ' + below));
} else if (xs.length < count) {
setTimeout(function () {
check(above + limit, below + limit);
}, period);
} else {
done();
}
};

setTimeout(function () { check(0, limit); }, period / 2);

});
});

describe('option', function () {
var maybe42 = function (x) {
if (x === 42) {
Expand Down Expand Up @@ -286,6 +348,19 @@ describe('examples', function () {
}).apply(6));
});

it('read', function () {
var getX = teep.read.map(function (e) { return e.x; });
var getY = teep.read.map(function (e) { return e.y; });
var e = { x: 6, y: 7, };
assert.equal(42,
getX.flatMap(function (x) {
return getY.map(function (y) {
return x * y;
});
}).apply(e)
);
});

});

describe('future', function () {
Expand Down

0 comments on commit c4dfcc1

Please sign in to comment.