我想學習如何在JavaScript中使用應用函子,並遇到了ap
方法。我想用它來三個陣列相結合,像這樣:我的應用函子不適用於Ramda的ap?
const products = ['teeshirt', 'sweater']
const options = ['large', 'medium', 'small']
const colors = ['red', 'black']
所以按照該documentation我嘗試了這一點:
const buildMerch = product => option => color =>`${product}-${option}-${color}`
const merchandise = R.ap([buildMerch], [products, options, colors])
但是,這是給我回三個功能:
[function (option) {
return function (color) {
return product + '-' + option + '-' + color;
};
}, function (option) {
return function (color) {
return product + '-' + option + '-' + color;
};
}, function (option) {
return function (color) {
return product + '-' + option + '-' + color;
};
}]
...而不是我期望的陣列的組合結果:
["teeshirt- large-red", "teeshirt- large-black", "teeshirt- medium-red", "teeshirt- medium-black", "teeshirt- small-red", "teeshirt- small-black", "sweater- large-red", "sweater- large-black", "sweater- medium-red", "sweater- medium-black", "sweater- small-red", "sweater- small-black"]
我在做什麼錯了?我該如何解決?
下面是這一問題的jsbin:https://jsbin.com/fohuriy/14/edit?js,console
很好的解釋,謝謝! –