Source code for `Choose Functional` article
This file contains 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
import getData from './data.js' | |
/* | |
* Sum. | |
* sum :: (Number, Number) → Number | |
* | |
* Provided two numbers, return their summation. | |
**/ | |
const sum = (numX, numY) => numX + numY | |
/* | |
* Increment. | |
* incr :: Number → Number | |
* | |
* Provided a number, return its value incremented by one. | |
**/ | |
const incr = num => sum(num, 1) | |
/* | |
* Construct bins. | |
* consBins :: Object → [[Number]] | |
* | |
* Configuration parameters: | |
* min - The numerical floor from which to initialize the first interval. | |
* max - The numerical ceiling by which we limit the last of intervals. | |
* width - The range encompassed by each interval. | |
**/ | |
const consBins = ({min = 0, max = Infinity, width = 1, accum = []}) => | |
// Ensure `max` does not exceed the range of our current interval... | |
sum(min, width) > max | |
? // If so, return our accumulator; | |
accum | |
: // Otherwise, call upon our method recursively until we fulfill our | |
// predicating condition. | |
consBins({ | |
// Increment interval, so as to preclude overlap. | |
min: incr(sum(min, width)), | |
// `max` and `width` remain constant. | |
max, | |
width, | |
// Wax upon our accumulator, combining it with a new array, in which the | |
// current interval is represented. | |
accum: accum.concat([[min, sum(min, width)]]), | |
}) | |
/* | |
* Filter property by minimum and maximum values. | |
* filterPropByMinMax :: Object → Array | |
* | |
* From a collection, return only those cases where the given property falls | |
* within range of specified minimum and maximum values. | |
* | |
* Configuration parameters: | |
* {String} key - The property to -- and by which to -- filter. | |
* {String} min - The minimum value by which to filter our attribute. | |
* {String} min - The maximum value by which to filter our attribute. | |
**/ | |
const filterPropByMinMax = (arr, {key = '', min = '0', max = '100'}) => | |
arr.filter( | |
// The numerical values encompassed by our JSON list assume the form of | |
// strings; hence, we need abstract their numerical values via `parseInt`. | |
// Our callback will return `true` provided the item's value is greater than | |
// or equal to our minimum _and_ less than or equal to our maximum. | |
item => parseInt(item[key], 10) >= min && parseInt(item[key], 10) <= max | |
) | |
/* | |
* Construct histogram object. | |
* consHistObj :: (Array, [[Number]]) → String → Object | |
**/ | |
const consHistObj = (arr = [], bins = []) => (key = '') => | |
// For each of the nested arrays within out `bins` list... | |
bins.reduce( | |
(accum, currVal) => ({ | |
// Build upon our accumulator, which we initialize as an empty object... | |
...accum, | |
// And where each entry is an object, whereof the the key shall represent | |
// the minimum and maximum values encompassed by each bin (e.g., `15,19`), | |
// and whereof the value shall delineate all vehicles encompassed by such | |
// bin. We retrieve such values by of `filterPropByMinMax`... | |
[currVal]: filterPropByMinMax(arr, { | |
key, | |
min: currVal[0], | |
max: currVal[1], | |
// And we calculate a total by way of `Array.prototype.length`. | |
}).length, | |
}), | |
{} | |
) | |
/* | |
* Construct histogram object provided bins. | |
* consHistObjWithBins :: (Object → [Object] → String) → Object | |
* | |
* Configuration parameters: | |
* {Number} min - The minimum value by which to filter our attribute. | |
* {Number} min - The maximum value by which to filter our attribute. | |
* {Number} width - The range encompassed by each interval. | |
**/ | |
const consHistObjWithBins = (arr = [], {min = 0, max = 100, width = 1}) => ( | |
key = '' | |
) => consHistObj(arr, consBins({min, max, width}))(key) | |
/* | |
* Construct mileage histogram. | |
**/ | |
const consMileageHist = (key = '') => | |
consHistObjWithBins(getData(), {min: 0, max: 50, width: 4})(key) | |
// Apply our helper function, specifying in each case the relevant keys. | |
const consCityMileageHist = () => consMileageHist('cityFuelEfficiency') | |
const consHighwayMileageHist = () => consMileageHist('highwayFuelEfficiency') | |
// Initialize the app | |
console.log('City mileage', consCityMileageHist()) | |
console.log('Highway mileage', consHighwayMileageHist()) |
This file contains 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
const getData = () => [ | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'SUV', | |
certified: 'true', | |
cityFuelEfficiency: '19', | |
driveLine: 'FWD', | |
engine: 'V-6 cyl', | |
exteriorColor: 'Crystal Black Pearl', | |
features: [], | |
fuelType: 'Premium Unleaded', | |
highwayFuelEfficiency: '27', | |
interiorColor: 'Graystone', | |
internetPrice: '36500', | |
inventoryType: 'all', | |
make: 'Acura', | |
model: 'MDX', | |
modelCode: null, | |
modelYear: '2016', | |
msrp: '39975', | |
newOrUsed: 'used', | |
odometer: '15683', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRT26599', | |
transmission: '9-Speed Automatic', | |
trim: '3.5L', | |
uuid: '76e6301d0a0e0ae757ee4a8214e64e3e', | |
vin: '5FRYD3H27GB003986', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'SUV', | |
certified: 'true', | |
cityFuelEfficiency: '19', | |
driveLine: 'AWD', | |
engine: 'V-6 cyl', | |
exteriorColor: 'Kona Coffee', | |
features: [], | |
fuelType: 'Premium Unleaded', | |
highwayFuelEfficiency: '27', | |
interiorColor: 'Parchment', | |
internetPrice: '23200', | |
inventoryType: 'all', | |
make: 'Acura', | |
model: 'RDX', | |
modelCode: 'TB4H3FJNW', | |
modelYear: '2015', | |
msrp: '26975', | |
newOrUsed: 'used', | |
odometer: '41042', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRT26325', | |
transmission: '6-Speed Automatic', | |
trim: 'Base', | |
uuid: 'd39bb6200a0e0ae965f9dd8451a9d3c0', | |
vin: '5J8TB4H34FL014179', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'Sedan', | |
certified: 'true', | |
cityFuelEfficiency: '20', | |
driveLine: 'Front-wheel Drive', | |
engine: 'V-6 cyl', | |
exteriorColor: 'Forged Silver Metallic', | |
features: [], | |
fuelType: 'Premium Unleaded', | |
highwayFuelEfficiency: '31', | |
interiorColor: 'Ebony', | |
internetPrice: '25000', | |
inventoryType: 'all', | |
make: 'Acura', | |
model: 'RLX', | |
modelCode: 'KC1F9EKNW', | |
modelYear: '2014', | |
msrp: '29975', | |
newOrUsed: 'used', | |
odometer: '24145', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRC30840', | |
transmission: '6-Speed Automatic', | |
trim: 'Base', | |
uuid: 'a950c22f0a0e0ae7682674b86f74b2f9', | |
vin: 'JH4KC1F90EC000290', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'Sedan', | |
certified: 'true', | |
cityFuelEfficiency: '24', | |
driveLine: 'FWD', | |
engine: 'I-4 cyl', | |
exteriorColor: 'Fathom Blue Pearl', | |
features: [], | |
fuelType: 'Premium Unleaded', | |
highwayFuelEfficiency: '35', | |
interiorColor: 'Graystone', | |
internetPrice: '22189', | |
inventoryType: 'all', | |
make: 'Acura', | |
model: 'TLX', | |
modelCode: 'UB1F3FJW', | |
modelYear: '2015', | |
msrp: '25975', | |
newOrUsed: 'used', | |
odometer: '10740', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRC30880', | |
transmission: '8-Speed Dual-Clutch', | |
trim: '2.4L', | |
uuid: 'b39c6df10a0e0ae965f9dd8410212be7', | |
vin: '19UUB1F35FA003225', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'Sedan', | |
certified: 'true', | |
cityFuelEfficiency: '21', | |
driveLine: 'AWD', | |
engine: 'V-6 cyl', | |
exteriorColor: 'Basque Red Pearl II', | |
features: [], | |
fuelType: 'Premium Unleaded', | |
highwayFuelEfficiency: '31', | |
interiorColor: 'Parchment', | |
internetPrice: '22959', | |
inventoryType: 'all', | |
make: 'Acura', | |
model: 'TLX', | |
modelCode: 'UB3F5FKNW', | |
modelYear: '2015', | |
msrp: '26475', | |
newOrUsed: 'used', | |
odometer: '48819', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRC30720', | |
transmission: '9-Speed Automatic', | |
trim: '3.5L V6', | |
uuid: '428c29ec0a0e0adf5dc3189615a52f77', | |
vin: '19UUB3F55FA002184', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'Sedan', | |
certified: 'true', | |
cityFuelEfficiency: '25', | |
driveLine: 'FrontTrak', | |
engine: 'TFSI four-cylinder engine', | |
exteriorColor: 'Moonlight blue metallic', | |
features: [], | |
fuelType: 'Gas', | |
highwayFuelEfficiency: '33', | |
interiorColor: 'Nougat brown / Black', | |
internetPrice: '25800', | |
inventoryType: 'all', | |
make: 'Audi', | |
model: 'A4', | |
modelCode: '8W25NG', | |
modelYear: '2017', | |
msrp: '31475', | |
newOrUsed: 'used', | |
odometer: '26873', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRC30907', | |
transmission: '7-Speed Automatic S tronic', | |
trim: '2.0T Premium', | |
uuid: 'c835333a0a0e0aea4a9cbb01accadf43', | |
vin: 'WAUGNAF45HN015721', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'Sedan', | |
certified: 'false', | |
cityFuelEfficiency: '18', | |
driveLine: 'quattro', | |
engine: 'V-6 cyl', | |
exteriorColor: 'Brilliant Black', | |
features: [], | |
fuelType: 'Premium Unleaded', | |
highwayFuelEfficiency: '26', | |
interiorColor: 'Amaretto/Black', | |
internetPrice: '14399', | |
inventoryType: 'all', | |
make: 'Audi', | |
model: 'A6', | |
modelCode: '4F25VL', | |
modelYear: '2010', | |
msrp: '15475', | |
newOrUsed: 'used', | |
odometer: '86220', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRC31210', | |
transmission: '6-Speed Automatic with Tiptronic', | |
trim: '3.0 Premium', | |
uuid: 'a6cbc0390a0e0ae804ee321d0ee3786f', | |
vin: 'WAUFGAFBXAN050040', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'SUV', | |
certified: 'false', | |
cityFuelEfficiency: '20', | |
driveLine: 'quattro', | |
engine: 'TFSI four-cylinder engine', | |
exteriorColor: 'Brilliant black', | |
features: [], | |
fuelType: 'Gas', | |
highwayFuelEfficiency: '28', | |
interiorColor: 'Black', | |
internetPrice: '0', | |
inventoryType: 'all', | |
make: 'Audi', | |
model: 'Q5', | |
modelCode: '8RB52A', | |
modelYear: '2014', | |
msrp: '0', | |
newOrUsed: 'used', | |
odometer: '37245', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRT26697', | |
transmission: '8-Speed Automatic', | |
trim: '2.0T Premium', | |
uuid: 'c48ceb640a0e0a1748743f969666e5d0', | |
vin: 'WA1CFAFP4EA087547', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'Sedan', | |
certified: 'true', | |
cityFuelEfficiency: '23', | |
driveLine: 'RWD', | |
engine: 'I-4 cyl', | |
exteriorColor: 'Black Sapphire Metallic', | |
features: [], | |
fuelType: 'Premium Unleaded', | |
highwayFuelEfficiency: '35', | |
interiorColor: 'Black', | |
internetPrice: '26200', | |
inventoryType: 'all', | |
make: 'BMW', | |
model: '328i', | |
modelCode: '163O', | |
modelYear: '2016', | |
msrp: '32975', | |
newOrUsed: 'used', | |
odometer: '28587', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRC30904', | |
transmission: '8-Speed Automatic', | |
trim: '328i', | |
uuid: 'c83531820a0d04fe7162913b9a1b8bf8', | |
vin: 'WBA8E9C53GK646953', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'Sedan', | |
certified: 'false', | |
cityFuelEfficiency: '22', | |
driveLine: 'AWD', | |
engine: 'I-4 cyl', | |
exteriorColor: 'Brown', | |
features: [], | |
fuelType: 'Premium Unleaded', | |
highwayFuelEfficiency: '33', | |
interiorColor: null, | |
internetPrice: '0', | |
inventoryType: 'all', | |
make: 'BMW', | |
model: '528i xDrive', | |
modelCode: '145B', | |
modelYear: '2014', | |
msrp: '0', | |
newOrUsed: 'used', | |
odometer: '31490', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRC31261', | |
transmission: '8-Speed Automatic', | |
trim: '528i Xdrive', | |
uuid: 'c48ce9870a0d0cc73abae2a8e4367247', | |
vin: 'WBA5A7C57ED620375', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'Sedan', | |
certified: 'true', | |
cityFuelEfficiency: '22', | |
driveLine: 'AWD', | |
engine: 'I-4 cyl', | |
exteriorColor: 'Dark Graphite II', | |
features: [], | |
fuelType: 'Premium Unleaded', | |
highwayFuelEfficiency: '32', | |
interiorColor: 'Black', | |
internetPrice: '17000', | |
inventoryType: 'all', | |
make: 'BMW', | |
model: '528i xDrive', | |
modelCode: '125B', | |
modelYear: '2012', | |
msrp: '21975', | |
newOrUsed: 'used', | |
odometer: '54065', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRT25876A', | |
transmission: '8-Speed Automatic', | |
trim: '528i Xdrive', | |
uuid: '160531990a0d0c1477af376f5345886f', | |
vin: 'WBAXH5C59CDW05768', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'SUV', | |
certified: 'true', | |
cityFuelEfficiency: '21', | |
driveLine: 'AWD', | |
engine: 'I-4 cyl', | |
exteriorColor: 'Alpine White', | |
features: [], | |
fuelType: 'Premium Unleaded', | |
highwayFuelEfficiency: '28', | |
interiorColor: 'Black', | |
internetPrice: '23997', | |
inventoryType: 'all', | |
make: 'BMW', | |
model: 'X3 xDrive28i', | |
modelCode: '14XD', | |
modelYear: '2014', | |
msrp: '26975', | |
newOrUsed: 'used', | |
odometer: '53864', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRT26245', | |
transmission: '8-Speed Automatic Steptronic', | |
trim: 'Xdrive28i', | |
uuid: 'ae776bb60a0d0cc742fbd3bf8085527e', | |
vin: '5UXWX9C57E0D27716', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'SUV', | |
certified: 'true', | |
cityFuelEfficiency: '15', | |
driveLine: 'AWD', | |
engine: 'V-6 cyl', | |
exteriorColor: 'Crimson Red Tintcoat', | |
features: [], | |
fuelType: 'Regular Unleaded', | |
highwayFuelEfficiency: '22', | |
interiorColor: 'Ebony', | |
internetPrice: '32499', | |
inventoryType: 'all', | |
make: 'Buick', | |
model: 'Enclave', | |
modelCode: '4V14526', | |
modelYear: '2016', | |
msrp: '34475', | |
newOrUsed: 'used', | |
odometer: '17205', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'HTH1673A', | |
transmission: '6-Speed Automatic Electronic with Overdrive', | |
trim: 'Leather Group', | |
uuid: 'bf6c580c0a0e0ae70e749f9f3f9ca075', | |
vin: '5GAKVBKD4GJ260011', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'SUV', | |
certified: 'true', | |
cityFuelEfficiency: '16', | |
driveLine: 'AWD', | |
engine: 'V-6 cyl', | |
exteriorColor: 'Iridium', | |
features: [], | |
fuelType: 'Regular Unleaded', | |
highwayFuelEfficiency: '22', | |
interiorColor: 'Ebony', | |
internetPrice: '25000', | |
inventoryType: 'all', | |
make: 'Buick', | |
model: 'Enclave', | |
modelCode: '4V14526', | |
modelYear: '2015', | |
msrp: '27975', | |
newOrUsed: 'used', | |
odometer: '49983', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRT26636', | |
transmission: '6-Speed Automatic Electronic with Overdrive', | |
trim: 'Leather Group', | |
uuid: '87a7a8a50a0e0ae8145e712d37eb13c1', | |
vin: '5GAKVBKD3FJ171464', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'SUV', | |
certified: 'true', | |
cityFuelEfficiency: '16', | |
driveLine: 'AWD', | |
engine: 'V-6 cyl', | |
exteriorColor: 'Carbon Black Metallic', | |
features: [], | |
fuelType: 'Regular Unleaded', | |
highwayFuelEfficiency: '22', | |
interiorColor: 'Choccahino', | |
internetPrice: '24300', | |
inventoryType: 'all', | |
make: 'Buick', | |
model: 'Enclave', | |
modelCode: '4V14526', | |
modelYear: '2014', | |
msrp: '29475', | |
newOrUsed: 'used', | |
odometer: '44320', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRT26346', | |
transmission: '6-Speed Automatic', | |
trim: 'Leather Group', | |
uuid: 'e30ceceb0a0e0a6b13730521e203a86b', | |
vin: '5GAKVBKD8EJ273762', | |
}, | |
{ | |
accountId: 'ricartpreowned', | |
bodyStyle: 'SUV', | |
certified: 'true', | |
cityFuelEfficiency: '16', | |
driveLine: 'AWD', | |
engine: 'V-6 cyl', | |
exteriorColor: 'Atlantis Blue Metallic', | |
features: [], | |
fuelType: 'Regular Unleaded', | |
highwayFuelEfficiency: '22', | |
interiorColor: 'Ebony', | |
internetPrice: '25000', | |
inventoryType: 'all', | |
make: 'Buick', | |
model: 'Enclave', | |
modelCode: '4V14526', | |
modelYear: '2014', | |
msrp: '29725', | |
newOrUsed: 'used', | |
odometer: '33709', | |
optionCodes: null, | |
packages: [], | |
status: 'Live', | |
stockNumber: 'PRT26012', | |
transmission: '6-Speed Automatic', | |
trim: 'Leather Group', | |
uuid: '2d5201e20a0e0aea4a9cbb0140681074', | |
vin: '5GAKVBKD2EJ239865', | |
}, | |
] | |
export default getData |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment