fact = lambda n: _if (n <= 1) (1) (lambda: n * fact(n-1))
over 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.Is there more optimisation possible?
python -c "import _if; fact = lambda n: _if (n <= 1) (1) (lambda: n * fact(n-1)) ; fact(5)"
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.
fact = lambda n: 1 if n <= 1 else n*fact(n-1)
or, for that matter: def fact(n): return 1 if n <= 1 else n*fact(n-1) def fact(n):
if n<=1: return 1
else: return n * fact(n-1) fact(1) = 1
fact(n) = n * fact(n-1)
Seems much more readable to me. def fact(n):
return 1 if n<=1 else n*fact(n-1)I don't like tho' that "lambda" is a keyword in Python, I much prefer Haskell's \ or OCaml's fun.
from math import factorial
Or fact = lambda n: reduce(operator.mul, xrange(1, n+1), 1)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.
- 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.
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>
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.
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Personally I'd hate to see something like that in any real-world project. (test and [result] or [alternative])[0]And for desert: I love this syntax:
if 0 < x <= 100: pass
For more info see: http://stackoverflow.com/questions/394809/python-ternary-ope...Is there any real world evidence of said claim, outside geek-fetish bubbles like HN ?
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.
If you work for an actual software company (or perhaps an innovative financial services company) as a software engineer (or even on an operations team in some cases e.g., Sysadmins at Google using Haskell, ops tools at Yahoo once built in OCaml), there's plenty of chances to do functional programming (or something else that interests you).
It's just generally people are programmed to think "it's just a job" and choose their jobs based on salary, name recognition, commute hours etc... (Some also have very specialized skills like systems programming or machine learning and aren't particularly interested in programming languages, although I think they're missing out)
As for me, I interviewed at several jobs that featured functional programming during my last jobsearch. I actually turned down offers that were not devoted to functional (or at least hybrid object/functional) technologies.
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.
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! x = 3 if foo() else 6