-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:kolodny/exercises
- Loading branch information
Showing
9 changed files
with
173 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"test": "../node_modules/.bin/mocha" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
}); | ||
|
||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |