Skip to content

Commit 83d5687

Browse files
committed
Migrated code for most exercises. A few remain to be ported and adapted.
1 parent 6e4ced6 commit 83d5687

File tree

79 files changed

+356
-198
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+356
-198
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
22
.DS_Store
3+
npm-debug.log
4+
TODO.md
File renamed without changes.

problems/basic_call/setup.js renamed to exercises/basic_call/exercise.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
"use strict"
22

3-
var input = require('../../input')
3+
var runner = require('../../runner')
4+
var util = require('util')
45

56
function randomInt(min, max) {
6-
return Math.floor((Math.random() * (max - min + 1)) + min)
7+
return Math.floor((Math.random() * (max - min)) + min)
78
}
89

9-
module.exports = input(new Array(randomInt(0, 20))
10-
.join(',')
11-
.split(',')
12-
.map(function() {
10+
var input = Array.apply(null, { length: randomInt(0, 20) }).map(function() {
1311
return randomInt(0, 10)
14-
})).wrap(function(input, mod) {
15-
var numbers = input[0]
12+
})
13+
14+
module.exports = runner.custom(function(fx, numbers) {
1615
var valid = 1
1716
var objects = [{quack: true}].concat(numbers.map(function(num) {
1817
switch(num) {
@@ -60,5 +59,5 @@ module.exports = input(new Array(randomInt(0, 20))
6059
}
6160
}))
6261

63-
console.log('Matched %d of %d valid objects from %d total.', mod.apply(mod, objects), valid, objects.length)
64-
})
62+
return util.format('Matched %d of %d valid objects from %d total.', fx.apply(null, objects), valid, objects.length)
63+
})(input)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict"
2+
3+
var loremIpsum = require('lorem-ipsum')
4+
var runner = require('../../runner')
5+
6+
function randomInt(min, max) {
7+
return Math.floor((Math.random() * (max - min)) + min)
8+
}
9+
10+
function makeUser() {
11+
return {
12+
id: randomInt(0, 1000),
13+
name: loremIpsum().split(' ').slice(0, 2).map(function(word) {
14+
word[0] = word[0].toUpperCase();
15+
return word;
16+
}).join(' ')
17+
}
18+
}
19+
20+
function makeListOfUsers() {
21+
return Array.apply(null, { length : randomInt(10, 100) }).map(makeUser)
22+
}
23+
24+
var good = makeListOfUsers()
25+
var bad = makeListOfUsers()
26+
var lists = Array.apply(null, {length: 20}).map(function() {
27+
return Array.apply(null, {length: 20}).map(function() {
28+
if (Math.random() < 0.95) {
29+
return good[randomInt(0, 10)]
30+
} else {
31+
return bad[randomInt(0, 10)]
32+
}
33+
})
34+
})
35+
36+
module.exports = runner.custom(function(fx, good, lists) {
37+
var test = fx(good)
38+
39+
var goodLists = 0
40+
41+
lists.forEach(function(list) {
42+
test(list) && ++goodLists
43+
})
44+
45+
return 'found ' + goodLists + ' good lists!'
46+
}).hideInput(good, lists)

exercises/basic_filter/exercise.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict"
2+
3+
var loremIpsum = require('lorem-ipsum')
4+
var runner = require('../../runner')
5+
6+
function randomInt(min, max) {
7+
return Math.floor((Math.random() * (max - min)) + min)
8+
}
9+
10+
var input = new Array(randomInt(10, 30)).join(',').split(',')
11+
.map(function() { return { message: loremIpsum() } })
12+
13+
module.exports = runner.hideInput(input)

exercises/basic_map/exercise.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict"
2+
3+
var runner = require('../../runner')
4+
5+
function randomInt(min, max) {
6+
return Math.floor((Math.random() * (max - min)) + min)
7+
}
8+
9+
var input = new Array(randomInt(0, 19)).join(',').split(',')
10+
.map(function() { return randomInt(0, 9) })
11+
12+
var regularMap = Array.prototype.map, usedMap
13+
Array.prototype.map = function() {
14+
usedMap = true
15+
return regularMap.apply(this, arguments)
16+
}
17+
18+
module.exports = runner.init(function() {
19+
usedMap = false
20+
}).wrapUp(function(callback) {
21+
if (!usedMap) {
22+
this.emit('fail', this.__('didnt_use_map'));
23+
} else {
24+
this.emit('pass', this.__('used_map'));
25+
}
26+
callback(null, usedMap)
27+
})(input)

exercises/basic_recursion/exercise.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict"
2+
3+
var loremIpsum = require('lorem-ipsum')
4+
var runner = require('../../runner')
5+
6+
var input = loremIpsum({count: 1, units:'paragraphs'})
7+
.replace(/([^\w ])/g, '')// remove non-words and spaces
8+
.toLowerCase() // lowercase I guess
9+
.split(' ') // create array of words
10+
11+
module.exports = runner.custom(function(fx, input) {
12+
return fx(input, function(prev, curr) {
13+
prev[curr] = ++prev[curr] || 1
14+
return prev
15+
}, {})
16+
})(input)

exercises/basic_reduce/exercise.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict"
2+
3+
var loremIpsum = require('lorem-ipsum')
4+
var runner = require('../../runner')
5+
6+
var input = loremIpsum({ count: 1, units: 'paragraphs' })
7+
.replace(/([^\w ])/g, '')// remove non-words and spaces
8+
.toLowerCase() // lowercase I guess
9+
.split(' ') // create array of words
10+
11+
module.exports = runner(input)
File renamed without changes.
File renamed without changes.

exercises/hello_world/exercise.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict"
2+
3+
var runner = require('../../runner')
4+
5+
module.exports = runner(require('lorem-ipsum')())
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict"
2+
3+
var runner = require('../../runner')
4+
5+
function randomInt(min, max) {
6+
return Math.floor((Math.random() * (max - min)) + min)
7+
}
8+
9+
var counter
10+
11+
module.exports = runner.init(function() {
12+
console.log("------------------------")
13+
counter = 0
14+
}).quiet(function count() {
15+
console.log("Called function %d times.", ++counter)
16+
}, randomInt(3, 10))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict"
2+
3+
var runner = require('../../runner')
4+
5+
function randomInt(min, max) {
6+
return Math.floor((Math.random() * (max - min)) + min)
7+
}
8+
9+
var numbers = Array.apply(null, {length: Math.random() * 20 + 1}).map(function() {
10+
return randomInt(0, 9)
11+
})
12+
13+
module.exports = runner(numbers, function(item) { return item * 3 })
File renamed without changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict"
2+
3+
var loremIpsum = require('lorem-ipsum')
4+
var runner = require('../../runner')
5+
6+
var input = Array.apply(null, {length: Math.random() * 20 + 1}).map(function() {
7+
return loremIpsum()
8+
})
9+
10+
// FIXME: THIS DOES NOT CAPTURE console.log OUTPUT PROPERLY (as it happens
11+
// inside the runner, not in child processes). WE NEED TO CHANGE THIS INTO
12+
// NON-runner, RAW EXERCISE CODE WITH A WRAPPER MODULE FOR SUBMISSION/SOLUTION CMDS
13+
14+
module.exports = runner.custom(function(fx, input) {
15+
console.log.bind = function() {
16+
throw new Error('Try implementing this without bind!')
17+
}
18+
19+
var info = fx('INFO:')
20+
var warn = fx('WARN:')
21+
input.forEach(function(message, i) {
22+
if (i % 2 === 0) info.apply(null, message.split(' '))
23+
else warn.apply(null, message.split(' '))
24+
})
25+
})(input)
File renamed without changes.
File renamed without changes.

functional-javascript.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ var path = require('path')
77

88
Workshopper({
99
name : 'functional-javascript'
10-
, title : 'FUNCTIONAL JAVASCRIPT IS GOOD'
11-
, appDir : path.join(__dirname)
12-
}).init()
10+
, appDir : __dirname
11+
, languages : ['en', 'fr']
12+
})

