// 函数式编程
const _ = require('../lib/underScore.js')
// let lyrics = []
// for (let bottles = 10; bottles > 0; bottles--) {
// lyrics.push(bottles + '墙上有啤酒')
// lyrics.push(bottles + '多少瓶啤酒')
// // lyrics.push('take one down, pass it around')
// lyrics.push('拿一个下来,分给大家')
// if(bottles > 1) {
// lyrics.push((bottles - 1) + '墙上还有的啤酒')
// } else {
// lyrics.push('墙上没有啤酒了')
// }
// }
// console.log(lyrics)
// 抽象for循环内容
function LyricSegment(n) {
return _.chain([])
.push(n + '墙上有啤酒')
.push(n + '多少瓶啤酒')
.push('拿一个下来,分给大家')
.tap(function(lyrics) {
if(n > 1 ) {
lyrics.push((n - 1) + '墙上还有的啤酒')
} else {
lyrics.push('墙上没有啤酒了')
}
})
.value()
}
// 使用reduce取待for循环
function song(start, end, lyricsGen) {
let ary = _.range(start, end, -1)
return ary.reduce(function(acc, n) {
return acc.concat(lyricsGen(n))
}, [])
}
let l = song(10, 0, LyricSegment)
console.log(l)