This is a cool piece of python code showing off the first class function ability of python, written by Vivek Haldar.
Perhaps, not very useful (well maybe except in some long integer handling, but not efficient there), nevertheless it’s a cool piece of code.
It’s cool, because it illustrates 2 things:
1.Python’s first class treatment of functions and lambda function facility allows you to stack on a bunch of func calls, while evaluating them only at the end.
2.Natural numbers can be represented as set and operators that add elements to the set. or It can be represented as a set of functions composed and applied on an initial value.
Here’s the code:
#Copied from (https://gist.github.com/2438498)
zero = lambda f: lambda x: x
succ = (lambda n: lambda f: lambda x: f(n(f)(x)))
one = succ(zero)
add = (lambda m: lambda n: lambda f: lambda x: n(f(m(f)(x))))
mult = (lambda m: lambda n: lambda f: lambda x: n(m(f))(x))
exp = lambda m: lambda n: n(m)
plus1 = lambda x:x+1
church2int = lambda n: n(plus1)(0)
if i == 0:
print s, ' = ',eval(s)
c232 = int2church(232)
c421 = int2church(421)
print "232*421 = ",232*421
c2 = int2church(2)
c10 = int2church(10)
print '2**10 = ',2 **10
Now the coolest part is how he builds on expressions using the lambda(anonymous function ability.)
This is also the reason I tend to frown, when someone calls Python a Object-Oriented language.
I mean, it’s as much function-oriented as it is object-oriented language.
The two don’t necessarily need to be exclusive, but calling a language , implies or atleast entails,
that design debates/arguments (ones that don’t have clear datapoints to swing either way), tend to be decided/settled by .
I have used python for about 5 years, hung around passively, python-ideas,python-users,python-core-mentorship mailing lists,
And I can’t find that strong a bias in any of these places.
I can perhaps claim Python is more (English language specific?) than any other paradigm.
It’s an issue I get into arguing with some interviewers in the past,
except very few seem to even try and explain why they call it object-oriented.
(signs they don’t know what they are talking about??)
I know the Python homepage calls itself object-oriented language, but that doesn’t mean the language is Object-oriented.
Besides, I am not really worried too much about the orientation of a language,person or cat.
I think, the point really comes down to this. What properties are you implying/inferring when you say object-oriented?
What properties do you need for your application that depends on the choice of the language?
Once you can answer some parts of the questions, you have an idea of what language to choose.