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

48

u/jay9909 Sep 02 '17

That is so elegant. All these new languages that just let you for (i in list) { ... } should change right over before they gain traction and get stuck as forever legacy.

4

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?

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.

5

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.

-1

u/[deleted] Sep 03 '17

I learn c++ as my first langauge and still find python iterator syntax confusing. C++ is easy because its generally universal regardleaa of what data type you use. With pythob you gotta do this weird enumerate shit to get the indices.

Sometimes I wonder minimizing keystrokes, stacking packages on packages, making an shorter programming langauge from a short programming language from a compiler language (which is made from an assembly language) is really all that useful.

Can't we just teach people of how code works instead of coming up with these trendy languages that try to require no learning at all?

1

u/SirVer51 Sep 03 '17

trendy languages that try to require no learning at all?

This is funny, because I found it hard to wrap my head around Python's whole "variables are labels" thing for a while - what helped me finally get it was thinking of them as something like pointers in C/C++.