diff --git a/README.md b/README.md index e5b98b7..a24c65e 100644 --- a/README.md +++ b/README.md @@ -3,14 +3,14 @@ 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 ### Contributing @@ -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 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 +}); +``` 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/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); + + }); + + +}); diff --git a/middleware/README.md b/middleware/README.md new file mode 100644 index 0000000..6352277 --- /dev/null +++ b/middleware/README.md @@ -0,0 +1,33 @@ +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; + next(); + }, 10); +}); + +middleware.use(function(next) { + var self = this; + setTimeout(function() { + self.hook2 = true; + next(); + }, 10); +}); + +var start = new Date(); +middleware.go(function() { + console.log(this.hook1); // true + console.log(this.hook2); // true + console.log(new Date() - start); // around 20 +}); +``` + +More info: http://expressjs.com/guide/using-middleware.html diff --git a/package.json b/package.json index a03137f..29f1167 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", 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