▲ 342 ▼ What are your programming hot takes? (lemmy.ml) submitted 3 years ago by 257m@lemmy.ml to c/programming@programming.dev 894 comments fedilink hide all child comments
[–] indepndnt@lemmy.world 2 points 3 years ago (3 children) Honest question: is there a mapping function that handles the case where you need to loop through an iterable, and conditionally reference an item one or two steps ahead in the iterable? permalink fedilink source parent hideshow 6 child comments replies: [–] xigoi@lemmy.sdf.org 2 points 3 years ago* In Haskell, you could do something like map (\(thisItem, nextItem) -> …) (zip list (tail list)) permalink fedilink source parent [–] hellishharlot@programming.dev 2 points 3 years ago (1 child) Not that I'm aware of but that's a condition where you're thinking with an index. What's the difference you're looking for? permalink fedilink source parent hideshow 2 child comments replies: [–] indepndnt@lemmy.world 1 point 3 years ago (1 child) Something like parsing a string that could have command codes in it of varying length. So I guess the difference is, is this a 1-, 2-, or 3-character code? I have something like this in a barcode generator and I keep trying to find a way to make it more elegant, but I keep coming back to index and offset as the simplest and most understandable approach. permalink fedilink source parent hideshow 2 child comments replies: [–] hellishharlot@programming.dev 1 point 3 years ago So you could generate lists of 1, 2, and 3 character code items rather than looking at index +1 or something. permalink fedilink source parent [–] drathvedro@lemm.ee 2 points 3 years ago 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. permalink fedilink source parent
[–] xigoi@lemmy.sdf.org 2 points 3 years ago* In Haskell, you could do something like map (\(thisItem, nextItem) -> …) (zip list (tail list)) permalink fedilink source parent
[–] hellishharlot@programming.dev 2 points 3 years ago (1 child) Not that I'm aware of but that's a condition where you're thinking with an index. What's the difference you're looking for? permalink fedilink source parent hideshow 2 child comments replies: [–] indepndnt@lemmy.world 1 point 3 years ago (1 child) Something like parsing a string that could have command codes in it of varying length. So I guess the difference is, is this a 1-, 2-, or 3-character code? I have something like this in a barcode generator and I keep trying to find a way to make it more elegant, but I keep coming back to index and offset as the simplest and most understandable approach. permalink fedilink source parent hideshow 2 child comments replies: [–] hellishharlot@programming.dev 1 point 3 years ago So you could generate lists of 1, 2, and 3 character code items rather than looking at index +1 or something. permalink fedilink source parent
[–] indepndnt@lemmy.world 1 point 3 years ago (1 child) Something like parsing a string that could have command codes in it of varying length. So I guess the difference is, is this a 1-, 2-, or 3-character code? I have something like this in a barcode generator and I keep trying to find a way to make it more elegant, but I keep coming back to index and offset as the simplest and most understandable approach. permalink fedilink source parent hideshow 2 child comments replies: [–] hellishharlot@programming.dev 1 point 3 years ago So you could generate lists of 1, 2, and 3 character code items rather than looking at index +1 or something. permalink fedilink source parent
[–] hellishharlot@programming.dev 1 point 3 years ago So you could generate lists of 1, 2, and 3 character code items rather than looking at index +1 or something. permalink fedilink source parent
[–] drathvedro@lemm.ee 2 points 3 years ago 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. permalink fedilink source parent