Last active
July 15, 2017 00:10
-
-
Save starsinmypockets/abb6b23557aa3080562330e416529f71 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Example Data | |
| var 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); | |
| }; | |
| console.log('X', _.prop('in_stock')); | |
| const myIsLastInStock = _.compose(_.prop('in_stock'), _.last); | |
| const myIsFirstInStock = _.compose(_.prop('in_stock'), _.first); | |
| console.log('In stock:', myIsLastInStock(CARS), myIsFirstInStock(CARS)); | |
| // Exercise 2: | |
| // ============ | |
| // Use _.compose(), _.prop() and _.head() to retrieve the name of the first car. | |
| const nameOfFirstCar = _.compose(_.prop('name'), _.first); | |
| console.log(nameOfFirstCar(CARS)); | |
| // 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); | |
| }; | |
| const myAvgDolVar = _.compose(_average, _.map(_.prop('dollar_value'))); | |
| console.log(myAvgDolVar(CARS)); | |
| // 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 | |
| const toLowerCase = str => str.toLowerCase(); | |
| var sanitizeNames = _.map(_.compose(_underscore, toLowerCase, _.prop('name'))); | |
| console.log(sanitizeNames(CARS)); | |
| // 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(', '); | |
| }; | |
| const toDollarAmt = n => '$' + Number(n).toFixed(2); // this is a stand in for lib code | |
| const myAvailablePrices = _.compose(_.map(_.compose(toDollarAmt, _.prop('dollar_value'))), _.filter('in_stock')); | |
| console.log(myAvailablePrices(CARS)); | |
| // 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'; | |
| }; | |
| const append = str => str + ' is the fastest'; | |
| const myFastestCar = _.compose(append, _.prop('name'), _.last, _.sortBy('horsepower')); | |
| console.log(myFastestCar(CARS)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment