Skip to content

Commit a9cb579

Browse files
committed
demo progress
1 parent 81faf38 commit a9cb579

27 files changed

+215
-153
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Functional School
22

3-
A trip through functional programming.
3+
A trip through functional programming in Javascript.
44

55

66
## CodeRoad
@@ -19,13 +19,15 @@ CodeRoad is an open-sourced interactive tutorial platform for the Atom Editor. L
1919

2020
## Outline
2121

22-
### The Basics
22+
### Array Methods
2323

24-
Some basic methods in Javascript
24+
Using common built-in Javascript array methods such as `map` & `reduce`.
25+
26+
By the end, you should have an understanding of how to use array methods to manipulate semi-complex data.
2527

2628
##### Filter
2729

28-
We'll start by looking at the `filter` method.
30+
Array -> Array of items that match a condition
2931

3032
##### Map
3133

coderoad.json

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
11
{
22
"project": {
33
"title": "Functional School",
4-
"description": "A trip through functional programming."
4+
"description": "A trip through functional programming in Javascript."
55
},
66
"chapters": [
77
{
8-
"title": "The Basics",
8+
"title": "Array Methods",
99
"pages": [
1010
{
1111
"title": "Filter",
12-
"description": "We'll start by looking at the `filter` method.",
13-
"explanation": "Last semester's grades just came in, but there's just too much data.\n\nUse the `filter` method to shorten the data to only what you need.\n\n```\n['a', 'b', 'c'].filter(function(item) {\n return item === 'a';\n});\n// ['a']\n```",
12+
"description": "Array -> Array of items that match a condition",
13+
"explanation": "You've hacked into the school's computer system, and just in time. The grades are in, aren't you're not too proud of your performance. But that's okay, you have a plan: you're going to create a fake report card.\n\nIt would be great if you could `filter` the scores that your parents will see. A `filter` takes a matching condition function and only returns items that result in true. As an example, look at `isB` below:\n\n```\nfunction isB(x) {\n return x === 'b';\n}\n```\n\nLike all of the methods in this chapter, `filter` is already part of the `Array.prototype`, so you can run it after any array. Each item is passed into the params of the condition function, one by one.\n\n```\nvar list = ['a', 'b', 'c'];\nlist.filter(isB);\n//> ['b']\n```\n\nThere's too much student data in the computer system. We'll have to sort through it. Have a look at an example below:\n\n```\nconsole.log(studentData[0]);\n//> { course: 'Web Security',\n// instructor: 'Sue Denim',\n// name: 'Rebecca Heineman',\n// score: 93,\n// grade: 'A' }\n```",
1414
"tasks": [
1515
{
16-
"description": "Set `var myData` equal to `filter`ed down data with your name: \"Jane\".",
16+
"description": "Write a filter condition function called `isAda` that returns true if param.`name` is true",
1717
"tests": [
18-
"1/01/filter-01"
18+
"1/01/01-filter"
1919
],
2020
"actions": [
2121
"open('01-filter.js')",
22-
"set('var data = [\n { name: \"Joe\", class: \"Computer Science\", grade: \"C\" },\n { name: \"Jane\", class: \"Computer Science\", grade: \"D\" },\n { name: \"Mo\", class: \"Computer Science\", grade: \"B\" },\n { name: \"Bob\", class: \"Computer Science\", grade: \"F\" },\n { name: \"Joe\", class: \"Math\", grade: \"C\" },\n { name: \"Jane\", class: \"Math\", grade: \"B\" },\n { name: \"Mo\", class: \"Math\", grade: \"D\" },\n { name: \"Bob\", class: \"Math\", grade: \"A\" },\n { name: \"Joe\", class: \"Art\", grade: \"C\" },\n { name: \"Jane\", class: \"Art\", grade: \"F\" },\n { name: \"Mo\", class: \"Art\", grade: \"B\" },\n { name: \"Bob\", class: \"Math\", grade: \"F\" }\n];\n')",
23-
"insert('var myData = data.filter(function(item) {\n // return true to add the item to the list\n});\n\n')"
22+
"set('function isAda(x) {\n // write condition here\n // the name must match \"Ada Lovelace\"\n}\n')"
23+
]
24+
},
25+
{
26+
"description": "Set `var myData` equal to data matching your name, \"Ada Lovelace\".",
27+
"tests": [
28+
"1/01/02-filter"
29+
],
30+
"actions": [
31+
"insert('var data = require('./tutorial/data/students')(require('./tutorial/data/courses.json')).slice(0);\n')",
32+
"insert('var myData = data.filter // call filter condition here\n')"
2433
]
2534
},
2635
{
27-
"description": "Set `var myFails` to the result of `filter`ing down your data to only your failing grades (\"D\" or \"F\").",
36+
"description": "Set `var myBest` to your scores, excluding any grades that are \"D\" or \"F\".",
2837
"tests": [
29-
"1/01/filter-02"
38+
"1/01/03-filter"
3039
],
3140
"actions": [
32-
"insert('var myFails = myData.filter(function() {\n // add filter here, don't forget the parameter\n});\n\n')"
41+
"insert('var myBest = myData.filter // filter out \"D\"'s and \"F\"'s here\n')"
3342
]
3443
}
3544
]
@@ -83,7 +92,7 @@
8392
"description": "coming soon"
8493
}
8594
],
86-
"description": "Some basic methods in Javascript"
95+
"description": "Using common built-in Javascript array methods such as `map` & `reduce`.\n\nBy the end, you should have an understanding of how to use array methods to manipulate semi-complex data."
8796
}
8897
]
8998
}

