从compose函数中删除Javascript Reduce

从compose函数中删除Javascript Reduce,javascript,ecmascript-6,reduce,Javascript,Ecmascript 6,Reduce,我在一门在线课程上遇到了一个问题,我正试图向自己解释这个问题。我理解“compose”函数/想法在一种更简单的格式中的作用,但当涉及到将reduce应用于函数工厂并在每个函数上运行compose时,我似乎感到困惑。希望有经验的人能更清楚地向我解释“return fns.reduce(compose)”行的情况,以及它是如何一次一个地实现这些功能的?在底部调用函数后,每个函数是否依次为“累加器”和“当前”reduce const user = { user: 'Jennnifer',

我在一门在线课程上遇到了一个问题,我正试图向自己解释这个问题。我理解“compose”函数/想法在一种更简单的格式中的作用,但当涉及到将reduce应用于函数工厂并在每个函数上运行compose时,我似乎感到困惑。希望有经验的人能更清楚地向我解释“return fns.reduce(compose)”行的情况,以及它是如何一次一个地实现这些功能的?在底部调用函数后,每个函数是否依次为“累加器”和“当前”reduce

const user = {
    user: 'Jennnifer',
    active: true,
    cart: [],
    purchases: []
}


const compose = (f, g) => (...args) => f(g(...args));

function purchaseItem(...fns){
    return fns.reduce(compose)
    // return Object.assign({}, user, {purchases: item})
}

//Receives a user, and the item
function addItemToCart(user, item){
    //Create a variable and concat the empty cart of the user object to add the item to the cart key.
    const updateCart = user.cart.concat(item);
    return Object.assign({}, user, { cart: updateCart });
}

function applyTaxToItems(user){
    const { cart } = user;
    const taxRate = 1.3;
    const updatedCart = cart.map(item => {
        return {
            name: item.name,
            price: item.price * taxRate
        }
    })

    return Object.assign({}, user, { cart: updatedCart})
}

function buyItem(user){
    return user
}

function emptyCart(user){
    return user
}

//Invoke Function:

console.log(purchaseItem
    (
        emptyCart,
        buyItem,
        applyTaxToItems,
        addItemToCart
    )(user, { name: 'Laptop', price: 200})
)


你的函数名有点不对劲

基本上,您的
compose
函数仅用于两个函数

您的
purchaseItem
功能实际上没有购买任何东西。如果仔细观察,可以看到它只返回N个函数的组合

基本上,在
reduce
的每一步中,您都使用
累加器来组合当前的
函数-所有以前组合的函数

如果是第0步(前面没有
),那么它将组合前两个函数,并将其放入
累加器中

最好将其重命名为:

compose
->
compose2

purchaseItem
->
compose

然后在最后一个console.log()中

purchaseItem
    (
        emptyCart,
        buyItem,
        applyTaxToItems,
        addItemToCart
    )(user, { name: 'Laptop', price: 200})
为了更好地理解,你可以这样写

// composition of 2 functions
const compose2 = (f, g) => (...args) => f(g(...args))

// composition of N functions
const compose(...fns) => fns.reduce(compose2)

// applying composition to 4 functions
const purchaseItem = compose(
        emptyCart,
        buyItem,
        applyTaxToItems,
        addItemToCart
)

// calling 
purchaseItem(user, { name: 'Laptop', price: 200})


你的函数名有点不对劲

基本上,您的
compose
函数仅用于两个函数

您的
purchaseItem
功能实际上没有购买任何东西。如果仔细观察,可以看到它只返回N个函数的组合

基本上,在
reduce
的每一步中,您都使用
累加器来组合当前的
函数-所有以前组合的函数

如果是第0步(前面没有
),那么它将组合前两个函数,并将其放入
累加器中

最好将其重命名为:

compose
->
compose2

purchaseItem
->
compose

然后在最后一个console.log()中

purchaseItem
    (
        emptyCart,
        buyItem,
        applyTaxToItems,
        addItemToCart
    )(user, { name: 'Laptop', price: 200})
为了更好地理解,你可以这样写

// composition of 2 functions
const compose2 = (f, g) => (...args) => f(g(...args))

// composition of N functions
const compose(...fns) => fns.reduce(compose2)

// applying composition to 4 functions
const purchaseItem = compose(
        emptyCart,
        buyItem,
        applyTaxToItems,
        addItemToCart
)

// calling 
purchaseItem(user, { name: 'Laptop', price: 200})


上面的答案已经足够好了,但是为了更好地理解
compose
函数的
reduce
方法

//减少返回:(…args)=>prev(curr(…args));
//f':(…args)=>f(g(…args));
//f':(…args)=>f'(h(…args));(…args)=>f(g(h(…args)))
//f'':(…args)=>f''(j(…args));(…args)=>f(g(h(j)(…args)))
//f''':(…args)=>f''(k(…args));(…args)=>f(g(h)(j(k)(…argsЮЮ))
函数组合(f,g){
返回函数(…args){
返回f(g(…args))
}
}
函数组合(…fns){
return fns.reduce(compose)//返回函数为(…args)=>f(g(h(j(k(…args ')))))
}
函数f(x){返回x+'1'}
函数g(x){返回x+'2'}
函数h(x){返回x+'3'}
函数j(x){返回x+'4'}
函数k(x){返回x+'5'}
设结果=composell(k,j,h,g,f)('cho');
console.log(结果);//cho12345
前五行注释解释了
reduce
函数的返回值发生了什么变化

f':(…args)=>f'(h(…args));(…args)=>f(g(h(…args)))

表示
(…args)=>f'(h(…args))
(…args)=>f(g(h(…args)))

我把它们表示为
f'


您需要区分reduce回调函数,reduce回调函数中的
f,g
(第9行)与
composell
f
g
参数(第14行)

上述答案足够好,但为了更好地理解
reduce
方法的
compose
函数

//减少返回:(…args)=>prev(curr(…args));
//f':(…args)=>f(g(…args));
//f':(…args)=>f'(h(…args));(…args)=>f(g(h(…args)))
//f'':(…args)=>f''(j(…args));(…args)=>f(g(h(j)(…args)))
//f''':(…args)=>f''(k(…args));(…args)=>f(g(h)(j(k)(…argsЮЮ))
函数组合(f,g){
返回函数(…args){
返回f(g(…args))
}
}
函数组合(…fns){
return fns.reduce(compose)//返回函数为(…args)=>f(g(h(j(k(…args ')))))
}
函数f(x){返回x+'1'}
函数g(x){返回x+'2'}
函数h(x){返回x+'3'}
函数j(x){返回x+'4'}
函数k(x){返回x+'5'}
设结果=composell(k,j,h,g,f)('cho');
console.log(结果);//cho12345
前五行注释解释了
reduce
函数的返回值发生了什么变化

f':(…args)=>f'(h(…args));(…args)=>f(g(h(…args)))

表示
(…args)=>f'(h(…args))
(…args)=>f(g(h(…args)))

我把它们表示为
f'


您需要区分reduce回调函数,reduce回调函数中的
f,g
(第9行)与
composell
f
g
参数(第14行)

值不同。。我想知道为什么参数“f”是emptyCart函数,“g”代表其他函数?我从compose中记录了f和g,并在编辑器中看到了这一点。很神秘,很有趣。。我想知道为什么参数“f”是emptyCart函数,“g”代表其他函数?我从compose中记录了f和g,并在编辑器中看到了这一点。很神秘。