From 31748af608cb67cb1c0a81a4a57e2d88dd48de84 Mon Sep 17 00:00:00 2001 From: Moshe Kolodny Date: Thu, 16 Apr 2015 11:25:16 -0400 Subject: [PATCH 01/10] added memoize --- memoize/package.json | 6 ++++++ memoize/test.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 memoize/package.json create mode 100644 memoize/test.js diff --git a/memoize/package.json b/memoize/package.json new file mode 100644 index 0000000..57a9b22 --- /dev/null +++ b/memoize/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "scripts": { + "test": "../node_modules/.bin/mocha" + } +} diff --git a/memoize/test.js b/memoize/test.js new file mode 100644 index 0000000..002c11b --- /dev/null +++ b/memoize/test.js @@ -0,0 +1,31 @@ +var assert = require('assert'); +var memoize = require('./'); + +describe('memoize', function() { + + it('can handle a single argument', function() { + var called = 0; + var fib = memoize(function(n) { + called++; + if (n < 2) return n; + return fib(n - 1) + fib(n - 2); + }); + fib(10); + assert.equal(called, 11); + }); + + it('can handle multiple arguments', function() { + var called = 0; + var fib = memoize(function(n, unused) { + called++; + if (n < 2) return n; + return fib(n - 1, unused) + fib(n - 2, unused); + }); + fib(10, 'x'); + fib(10, 'y'); + assert.equal(called, 22); + + }); + + +}); From fd56bb03137c8b2fe3d8a6ef48a47bca1866fef4 Mon Sep 17 00:00:00 2001 From: Moshe Kolodny Date: Thu, 16 Apr 2015 12:00:49 -0400 Subject: [PATCH 02/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e5b98b7..a0b560d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ cd throttle vi index.js npm test ``` - +This uses a basic TDD approach so take a look at the test.js file in each directory to see what needs to be implemented, write an index.js as the solution file ### Contributing From e6b5f6dcb1ec529b7b5c12d83be7350005308ce3 Mon Sep 17 00:00:00 2001 From: Moshe Kolodny Date: Thu, 16 Apr 2015 12:54:37 -0400 Subject: [PATCH 03/10] don't allow npm test from parent dir --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index a03137f..2c1056e 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,8 @@ "private": true, "description": "some javascript exercises", "scripts": { - "test": "mocha '**/test.js'" + "test": "echo \"Error: run npm test from inside the challange directories or do `npm run test-all`\" && exit 1", + "test-all": "mocha **/test.js" }, "author": "Moshe Kolodny", "license": "MIT", From 955ffc5321f5c778f64cde3a4a0e8f47bf667f41 Mon Sep 17 00:00:00 2001 From: Moshe Kolodny Date: Thu, 16 Apr 2015 12:56:20 -0400 Subject: [PATCH 04/10] work on windows systems --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2c1056e..29f1167 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "some javascript exercises", "scripts": { "test": "echo \"Error: run npm test from inside the challange directories or do `npm run test-all`\" && exit 1", - "test-all": "mocha **/test.js" + "test-all": "mocha '**/test.js'" }, "author": "Moshe Kolodny", "license": "MIT", From 85313cc024acf7be6aeaf730d1439b222115ed61 Mon Sep 17 00:00:00 2001 From: Moshe Kolodny Date: Thu, 16 Apr 2015 12:58:20 -0400 Subject: [PATCH 05/10] clarify workflow in readme --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a0b560d..3c80934 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,12 @@ These are some basic (and advanced) coding challenges Here's the basic workflow: ```bash -git clone https://github.com/kolodny/exercises -cd exercises -npm install -cd throttle -vi index.js -npm test +[~] $ git clone https://github.com/kolodny/exercises +[~] $ cd exercises +[exercises] $ npm install +[exercises] $ cd throttle +[throttle] $ vi index.js +[throttle] $ npm test ``` This uses a basic TDD approach so take a look at the test.js file in each directory to see what needs to be implemented, write an index.js as the solution file From 9e507a32efa00e5010731434f6d3043422dbfcaa Mon Sep 17 00:00:00 2001 From: Moshe Kolodny Date: Thu, 16 Apr 2015 13:16:11 -0400 Subject: [PATCH 06/10] added readme to async --- async/README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 async/README.md diff --git a/async/README.md b/async/README.md new file mode 100644 index 0000000..a62eba5 --- /dev/null +++ b/async/README.md @@ -0,0 +1,37 @@ +Here's the basic usage of the file that you'll be creating: + +```js +var async = require('./') // <- this is the file you make; + +var getUser = function(userId) { + return function(cb) { + setTimeout(function() { + cb(null, {userId: userId, name: 'Joe'}); + }, Math.random() * 100); + }; +}; + +var upperCaseName = function(cb, user) { + cb(null, user.name.toUpperCase()); +}; + +var userThunk = getUser(22); + +async.sequence([userThunk, upperCaseName])(function(err, data) { + console.log(data); // JOE +}); + +var userThunk1 = getUser(1); +var userThunk2 = getUser(2); + +async.parallel([userThunk1, userThunk2])(function(err, users) { + console.log(users); // [ { userId: 1, name: 'Joe' }, { userId: 2, name: 'Joe' } ] +}); + +var faster = function(cb) { + setTimeout(cb.bind(null, null, "I'm faster"), 10); +} +async.race([userThunk1, faster])(function(err, winner) { + console.log(winner); // I'm faster +}); +``` From ca3da29dcd0b9b94ab2d3ce44900de511bb1e2cd Mon Sep 17 00:00:00 2001 From: Moshe Kolodny Date: Thu, 16 Apr 2015 15:38:14 -0400 Subject: [PATCH 07/10] added more reademes --- curry/README.md | 14 ++++++++++++++ memoize/README.md | 22 ++++++++++++++++++++++ middleware/README.md | 31 +++++++++++++++++++++++++++++++ throttle/README.md | 20 ++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 curry/README.md create mode 100644 memoize/README.md create mode 100644 middleware/README.md create mode 100644 throttle/README.md diff --git a/curry/README.md b/curry/README.md new file mode 100644 index 0000000..be3de36 --- /dev/null +++ b/curry/README.md @@ -0,0 +1,14 @@ +Here's the basic usage of the file that you'll be creating: + +```js +var curry = require('./') // <- this is the file you make; + +function add(a, b) { + return a + b; +} + +var curried = curry(add); +console.log( curied(1)(2) ); // 3 +``` + +More info: http://en.wikipedia.org/wiki/Curry_%28programming_language%29 diff --git a/memoize/README.md b/memoize/README.md new file mode 100644 index 0000000..9a827be --- /dev/null +++ b/memoize/README.md @@ -0,0 +1,22 @@ +Here's the basic usage of the file that you'll be creating: + +```js +var memoize = require('./') // <- this is the file you make; + +function expensiveOperation() { + console.log('this should be shown once'); + return 22; +} + +var memoized = memoize(expensiveOperation); +console.log(memoized()); +console.log(memoized()); + +// the console should show: +// this should be shown once +// 22 +// 22 + +``` + +More info: http://en.wikipedia.org/wiki/Memoization diff --git a/middleware/README.md b/middleware/README.md new file mode 100644 index 0000000..a0da1df --- /dev/null +++ b/middleware/README.md @@ -0,0 +1,31 @@ +Middleware is the programming pattern of providing hooks with a resume callback. +Here's the basic usage of the file that you'll be creating: + +```js +var Middleware = require('./'); // <- this is the file you make; + +var middleware = new Middleware(); + +middleware.use(function(next) { + var self = this; + setTimeout(function() { + self.hook1 = true; + }, 10); +}); + +middleware.use(function(next) { + var self = this; + setTimeout(function() { + self.hook2 = true; + }, 10); +}); + +var start = new Date(); +middleware.go(function() { + console.log(this.hook1); // true + console.log(this.hook1); // true + console.log(new Date() - start); // around 20 +}); +``` + +More info: http://expressjs.com/guide/using-middleware.html diff --git a/throttle/README.md b/throttle/README.md new file mode 100644 index 0000000..765f752 --- /dev/null +++ b/throttle/README.md @@ -0,0 +1,20 @@ +Here's the basic usage of the file that you'll be creating: + +```js +var throttle = require('./') // <- this is the file you make; + +var sayHi = function() { + console.log('hi'); +}; + +var throttled = throttle(sayHi, 100); + +sayHi(); +sayHi(); +sayHi(); +sayHi(); + +// there should only be one 'hi' message on the console +``` + +More info: http://underscorejs.org/#throttle From 2186ac69c5d6db5df2d97e58e9a635880a82424b Mon Sep 17 00:00:00 2001 From: Moshe Kolodny Date: Thu, 16 Apr 2015 15:41:15 -0400 Subject: [PATCH 08/10] updated main readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3c80934..a24c65e 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,4 @@ Pull requests welcome, please follow the basic workflow: 1. Make a folder 2. Copy a package.json from a sibling folder 3. Make a test.js file +4. Optionally provide a README.md From bd9f9d3a654ccf34ddb48a8daa5ddd0d029ef68c Mon Sep 17 00:00:00 2001 From: Moshe Kolodny Date: Fri, 17 Apr 2015 08:09:32 -0400 Subject: [PATCH 09/10] Update README.md --- middleware/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/middleware/README.md b/middleware/README.md index a0da1df..e6dcade 100644 --- a/middleware/README.md +++ b/middleware/README.md @@ -10,6 +10,7 @@ middleware.use(function(next) { var self = this; setTimeout(function() { self.hook1 = true; + next(); }, 10); }); @@ -17,6 +18,7 @@ middleware.use(function(next) { var self = this; setTimeout(function() { self.hook2 = true; + next(); }, 10); }); From fb92632338dbfd81ba42a345235984afd65fca5e Mon Sep 17 00:00:00 2001 From: Moshe Kolodny Date: Fri, 17 Apr 2015 08:10:06 -0400 Subject: [PATCH 10/10] Update README.md --- middleware/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/middleware/README.md b/middleware/README.md index e6dcade..6352277 100644 --- a/middleware/README.md +++ b/middleware/README.md @@ -25,7 +25,7 @@ middleware.use(function(next) { var start = new Date(); middleware.go(function() { console.log(this.hook1); // true - console.log(this.hook1); // true + console.log(this.hook2); // true console.log(new Date() - start); // around 20 }); ```