257m@lemmy.ml to Programming@programming.dev · 1 year agoWhat are your programming hot takes?message-squaremessage-square897fedilinkarrow-up1349arrow-down114
arrow-up1335arrow-down1message-squareWhat are your programming hot takes?257m@lemmy.ml to Programming@programming.dev · 1 year agomessage-square897fedilink
minus-squaredrathvedro@lemm.eelinkfedilinkarrow-up2·1 year agoIn js there’s reduce. Something like arr.reduce((result, currentValue, currentIndex, original) => { if(currentIndex < original.length - 2 && original[currentIndendex + 2] % 2 === 0 ) { result.push(currentValue / 2) } else { result.push(currentValue); } return result; }, []) This would map arr and return halved values for elements for which the element two steps ahead is even. This should be available in languages where map is present. And sorry for possible typos, writing this on mobile.
In js there’s reduce. Something like
arr.reduce((result, currentValue, currentIndex, original) => { if(currentIndex < original.length - 2 && original[currentIndendex + 2] % 2 === 0 ) { result.push(currentValue / 2) } else { result.push(currentValue); } return result; }, [])
This would map arr and return halved values for elements for which the element two steps ahead is even. This should be available in languages where map is present. And sorry for possible typos, writing this on mobile.