Last active
June 10, 2019 01:16
-
-
Save hughsw/c92f4d9702bf051632c6d83342bfde11 to your computer and use it in GitHub Desktop.
Versions of collectSymbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const input = ['AED_AFN', 'ALL_AMD', 'AMD_AFN', 'ALL_GBP']; | |
console.log('input:', JSON.stringify(input)); | |
// somewhat functional, somewhat imperative | |
const collectSymbols0 = pairs => { | |
const collectedSymbols = {}; | |
pairs | |
.map(pair => pair.split('_')) | |
.forEach(([left, right]) => { | |
if (!collectedSymbols[left]) { | |
collectedSymbols[left] = []; | |
} | |
collectedSymbols[left].push(right); | |
}); | |
return collectedSymbols; | |
}; | |
console.log('collectSymbols0(input):', JSON.stringify(collectSymbols0(input))); | |
// more functional, less imperative | |
const collectSymbols1 = pairs => { | |
const collectedSymbols = {}; | |
pairs | |
.map(pair => pair.split('_')) | |
.forEach(([left, right]) => (collectedSymbols[left] = collectedSymbols[left] || []).push(right)); | |
return collectedSymbols; | |
}; | |
console.log('collectSymbols1(input):', JSON.stringify(collectSymbols1(input))); | |
// functional, imperative | |
const collectSymbols2 = pairs => pairs | |
.map(pair => pair.split('_')) | |
.reduce((collectedSymbols, [left, right]) => { | |
collectedSymbols[left] = collectedSymbols[left] || []; | |
collectedSymbols[left].push(right); | |
return collectedSymbols; | |
}, {}); | |
console.log('collectSymbols2(input):', JSON.stringify(collectSymbols2(input))); | |
// fairly functional, slightly imperative | |
const collectSymbols3 = pairs => pairs | |
.map(pair => pair.split('_')) | |
.reduce((collectedSymbols, [left, right]) => ((collectedSymbols[left] = collectedSymbols[left] || []).push(right), collectedSymbols), {}); | |
console.log('collectSymbols3(input):', JSON.stringify(collectSymbols3(input))); | |
// separate imperative from functional | |
const pushy = (collectedSymbols, [left, right]) => ((collectedSymbols[left] = collectedSymbols[left] || []).push(right), collectedSymbols); | |
const collectSymbols4 = pairs => pairs.map(pair => pair.split('_')).reduce(pushy, {}); | |
console.log('collectSymbols4(input):', JSON.stringify(collectSymbols4(input))); | |
/* $ node collectSymbols.js | |
input: ["AED_AFN","ALL_AMD","AMD_AFN","ALL_GBP"] | |
collectSymbols0(input): {"AED":["AFN"],"ALL":["AMD","GBP"],"AMD":["AFN"]} | |
collectSymbols1(input): {"AED":["AFN"],"ALL":["AMD","GBP"],"AMD":["AFN"]} | |
collectSymbols2(input): {"AED":["AFN"],"ALL":["AMD","GBP"],"AMD":["AFN"]} | |
collectSymbols3(input): {"AED":["AFN"],"ALL":["AMD","GBP"],"AMD":["AFN"]} | |
collectSymbols4(input): {"AED":["AFN"],"ALL":["AMD","GBP"],"AMD":["AFN"]} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment