utils.js icon indicating copy to clipboard operation
utils.js copied to clipboard

Add converge analog from ramda

Open sp3ber opened this issue 6 years ago • 0 comments

In some cases i need converge function, f.e. ` const extractOfferSuggestions = pathOr(["offerSuggestions"], []); const extractWordSuggestions = pathOr(["wordSuggestions"], []); const mapSuggestionsToOptions = (suggestions): SuggestOption[] => [];

// without converge const transformSuggestionsToOptions = suggestions => { const wordSuggestions = extractWordSuggestions(suggestions); const offerSuggestions = extractOfferSuggestions(suggestions); return mapSuggestionsToOptions({ offerSuggestions, wordSuggestions }); };

// with converge const transformSuggestionsToOptions = converge( ([wordSuggestions, offerSuggestions]) => mapSuggestionsToOptions({ offerSuggestions, wordSuggestions }), [extractWordSuggestions, extractOfferSuggestions] ); // or with other mapSuggestionsToOptions signature (two params instead of one object param) const transformSuggestionsToOptions = converge( mapSuggestionsToOptions, [extractWordSuggestions, extractOfferSuggestions] ); ` Some points on this combinator https://jrsinclair.com/articles/2019/compose-js-functions-multiple-parameters/

I think, createSelector from 'reselect' has the same idea (but with memoization)

sp3ber avatar Sep 25 '19 10:09 sp3ber