const fp = require('lodash/fp')
// 数据
// horsepower 马力,dollar_value 价格,in_stock 库存
const 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}
]
let isLastInStock = function(cars) {
// 获取最后一条数据
let last_car = fp.last(cars)
// 获取最后一条数据的 in_stock 属性值
return fp.prop('in_stock', last_car)
}
解答:
const lastInStock = fp.flowRight(fp.prop('in_stock'),fp.last)
log(lastInStock(cars))
解答:
const getFirstCarName = fp.flowRight(fp.prop('name'),fp.first)
log(getFirstCarName(cars))
//fp.first可替换为fp.head
let _average = function(xs) { return fp.reduce(fp.add, 0, xs) / xs.length } // <- 无须改动
let averageDollarValue = function (cars) {
let dollar_values = fp.map(function(car) { return car.dollar_value }, cars)
return _average(dollar_values)
}
解答:
let averageDollarValue = fp.flowRight(_average,fp.map(car=>car.dollar_value))
log(averageDollarValue(cars))
//第二种写法
//let averageDollorValue = fp.flowRight(_average, fp.map(fp.curry(fp.props)('dollar_value')))
//console.log(averageDollorValue(cars))
第三种写法
function compose(f,g){
return function(value){
return f(g(value))
}
}
let _average = function(xs){
fp.reduce(fp.add,0,xs) / xs.length
}
let averageDollarValue = function(cars){
let dollar_values = fp.map(function(car){
return car.dollar_value
},cars)
return dollar_values
}
let B = compose(_average,averageDollarValue)
返回一个下划线连接的小写字符串,把数组中的 name 转换为这种形式:例如:sanitizeNames([“Hello World”]) => [“hello_world”]
let _underscore = fp.replace(/\W+/g, '_') // <-- 无须改动,并在 sanitizeNames 中使用它
解答:
let sanitizeNames = fp.flowRight(fp.map(_underscore), fp.map(car => car.name))
console.log(sanitizeNames(CARS))
//第二种写法
const sanitizeNames = fp.map(car=>{
car.name = fp.flowRight(_underscore,fp.toLower)(car.name)
return car
})
log(sanitizeNames(cars))
//第三种写法
//let sanitizeNames = fp.flowRight(fp.map(fp.flowRight(fp.toLower, _underscore)))
//console.log(sanitizeNames(['Hello World'])) // [ 'hello_world' ]
//第四种写法
let _underscore = fp.replace(/\W+/g,'_')
var sanitizeNames = fp.map(item => (
{
...item,
name: fp.flowRight(_underscore, fp.toLower)(item.name)
}
))
console.log(sanitizeNames(CARS))
//第五种写法
const sanitizeNames = fp.map(
fp.flowRight(_underscore,fp.toLower,fp.prop('name'))
)
console.log(sanitizeNames(CARS))
////第六种写法,不使用flowRight
const sanitizeNames = fp.map(
fp.compose(
_underscore,
fp.toLower,
fp.prop('name')
)
)
console.log(sanitizeNames(CARS))
// support.js
class Container {
static of (value) {
return new Container(value)
}
constructor (value) {
this._value = value
}
map (fn) {
return Container.of(fn(this._value))
}
}
class Maybe {
static of (x) {
return new Maybe(x)
}
isNothing () {
return this._value === null || this._value === undefined
}
constructor (x) {
this._value = x
}
map (fn) {
return this.isNothing ? this : Maybe.of(fn(this._value))
}
}
module.exports = {
Maybe,
Container
}
const fp = require('lodash/fp')
const { Maybe, Container } = require('./support')
let maybe = Maybe.of([5, 6, 1])
let ex1 = undefined
解答:
let ex1 = maybe.map(x => fp.map(fp.add(1), x))
console.log(ex1)
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
let ex2 = undefined
解答:
let ex2 = fp.map(fp.first)
console.log(xs.map(ex2))
第二种写法
let ex2 = ()=> xs.map(fp.first)._value
第三种写法
let ex2 = xs.map(x => fp.first(x) )
log(ex2())
let safeProp = fp.curry(function (x, o) { return Maybe.of(o[x]) })
let user = { id: 2, name: "Albert" }
let ex3 = undefined
解答:
//第一种写法
let ex3 = fp.flowRight(fp.map(fp.first), safeProp('name'))
console.log(ex3(user))
//第二种写法
let ex3 = safeProp('name',user).map(x => fp.first(x))
console.log(ex3(user))
let ex4 = function (n) {
if (n) { return parseInt(n) }
}
解答:
//第一种写法
let ex4 = fp.flowRight(fp.map(parseInt), Maybe.of)
// log(ex4(1)) // 1
// log(ex4('7')) // 7
// log(ex4(null)) // undefined
// log(ex4('null')) // NaN
// log(ex4('Abc')) // NaN
// log(ex4(undefined)) // undefined
// log(ex4({obj:'hhh'})) // NaN
---------------------------------------------------
//第二种写法
let ex4 = n=>Maybe.of(n).map(parseInt)
console.log(ex4)