Created
September 9, 2015 08:03
-
-
Save darul75/c40433a0a78266061a2d 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 = R.last(cars); | |
return _.prop('in_stock', last_car); | |
}; | |
isLastInStock = R.compose(R.prop('in_stock'), R.last); | |
// Exercise 2: | |
// ============ | |
// use _.compose(), _.prop() and _.head() to retrieve the name of the first car | |
var nameOfFirstCar = R.compose(R.prop('name'), R.head); | |
var result = nameOfFirstCar(CARS); | |
console.log(result); | |
// Exercise 3: | |
// ============ | |
// Use the helper function _average to refactor averageDollarValue as a composition | |
var _average = function(xs) { return R.reduce(R.add, 0, xs) / xs.length; }; // <- leave be | |
var averageDollarValue = function(cars) { | |
var dollar_values = R.map(function(c) { return c.dollar_value; }, cars); | |
return _average(dollar_values); | |
}; | |
averageDollarValue = R.compose(_average, R.map(R.prop('dollar_value'))); | |
averageDollarValue(CARS); | |
var toLowerCase = R.curry(function(what) { | |
return what.toLowerCase(); | |
}); | |
// Exercise 4: | |
// ============ | |
// Write a function: sanitizeNames() using compose that returns a list of lowercase and underscored names: e.g: sanitizeNames(["Hello World"]) //=> ["hello_world"]. | |
var _underscore = R.replace(/\W+/g, '_'); //<-- leave this alone and use to sanitize | |
var sanitizeNames = R.map(R.compose(_underscore, toLowerCase, R.prop('name'))); | |
sanitizeNames([{name: "Hello World", horsepower: 660, dollar_value: 700000, in_stock: true}]); | |
// Bonus 1: | |
// ============ | |
// Refactor availablePrices with compose. | |
var formatMoney = function(x) { | |
return x+100; | |
}; | |
var availablePrices = function(cars) { | |
var available_cars = R.filter(_.prop('in_stock'), cars); | |
return available_cars.map(function(x){ | |
return x.dollar_value + 50; | |
}).join(', '); | |
}; | |
availablePrices = R.compose(R.join(','), R.map(R.compose(formatMoney, R.prop('dollar_value'))), R.filter(R.prop('in_stock'))); | |
// Bonus 2: | |
// ============ | |
// Refactor to pointfree. Hint: you can use _.flip() | |
var fastestCar = function(cars) { | |
var sorted = R.sortBy(function(car){ return car.horsepower }, cars); | |
var fastest = R.last(sorted); | |
return fastest.name + ' is the fastest'; | |
}; | |
var sortBy = R.sortBy(R.prop('horsepower')); | |
var fasted = R.last; | |
fastedCar = R.compose(R.prop('name'), R.compose(fasted, sortBy)); | |
fastedCar(CARS) + ' is the fastest'; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment