Skip to content

Instantly share code, notes, and snippets.

@getify
Last active September 19, 2024 20:49
Show Gist options
  • Save getify/9d583553d5ada22917f488f01dbc78c2 to your computer and use it in GitHub Desktop.
Save getify/9d583553d5ada22917f488f01dbc78c2 to your computer and use it in GitHub Desktop.
improve imperative code? is the reducer better? is there another better way?
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!");
}
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