I dunno why people think functional programming jobs are vanishingly scarce. They are not. If you can demonstrate skill in functional languages, you might even get recruited.
It's true there are more wage-slave code-a-day jobs, but those jobs can be easily filled by uninspired hacks who are in it for the money. If you're reading this, the odds of you being one of those hacks is very low. The job market is smaller, but the workforce of talented functional programmers is much smaller, so the demand is still high.
And Clojure & Scala are even easier because you can get a java job and them worm them in. It's not difficult to make a compelling argument that Clojure & Scala are better at Java than Java is.
Functional programming jobs are scarce, but that's because no-one is a "functional programmer" in the same way that you might be a "PHP programmer". Instead you get lots of jobs where you happen to write a lot of code in a functional programming language but you don't look like a programmer in an organizational sense - you're an "analyst" or an "engineer" or whatever. That's where F# is making inroads.
The article actually contains the "if u cn rd ths..." phrase and it's a nice touch. However, it would be better to keep the original title and mention the phrase in the comments.
def fact(n):
if (n <= 1):
return 1
else:
return n * fact(n-1)
1 line vs 5 lines doesn't seem like an advantage to me when it takes pretty much the same amount of time to read and understand the complicated 1 liner.
The main advantage of a 1-liner is that you can compose the whole thing and execute it without having to format anything. That is a nice feature for code that is only ever intended to be used once, such as from the python repl or some other shell. --Or more than once, for the duration of a session. It's a lot easier to use command history to get and modify a 1-liner than to replay a 5-line function definition.
When the code is primarily expressions, it's relatively easy to to this. Trying to do one-liners with regular python syntax can get tricky.
Also, if you have a series of similar relationships to present, it's often easier to line them up next to each other. You might cut 12 lines to 6 lines and the whole thing will be far clearer. Although in those cases, it's probably more effective to use a dictionary to achieve the same thing, with the advantage of data/logic separation.
Some of the responses are outdated, here's one of the things that is now part of python:
Python 2.5 introduced defaultdict in the collections module:
from collections import defaultdict
d = defaultdict(int)
d['foo'] +=1 # look ma! no error!
For this application, if you're using Python 2.7 there's now a Counter in collections, you can do the same as above, just call Counter(). You can also do:
c = Counter(iter) # iter is an iterable. It will count the elements!
Incidentally, most C-style-syntax languages have a ternary conditional operator, and I use it quite a lot for things like building strings, very small conditionals, etc.... When used correctly, it can improve readability and expressiveness quite a bit.
x = <test> ? <result> : <alternative>
Some times for slightly more complex expressions, I'll put them on separate lines so as to have a very compact if/else structure.
As pointed out in the OP, there are actually quite a lot of ways to simulate the ternary operator in Python. An interesting one is (result, alternative)[test]. If result and alternative are functions you could write it as (result, alternative)[test]().
[+] [-] KirinDave|15 years ago|reply
It's true there are more wage-slave code-a-day jobs, but those jobs can be easily filled by uninspired hacks who are in it for the money. If you're reading this, the odds of you being one of those hacks is very low. The job market is smaller, but the workforce of talented functional programmers is much smaller, so the demand is still high.
And Clojure & Scala are even easier because you can get a java job and them worm them in. It's not difficult to make a compelling argument that Clojure & Scala are better at Java than Java is.
[+] [-] gaius|15 years ago|reply
[+] [-] pilom|15 years ago|reply
[+] [-] TomasSedovic|15 years ago|reply
The article actually contains the "if u cn rd ths..." phrase and it's a nice touch. However, it would be better to keep the original title and mention the phrase in the comments.
[+] [-] shabda|15 years ago|reply
[+] [-] JustinSeriously|15 years ago|reply
- If you can read this, you can get a job in financial program (if there were any)
- If you can read this, you can get a job in functional programs/programming (if there were any)
I don't know what this proves.
[+] [-] ximeng|15 years ago|reply
http://webcache.googleusercontent.com/search?q=cache:norvig....
Seems a bit out-of-date (upcoming Python 2.5?), not sure why it's here really.
[+] [-] danio|15 years ago|reply
Is there more optimisation possible?
[+] [-] Goladus|15 years ago|reply
When the code is primarily expressions, it's relatively easy to to this. Trying to do one-liners with regular python syntax can get tricky.
Also, if you have a series of similar relationships to present, it's often easier to line them up next to each other. You might cut 12 lines to 6 lines and the whole thing will be far clearer. Although in those cases, it's probably more effective to use a dictionary to achieve the same thing, with the advantage of data/logic separation.
[+] [-] Kilimanjaro|15 years ago|reply
[+] [-] gaius|15 years ago|reply
I don't like tho' that "lambda" is a keyword in Python, I much prefer Haskell's \ or OCaml's fun.
[+] [-] d0mine|15 years ago|reply
[+] [-] anateus|15 years ago|reply
Python 2.5 introduced defaultdict in the collections module:
For this application, if you're using Python 2.7 there's now a Counter in collections, you can do the same as above, just call Counter(). You can also do:[+] [-] noahlt|15 years ago|reply
[+] [-] unknown|15 years ago|reply
[deleted]
[+] [-] tomotomo|15 years ago|reply
[+] [-] albertcardona|15 years ago|reply
As for content of the Python IAQ, I've enjoyed learning about two python idioms I had never known possible:
1. The ternary conditional operator:
x = <result> if <test> else <alternative>
2. Using or to assign an alternative value when the first is False or None
x = <option> or <option>
[+] [-] TallGuyShort|15 years ago|reply
x = <test> ? <result> : <alternative>
Some times for slightly more complex expressions, I'll put them on separate lines so as to have a very compact if/else structure.
[+] [-] adambyrtek|15 years ago|reply
[+] [-] IgorPartola|15 years ago|reply
And for desert: I love this syntax:
For more info see: http://stackoverflow.com/questions/394809/python-ternary-ope...[+] [-] reinhardt|15 years ago|reply
Is there any real world evidence of said claim, outside geek-fetish bubbles like HN ?
[+] [-] unknown|15 years ago|reply
[deleted]