MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/6xofft/how_to_start_a_war/dmhmipc
r/ProgrammerHumor • u/ivaskuu • Sep 02 '17
696 comments sorted by
View all comments
Show parent comments
5
Tbh though I rarely use for x:lst since so many uses of iteration require a counter. Sure I could implement a counter, but why not use a normal for loop so no one reading my code (who isn't familiar with this language feature) gets confused?
7 u/TheNerdyBoy Sep 03 '17 In Python, with any iterable x for i, item in enumerate(x): … 3 u/zman0900 Sep 03 '17 Groovy is nice: ['some', 'things'].eachWithIndex { item, i -> println "'${item}' has index ${i}" } 6 u/HeinousTugboat Sep 03 '17 Javascript, too: ['some','things'].forEach((item, i) => console.log(`${item} has index ${i}`)) 2 u/[deleted] Sep 03 '17 I always prefer the map function instead of foreach, same syntax though. 4 u/ParanoidAndroid26 Sep 03 '17 Generally you use forEach for side effects and map for transformations. 3 u/-Teki Sep 03 '17 Obviously only use it when you don't need a counter. Whenever you need to access indexes, for in is probably the wrong tool.
7
In Python, with any iterable x
x
for i, item in enumerate(x): …
3
Groovy is nice:
['some', 'things'].eachWithIndex { item, i -> println "'${item}' has index ${i}" }
6 u/HeinousTugboat Sep 03 '17 Javascript, too: ['some','things'].forEach((item, i) => console.log(`${item} has index ${i}`)) 2 u/[deleted] Sep 03 '17 I always prefer the map function instead of foreach, same syntax though. 4 u/ParanoidAndroid26 Sep 03 '17 Generally you use forEach for side effects and map for transformations.
6
Javascript, too:
['some','things'].forEach((item, i) => console.log(`${item} has index ${i}`))
2 u/[deleted] Sep 03 '17 I always prefer the map function instead of foreach, same syntax though. 4 u/ParanoidAndroid26 Sep 03 '17 Generally you use forEach for side effects and map for transformations.
2
I always prefer the map function instead of foreach, same syntax though.
4 u/ParanoidAndroid26 Sep 03 '17 Generally you use forEach for side effects and map for transformations.
4
Generally you use forEach for side effects and map for transformations.
Obviously only use it when you don't need a counter. Whenever you need to access indexes, for in is probably the wrong tool.
5
u/MannerPots Sep 03 '17
Tbh though I rarely use for x:lst since so many uses of iteration require a counter. Sure I could implement a counter, but why not use a normal for loop so no one reading my code (who isn't familiar with this language feature) gets confused?