函數式編程 filter的使用 reduce curry let dragon = (name,size,element) = console.log(dragon('fluffykins','tiny','lightling')) //curry let dragon = name = size ...
函數式編程
filter的使用
var animals = [
{name:'Fluffykins',species:'rabbit'},
{name:'Caro',species:'dog'},
{name:'Hamilton',species:'dog'},
{name:'Harold',species:'fish'},
{name:'Ursula',species:'cat'},
{name:'Jimmy',species:'fish'}
]
//第一種做法
var dogs = []
for (var i = 0; i < animals.length; i++){
if(animals[i].species === 'dog'){
dog.push(animals[i])
}
}
//第二種做法
var dogs = animals.filter(function(animal){
return animal.species === 'dog'
})
//優化
let isDog = unction(animal){
return animal.species === 'dog'
}
var dogs = animals.filter(isDog)
map的使用
var animals = [
{name:'Fluffykins',species:'rabbit'},
{name:'Caro',species:'dog'},
{name:'Hamilton',species:'dog'},
{name:'Harold',species:'fish'},
{name:'Ursula',species:'cat'},
{name:'Jimmy',species:'fish'}
]
//第一種做法
var names = []
for ( var i = 0 ; i < animals.length; i++){
names.push(animals[i].name)
}
//第二種做法
var names = animals.map(function(animal){
return animal.name
})
//優化
var names = animals.map((animal) => animal.name)
reduce
var orders = [
{amount:250},
{amount:400},
{amount:100},
{amount:325}
]
//第一種做法
var totalAmount = 0
for ( var i = 0; i < orders.length; i++){
totalAmount = orders[i].amount
}
//第二種做法
var totalAmount = orders.reduce(function(sum,order){
return sum + order.amount
},0)
//優化
var totalAmount = orders.reduce((sum,order) => sum + order.amount
},0)
curry
let dragon = (name,size,element) =>
`${name} is a ${size} dragon that breathes ${element} !`
console.log(dragon('fluffykins','tiny','lightling'))
//curry
let dragon =
name =>
size =>
element =>
`${name} is a ${size} dragon that breathes ${element} !`
console.log(dragon('fluffykins')('tiny')('lightling'))
//還可以
let dragon =
name =>
size =>
element =>
`${name} is a ${size} dragon that breathes ${element} !`
let fluffykinDragon = dragon('fluffykins')
console.log(fluffykinDragon('tiny')('lightling'))
//甚至是這樣
let dragon =
name =>
size =>
element =>
`${name} is a ${size} dragon that breathes ${element} !`
let fluffykinDragon = dragon('fluffykins')
let tinyDragon = fluffykinDragon('tiny')
console.log(tinyDragon('lightling'))
//使用lodash
import _ from 'lodash'
let dragon =(name,size,element) => (
`${name} is a ${size} dragon that breathes ${element} !`
)
dragon = _.curry(dragon)
let fluffykinDragon = dragon('fluffykins')
let tinyDragon = fluffykinDragon('tiny')
console.log(tinyDragon('lightling'))
let dragons = [
{name:'fluffykins',element:'lightning'},
{name:'nomi',element:'lightning'},
{name:'karo',element:'fire'},
{name:'doomer',element:'timewrap'},
]
let hasElement = (element,obj) => obj.element === element
let lightingDragons = dragons.filter(x => hasElement('lightning',x)
console.log(lightingDragons)
//使用loadash_curry改造
import _ from 'lodash'
let hasElement = _.curry(element,obj) => obj.element === element
let lightingDragons = dragons.filter(hasElement('lightning'))
console.log(lightingDragons)
遞歸
let categories = [
{
id:'animals',
'parent':null
},
{
id:'mammals',
'parent':'animals'
},
{
id:'cats',
'parent':'mammals'
},
{
id:'dogs',
'parent':'mammals'
},
{
id:'chihuahua',
'parent':'dogs'
},
{
id:'labrador',
'parent':'dogs'
},
{
id:'persian',
'parent':'cats'
},
{
id:'siamese',
'parent':'cats'
}
]
let makeTree = (categories,parent) => {
let node = {
}
categories.filter(c => c.parent === parent)
.forEach(c =>
node[c.id] = makeTree(categories,c.id))
return node
}
console.log(
JSON.stringify(
makeTree(categories,null)
,null
,2
)
)