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

Diff for: .gitignore

+2
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.
File renamed without changes.

Diff for: problems/basic_call/setup.js renamed to exercises/basic_call/exercise.js

+9-10
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)
File renamed without changes.

Diff for: exercises/basic_every_some/exercise.js

+46
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)

Diff for: exercises/basic_filter/exercise.js

+13
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)
File renamed without changes.

Diff for: exercises/basic_map/exercise.js

+27
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)
File renamed without changes.

Diff for: exercises/basic_recursion/exercise.js

+16
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)

Diff for: exercises/basic_reduce/exercise.js

+11
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: exercises/hello_world/exercise.js

+5
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')())
File renamed without changes.

Diff for: exercises/higher_order_functions/exercise.js

+16
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))

Diff for: exercises/implement_map_with_reduce/exercise.js

+13
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 })

Diff for: menu.json renamed to exercises/menu.json

File renamed without changes.
+25
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.
File renamed without changes.
File renamed without changes.

Diff for: functional-javascript.js

+3-3
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+
})

Diff for: i18n/en.json

+22
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+
}

Diff for: i18n/fr.json

+42
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+
}

Diff for: package.json

+3-1
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": {

Diff for: problems/basic_every_some/setup.js

-50
This file was deleted.

Diff for: problems/basic_filter/setup.js

-14
This file was deleted.

0 commit comments

Comments
 (0)