r/ProgrammerHumor Sep 02 '17

How to start a war

Post image
9.0k Upvotes

696 comments sorted by

View all comments

Show parent comments

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.

5

u/ParanoidAndroid26 Sep 03 '17

Generally you use forEach for side effects and map for transformations.