domains = set( email.split('@')[-1] for email in open(path) )
I can't do the mental gymnastics to think about the code that way.
When I think about the purpose of the code, the result of the code, before the code has been written, I do this by "compiling and running" in my head. Of course, there's no actual compiling. There's just thinking.
When I reason about the code, either code I'm going to write, code I wrote, or code other people wrote, I execute the code in my head.
I'm currently building a web-scraping system, to look information on partner web site. I'm using python 3, selenium, pyodbc to sql server. I'm also developing the same in C# (3.5) using the webbrowser component in systems.forms. It's got a lot of moving parts, lots of languages involved. I read the documentation for the tools, then I assemble in my head, essentially "running the code" in my head, and then, once all the edge cases are dealt with (are there iframes, popups, ajax async calls, flash and pdfs) then I design on paper, with boxes, and then, later, later, if it all works in my head, then I code it.
Can't do that if I can't reason in my head about what the program is going to do, exactly.
Functional Programming, for me, does not fit in my mental model, so I can't use it, regardless of actual implementation.
http://qt-project.org/doc/qt-4.8/widgets-analogclock.html
When you could instead say just what you mean in functional programming:
http://elm-lang.org/edit/examples/Intermediate/Clock.elm
The most important skill for a programmer is to be able to learn new things. Especially when they're there to help you.
You can think of the body of a for loop in python as the body of a function. In that case, a for loop is like the built-in map function, which applies that function to every element of the list. So convert your for loop into:
And instead of a for loop we can use the standard function, 'map'. Now, obviously the data here isn't immutable. But what if, instead of changing 'domains' every time the function is called, we passed in the current value of 'domains', and returned a changed value, like so: Then, we could apply this function to each element of the list in turn, passing the current list of domains at each stage to the body. This is the standard function 'reduce' in Python (which the parent calls by its other common name, 'fold'): You could think of 'reduce' in python being implemented like: Or, it could also be implemented recursively, which means it wouldn't need to use mutation internally: Many for loops that build up a result can be expressed instead using 'reduce', so reduce is one of the most important building blocks of functional programming.