dev/generate.js

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,22 @@
11
#!/usr/bin/env node
22

33
(function() {
4-
var data = require('./data.json').slice(0);
5-
var classes = [1, 2, 3, 4];
6-
var name = "Ada Lovelace";
7-
var names = [
8-
"Grace Hopper",
9-
"Danielle Bunten Berry",
10-
"Rebecca Heineman",
11-
"Xiao Tian",
12-
"Ying Cracker",
13-
"Niklaus Wirth",
14-
"James Gosling",
15-
"Ken Thompson",
16-
"Donald Knuth",
17-
"Brian Kernaghan",
18-
"Tim Berners-Lee",
19-
"Linus Torvalds",
20-
"Hack Kerr",
21-
"Kevin Mitnick",
22-
"Albert Gonzalez"
23-
];
24-
var output = [];
4+
var data = require('./data.json').slice(0);
5+
var classes = [1, 2, 3, 4];
6+
var name = "Ada Lovelace";
7+
var names = ["Albert Gonzalez", "Brian Kernaghan", "Danielle Bunten Berry", "Donald Knuth", "Grace Hopper", "Hack Kerr", "James Gosling", "Ken Thompson", "Kevin Mitnick", "Linus Torvalds", "Niklaus Wirth", "Rebecca Heineman", "Tim Berners-Lee", "Xiao Tian", "Ying Cracker"];
8+
var output = [];
259

26-
function generate(data) {
27-
console.log('generating data...');
28-
output = data.map(function(classItem) {
29-
classItem.students = createStudentsInClass();
30-
classItem.average = classAverage(classItem.students);
31-
return classItem;
32-
});
33-
console.log("[");
34-
output.forEach(function(classItem, index) {
35-
console.log(`{ "course": "${classItem.course}", "instructor": "${classItem.instructor}", "average": "${classItem.average}", "students": [${classItem.students.map(function(student) {
10+
function generate(data) {
11+
console.log('generating data...');
12+
output = data.map(function(classItem) {
13+
classItem.students = createStudentsInClass();
14+
classItem.average = classAverage(classItem.students);
15+
return classItem;
16+
});
17+
console.log("[");
18+
output.forEach(function(classItem, index) {
19+
console.log(`{ "course": "${classItem.course}", "instructor": "${classItem.instructor}", "average": "${classItem.average}", "students": [${classItem.students.map(function(student) {
3620
return `{ "section": "${student.section}", "name": "${student.name}", "score": ${student.score}, "grade": "${student.grade}" }`
3721
})}] }`);
3822
if (index !== output.length - 1) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"chai": "3.5.0",
1313
"chai-spies": "0.7.1",
14+
"lodash": "4.3.0",
1415
"mocha": "2.4.5"
1516
},
1617
"license": "MIT",

tutorial/1/01/01-filter.spec.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
var expect = require('chai').expect;
2-
32
// loads file context to allow globals
43
var context = require('test-context');
54
var filePath = '../../../../../01-filter.js';
65
context(filePath);
76

8-
describe('var myData', function() {
7+
describe('isAda', function() {
98

109
it('doesn\'t exist', function() {
11-
expect(myData).to.not.be.undefined;
10+
expect(isAda).to.not.be.undefined;
1211
});
1312

14-
it('isn\'t an array', function() {
15-
expect(myData).to.be.an('array');
13+
it('isn\'t a Function', function() {
14+
expect(isAda).to.be.a('function');
1615
});
1716

18-
it('doesn\'t have three items', function() {
19-
expect(myData.length).to.equal(3);
17+
it('doesn\'t have any params', function() {
18+
expect(isAda.length).to.equal(1);
2019
});
2120

22-
it('isn\'t the right data for Jane', function() {
23-
expect(myData).to.deep.equal([
24-
{ name: "Jane", class: "Computer Science", grade: "D" },
25-
{ name: "Jane", class: "Math", grade: "B" },
26-
{ name: "Jane", class: "Art", grade: "F" }
27-
]);
21+
it('doesn\'t return true when an items name matches "Ada Lovelace"', function() {
22+
var test = [{
23+
name: 'Jane'
24+
}, {
25+
name: 'Joe'
26+
}, {
27+
name: 'Ada Lovelace'
28+
}];
29+
expect(test.filter(isAda)).to.deep.equal([{
30+
name: "Ada Lovelace"
31+
}]);
2832
});
2933

3034
});

tutorial/1/01/02-filter.spec.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
21
var expect = require('chai').expect;
32
var context = require('test-context');
4-
context('../../../../../01-filter.js');
3+
var filePath = '../../../../../01-filter.js';
4+
context(filePath);
55

66
describe('var myData', function() {
77

88
it('doesn\'t exist', function() {
9-
expect(myFails).to.not.be.undefined;
9+
expect(myData).to.not.be.undefined;
1010
});
1111

12-
it('isn\'t an array', function() {
13-
expect(myFails).to.be.an('array');
12+
it('doesn\'t output an array', function() {
13+
expect(myData).to.be.an('array');
1414
});
1515

16-
it('doesn\'t have exactly two items', function() {
17-
expect(myFails.length).to.equal(2);
16+
it('doesn\'t output exactly ten items', function() {
17+
expect(myData).to.have.length(10);
1818
});
1919

20-
it('isn\'t the right data for Jane', function() {
21-
expect(myFails).to.deep.equal([
22-
{ name: "Jane", class: "Computer Science", grade: "D" },
23-
{ name: "Jane", class: "Art", grade: "F" }
24-
]);
20+
it('isn\'t the right filtered data for "Ada Lovelace"', function() {
21+
var values = [91, 88, 61, 81, 73, 58, 93, 82, 88, 65];
22+
myVals = myBest.map(function(x) {
23+
return x.score;
24+
});
25+
values.forEach(function(value) {
26+
expect(myVals).contains(value);
27+
});
2528
});
2629

2730
});

tutorial/1/01/03-filter.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var expect = require('chai').expect;
2+
var context = require('test-context');
3+
context('../../../../../01-filter.js');
4+
5+
describe('var myBest', function() {
6+
7+
it('doesn\'t exist', function() {
8+
expect(myBest).to.not.be.undefined;
9+
});
10+
11+
it('doesn\'t output an array', function() {
12+
expect(myBest).to.be.an('array');
13+
});
14+
15+
it('doesn\'t output exactly ten items', function() {
16+
expect(myBest).to.have.length(7);
17+
});
18+
19+
it('isn\'t the right filtered data for "Ada Lovelace"', function() {
20+
var values = [91, 88, 81, 73, 93, 82, 88];
21+
myBestVals = myBest.map(function(x) {
22+
return x.score;
23+
});
24+
values.forEach(function(value) {
25+
expect(myBestVals).contains(value);
26+
});
27+
});
28+
29+
});

tutorial/1/01/filter-sort.md

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

tutorial/1/01/filter.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
### Filter
2+
Array -> Array of items that match a condition
3+
4+
You've hacked into the school's computer system, and just in time. The grades are in, aren't you're not too proud of your performance. But that's okay, you have a plan: you're going to create a fake report card.
5+
6+
It would be great if you could `filter` the scores that your parents will see. A `filter` takes a matching condition function and only returns items that result in true. As an example, look at `isB` below:
7+
8+
```
9+
function isB(x) {
10+
return x === 'b';
11+
}
12+
```
13+
14+
15+
Like all of the methods in this chapter, `filter` is already part of the `Array.prototype`, so you can run it after any array. Each item is passed into the params of the condition function, one by one.
16+
17+
```
18+
var list = ['a', 'b', 'c'];
19+
list.filter(isB);
20+
//> ['b']
21+
```
22+
23+
24+
There's too much student data in the computer system. We'll have to sort through it. Have a look at an example below:
25+
26+
```
27+
console.log(studentData[0]);
28+
//> { course: 'Web Security',
29+
// instructor: 'Sue Denim',
30+
// name: 'Rebecca Heineman',
31+
// score: 93,
32+
// grade: 'A' }
33+
```
34+
35+
+ Write a filter condition function called `isAda` that returns true if param.`name` is true
36+
@test('1/01/01-filter')
37+
@action(open('01-filter.js'))
38+
@action(set(
39+
```
40+
function isAda(x) {
41+
// write condition here
42+
// the name must match "Ada Lovelace"
43+
}
44+
```))
45+
46+
+ Set `var myData` equal to data matching your name, "Ada Lovelace".
47+
@test('1/01/02-filter')
48+
@action(insert(
49+
```
50+
var data = require('./tutorial/data/students')(require('./tutorial/data/courses.json')).slice(0);
51+
```))
52+
@action(insert(
53+
```
54+
var myData = data.filter // call filter condition here
55+
```))
56+
57+
+ Set `var myBest` to your scores, excluding any grades that are "D" or "F".
58+
59+
@test('1/01/03-filter')
60+
@action(insert(
61+
```
62+
var myBest = myData.filter // filter out "D"'s and "F"'s here
63+
```))
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tutorial/1/03/04-map.spec.js

Whitespace-only 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.

tutorial/common/array.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,4 @@ Array.prototype.concatAll = function() {
66
});
77
});
88
return results;
9-
};
10-
11-
// Array.prototype.flatten = function() {
12-
// return this.reduce(function(a, b) {
13-
// if (Array.isArray(a)) {
14-
// return a.concat(b);
15-
// } else {
16-
// a.push(b);
17-
// }
18-
// });
19-
// };
9+
}

0 commit comments

Comments
 (0)