i18n/en.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"title": "FUNCTIONAL JAVASCRIPT IS GOOD",
3+
"subtitle": "\u001b[23mSelect an exercise and hit \u001b[3mEnter\u001b[23m to begin",
4+
"common": {
5+
"exercise": {
6+
"fail": {
7+
"missing_deps": "You need to install all of the dependencies you are using in your solution (e.g. lodash)",
8+
"module_not_found": "Could not find your file. Make sure the path is correct.",
9+
"must_export_function": "You should always return a function using the module.exports object."
10+
},
11+
"input": "input: %j",
12+
"submission": "submission: %j",
13+
"solution": "solution: %j"
14+
}
15+
},
16+
"exercises": {
17+
"Basic: Map": {
18+
"didnt_use_map": "You did not use Array#map",
19+
"used_map": "Yay! You used Array#map"
20+
}
21+
}
22+
}

i18n/fr.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"title": "JAVASCRIPT FONCTIONNEL C’EST LE BIEN",
3+
"subtitle" : "\u001b[23mSélectionnez un exercice et tapez \u001b[3mEnter\u001b[23m pour démarrer",
4+
"common": {
5+
"exercise": {
6+
"fail": {
7+
"missing_deps": "You need to install all of the dependencies you are using in your solution (e.g. lodash)",
8+
"module_not_found": "Could not find your file. Make sure the path is correct.",
9+
"must_export_function": "You should always return a function using the module.exports object."
10+
},
11+
"input": "entrée: %j",
12+
"submission": "votre résultat : %j",
13+
"solution": "résultat attendu : %j"
14+
}
15+
},
16+
"exercise": {
17+
"Hello World": "Bonjour Monde",
18+
"Higher Order Functions": "Fonctions d’Ordre Supérieur",
19+
"Basic: Map": "Les bases : Map",
20+
"Basic: Filter": "Les bases : Filter",
21+
"Basic: Every Some": "Les bases : Every, Some",
22+
"Basic: Reduce": "Les bases : Reduce",
23+
"Basic: Recursion": "Les bases : Récursion",
24+
"Basic: Call": "Les bases : Call",
25+
"Partial Application without Bind": "Application Partielle sans Bind",
26+
"Partial Application with Bind": "Application Partielle avec Bind",
27+
"Implement Map with Reduce": "Implémenter Map à l’aide de Reduce",
28+
"Function Spies": "Espions sur Fonctions",
29+
"Blocking Event Loop": "Boucle d’Événements Bloquée",
30+
"Trampoline": "Trampoline",
31+
"Async Loops": "Boucles Asynchrones",
32+
"Recursion": "Récursion",
33+
"Currying": "Currying",
34+
"Function Call": "Appel de Fonction"
35+
},
36+
"exercises": {
37+
"Basic: Map": {
38+
"didnt_use_map": "Vous n’avez pas utilisé Array#map",
39+
"used_map": "Youpi ! Vous avez utilisé Array#map"
40+
}
41+
}
42+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"author": "Tim Oxley",
1818
"license": "MIT",
1919
"dependencies": {
20-
"workshopper": "~0.7.2",
20+
"workshopper": "^2.3.1",
21+
"workshopper-exercise": "^2.3.0",
22+
"deep-eql": "^0.1.3",
2123
"lorem-ipsum": "~0.1.1"
2224
},
2325
"repository": {

problems/basic_every_some/setup.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

problems/basic_filter/setup.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)