Last active
September 19, 2024 20:49
-
-
Save getify/9d583553d5ada22917f488f01dbc78c2 to your computer and use it in GitHub Desktop.
improve imperative code? is the reducer better? is there another better way?
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
function isEligible(value,userID) { | |
// .. whatever arbitrary logic .. | |
} | |
const buckets = [ | |
user1: new Set(), | |
user2: new Set(), | |
user3: new Set(), | |
user4: new Set() | |
} | |
const specialValue = 42; | |
let count = 0; | |
for (const [ userID, bucket ] of Object.entries(buckets)) { | |
if (isEligible(specialValue,userID)) { | |
count++; | |
bucket.add(specialValue); | |
} | |
} | |
if (count >= 2) { | |
console.log("at least 2 users were eligible for the special value!"); | |
} |
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
function isEligible(value,userID) { | |
// .. whatever arbitrary logic .. | |
} | |
const buckets = [ | |
user1: new Set(), | |
user2: new Set(), | |
user3: new Set(), | |
user4: new Set() | |
} | |
const specialValue = 42; | |
const count = ( | |
Object.entries(buckets) | |
.filter(([userID,bucket]) => isEligible(specialValue,userID)) | |
.reduce((counter,[userID,bucket]) => (bucket.add(specialValue),counter+1),0) | |
); | |
if (count >= 2) { | |
console.log("at least 2 users were eligible for the special value!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment