2017-05-29 117 views
0

我的工作讓我身邊的函數式編程頭和函數式編程,我有以下功能:與lodash收益率意外輸出

fp = lodash/fp, _ = lodash (not yet optimized) 

// Reference functions 
const getAttributes = fp.getOr({}, `attributes`) 
const toArray = fp.curry(input => _.isArray(input) ? [...input] : input ? [input] : []) 

const getTags = fp.flow(getAttributes(), fp.getOr([], 'tags'), toArray()) 
// returns ['tag 1', 'Tag 2', ...] or else an empty array. 


const lowerString = fp.map(taxonomy => taxonomy ? taxonomy.toLowerCase() : '') 
// Used to normalize the tags to lowercase. 


const normalizeTags = fp.flow(getTags(), (d) => {console.log(d); return d}, lowerString()) 
// My composed function that combines the two 

的問題是,在兩者之間console.log(),因此結果原因是我的lowerString()錯誤,是getAttributes()返回的原始對象。

+0

可以陷害我們在Plunker或類似工作的例子嗎? – valepu

回答

0

我認爲這個問題是您傳遞調用getAttributestoArraygetTags,並且lowerString作爲參數flow而不是函數本身的結果。

const getTags = fp.flow(getAttributes(), fp.getOr([], 'tags'), toArray()) 

VS

const getTags = fp.flow(getAttributes, fp.getOr([], 'tags'), toArray) 

const normalizeTags = fp.flow(getTags(), (d) => {console.log(d); return d}, lowerString()) 

VS

const normalizeTags = fp.flow(getTags, (d) => {console.log(d); return d}, lowerString) 
+0

謝謝!我錯過了! – motleydev