-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples.js
More file actions
68 lines (52 loc) · 1.71 KB
/
examples.js
File metadata and controls
68 lines (52 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// examples.js - functional programming examples for UpFront-Underscore
// discussion materials. This content is free to use and distribute.
//
// Author: Jerrod Long, 5/1/2012
//
// https://github.com/UpFront-Underscore/functional-programming-examples
var underscore = _.noConflict();
var Follower = function(userData){
if (!(this instanceof Follower)) {
return new Follower(userData);
}
this.name = userData.name;
this.location = userData.location;
this.friendCount = userData.friends_count;
this.followerCount = userData.followers_count;
}
var example = function(_){
var publicAccessors = {};
publicAccessors.followers = function(){
return _.map(mockUserData, Follower)
}
publicAccessors.followerNames = function(){
return _.pluck(example.followers(), 'name');
}
publicAccessors.followerLocations = function(){
return _.pluck(example.followers(), 'location');
}
publicAccessors.mostFollowers = function(){
return _.max(example.followers(), function(follower){
return follower.followerCount;
});
}
publicAccessors.followersByLocation = function(){
return _.groupBy(example.followers(), 'location');
}
publicAccessors.followersByLocationConsolidated = function(){
return _.groupBy(example.followers(), function(follower){
return follower.location.indexOf("Kansas City")?
follower.location : "Kansas City";
});
}
publicAccessors.friendsOfFollowers = function(){
return _.reduce(example.followers(), function(total, follower){
return total + follower.friendCount;
}, 0);
}
publicAccessors.print = function(string){
console.log(string);
}
return publicAccessors;
}(underscore);