-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter5.js
More file actions
171 lines (131 loc) · 4.54 KB
/
Copy pathchapter5.js
File metadata and controls
171 lines (131 loc) · 4.54 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"use strict";
let _ = require("Ramda")
let compose = (a, b) => (n) => a(b(n));
let toUpperCase = str => str.toUpperCase();
let exclaim = str => str + '!';
let shout = compose(toUpperCase, exclaim);
let head = a => a[0];
let reverse = _.reduce((acc, x) => [x].concat(acc), []);
let reverse2 = (ary) => {
let newArray = [].concat(ary);
for (let i = 0; i < ary.length / 2; i++) {
[newArray[i], newArray[newArray.length - 1 - i]] = [newArray[newArray.length - 1 - i], newArray[i]];
}
return newArray;
}
let words = ['jumpkick', 'roundhouse', 'uppercut'];
let loudLast = compose(shout, compose(head, reverse2));
let loudLast2 = _.compose(head, shout, reverse2);
// Pointfree
let snakeCase = function (word) {
return word.toLowerCase().replace(/\s+/ig, '_');
};
let replace = _.curry((regexp, what, str) => str.replace(regexp, what));
let replaceSpaces = replace(/\s+/ig);
let replaceSpacesWithSnakes = replaceSpaces("_");
let snakeCasePointfree = compose(replaceSpacesWithSnakes, toUpperCase);
// Debugging
const angry = _.compose(exclaim, toUpperCase);
const map = _.curry((f, ary) => ary.map(f))
const join = _.curry((str, ary) => ary.join(str));
let latin = _.compose(map(angry), reverse);
const trace = _.curry((tag, x) => {
console.log(tag, x);
return x;
})
const dasherize = _.compose(_.join("-"), trace("after map to lower"), _.map(_.toLower), trace("after split"), _.split(" "), trace("after replace"), _.replace(/\s{2,}/ig, " "));
const g = x => x.length;
const f = x => x === 4;
const id = (x) => x;
const isFourLetterWord = _.compose(f, g)
let accounting = require('accounting');
// Example Data
let CARS = [{
name: 'Ferrari FF',
horsepower: 660,
dollar_value: 700000,
in_stock: true,
}, {
name: 'Spyker C12 Zagato',
horsepower: 650,
dollar_value: 648000,
in_stock: false,
}, {
name: 'Jaguar XKR-S',
horsepower: 550,
dollar_value: 132000,
in_stock: false,
}, {
name: 'Audi R8',
horsepower: 525,
dollar_value: 114200,
in_stock: false,
}, {
name: 'Aston Martin One-77',
horsepower: 750,
dollar_value: 1850000,
in_stock: true,
}, {
name: 'Pagani Huayra',
horsepower: 700,
dollar_value: 1300000,
in_stock: false,
}];
// Exercise 1:
// ============
// Use _.compose() to rewrite the function below. Hint: _.prop() is curried.
var isLastInStock = function(cars) {
var last_car = _.last(cars);
return _.prop('in_stock', last_car);
};
isLastInStock = _.compose(_.prop('in_stock'), _.last);
// Exercise 2:
// ============
// Use _.compose(), _.prop() and _.head() to retrieve the name of the first car.
var nameOfFirstCar = undefined;
nameOfFirstCar = _.compose(_.prop("name"), _.head);
// Exercise 3:
// ============
// Use the helper function _average to refactor averageDollarValue as a composition.
var _average = function(xs) {
return _.reduce(_.add, 0, xs) / xs.length;
}; // <- leave be
var averageDollarValue = function(cars) {
var dollar_values = _.map(function(c) {
return c.dollar_value;
}, cars);
return _average(dollar_values);
};
averageDollarValue = _.compose(_average, _.map(_.prop('dollar_value')))
// Exercise 4:
// ============
// Write a function: sanitizeNames() using compose that returns a list of lowercase and underscored car's names: e.g: sanitizeNames([{name: 'Ferrari FF', horsepower: 660, dollar_value: 700000, in_stock: true}]) //=> ['ferrari_ff'].
var _underscore = _.replace(/\W+/g, '_'); //<-- leave this alone and use to sanitize
var sanitizeNames = _.map(_.compose(_underscore, _.toLower, _.prop('name')));
// Bonus 1:
// ============
// Refactor availablePrices with compose.
var availablePrices = function(cars) {
var available_cars = _.filter(_.prop('in_stock'), cars);
return available_cars.map(function(x) {
return accounting.formatMoney(x.dollar_value);
}).join(', ');
};
availablePrices = _.compose(_.join(", "), _.map(_.compose (accounting.formatMoney, _.prop("dollar_value"))), _.filter(_.prop('in_stock')))
// Bonus 2:
// ============
// Refactor to pointfree. Hint: you can use _.flip().
var fastestCar = function(cars) {
var sorted = _.sortBy(function(car) {
return car.horsepower;
}, cars);
var fastest = _.last(sorted);
return fastest.name + ' is the fastest';
};
console.log(fastestCar(CARS))
let append = _.flip(_.concat)
let appendSign = append("$");
console.log(appendSign("135"))
console.log((_.concat("$", "135")))
fastestCar = _.compose(append(' is the fastest'), _.prop("name"), _.last, _.sortBy(_.prop("horsepower")))
console.log(fastestCar(CARS))