Skip to content

Commit

Permalink
Merge branch 'master' of github.com:kolodny/exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
Moshe Kolodny committed Apr 20, 2015
2 parents 52b3a16 + fb92632 commit 113c274
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 8 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
37 changes: 37 additions & 0 deletions async/README.md
Original file line number Diff line number Diff line change
@@ -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
});
```
14 changes: 14 additions & 0 deletions curry/README.md
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions memoize/README.md
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions memoize/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"scripts": {
"test": "../node_modules/.bin/mocha"
}
}
31 changes: 31 additions & 0 deletions memoize/test.js
Original file line number Diff line number Diff line change
@@ -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);

});


});
33 changes: 33 additions & 0 deletions middleware/README.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions throttle/README.md
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 113c274

Please sign in to comment.