[BangPypers] diffrerence between lambda function and ordinary one

2010-08-06 Thread Rahul R
i was writing some basic code . for understanding lambda functions , but
couldnt understand the difference between a lambda function and an ordinary
function.

for example

>>>def f (x): return x**2
...
>>> print f(8)
>>> 64
>>> g = lambda x: x**2
>>>
>>> print g(8)
>>> 64

wats a need for using lambda functions ..?
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] diffrerence between lambda function and ordinary one

2010-08-06 Thread Zubin Mithra
>
>
> i was writing some basic code . for understanding lambda functions , but
> couldnt understand the difference between a lambda function and an ordinary
> function.
>
> for example
>
> >>>def f (x): return x**2
> ...
> >>> print f(8)
> >>> 64
> >>> g = lambda x: x**2
> >>>
> >>> print g(8)
> >>> 64
>
> wats a need for using lambda functions ..?
>

Lambda functions are generally used where you need one liner functions to
get things done. It was a feature which was considered for removal in py3k
but there were too many people who loved the feature. :)

Personally I feel that lambda functions are great as long as they are
readable.

zm
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] diffrerence between lambda function and ordinary one

2010-08-06 Thread Anand Chitipothu
2010/8/6 Rahul R :
> i was writing some basic code . for understanding lambda functions , but
> couldnt understand the difference between a lambda function and an ordinary
> function.
>
> for example
>
def f (x): return x**2
> ...
 print f(8)
 64
 g = lambda x: x**2

 print g(8)
 64
>
> wats a need for using lambda functions ..?

Lambda functions are expressions. You can define a lambda function
inside a function call or a dictionary definition, which is not
possible with regular functions. For example:

d.sort(key=lambda x: x.lower())

functions = {
"lower": lambda k: k.lower(),
"upper": lambda k: k.upper()
}

Anand
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] diffrerence between lambda function and ordinary one

2010-08-06 Thread Pradeep Gowda
On Fri, Aug 6, 2010 at 10:20 AM, Rahul R  wrote:
> i was writing some basic code . for understanding lambda functions , but
> couldnt understand the difference between a lambda function and an ordinary
> function.
>
> for example
>
def f (x): return x**2
> ...
 print f(8)
 64
 g = lambda x: x**2

 print g(8)
 64
>
> wats a need for using lambda functions ..?

You mean "what's the need for using lambda functions?"

Lambda functions are anonymous functions, ie., functions without a name.
Along with map, reduce, filter, lambda functions provide functional
programming[1]
constructs to python.

You can write code like this:

from operator import add
print reduce(add, filter(lambda x: x%2==0, xrange(10)))

to print the sum of even numbers less than 10
instead of:
tot = 0
for i in xrange(10):
if i%2 == 0:
tot += i
print tot

...

Anyway,
One does not "need" to use lambda function. Almost in all the places where
you use lambda functions, you can replace it with a named function with
added benefit of clarity.

Python lambdas are limited to a single statement, which makes them particularly
hobbled.

Guido is not very fond of FP constructs, in fact he wanted to remove
them from python 3.
After much discussion, lambda survived in Py3.


[1] http://en.wikipedia.org/wiki/Functional_programming
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] python speed comparison

2010-08-06 Thread Abhishek Mishra
>
> 
>


> Your python code as is clocked about 72 seconds on my notebook. The
> following came in at about 4.6 seconds (just has a small trick of reusing
> earlier results)
>
> import time
> start =time.time()
> longest = None
> longest_elements = 0
> solved = {}
> for val in xrange(1,100) :
>counter = 1
>number = val
>while number != 1 :
>number = number / 2 if number % 2 == 0 else 3 * number + 1
>if number in solved :
>counter = counter + solved[number]
>break
>else :
>counter = counter + 1
>if counter > longest_elements :
>longest_elements = counter
>longest = val
>solved[val] = counter
> end = time.time()
> print "Time:", end - start
> print longest, ':', longest_elements
>
> Dhananjay
>
>
If you are still looking forward to squeeze around 200ms more, try bit ops -
Original code by Dhananjay averages to 4.25823163986s on my machine

While if you change -
number = number / 2 if number % 2 == 0 else 3 * number + 1
into
number =  3*number+1 if number&1 else number >> 1

it averages 4.058642069496667s on my machine.

Abhishek
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] diffrerence between lambda function and ordinary one

2010-08-06 Thread Shashwat Anand
>
>
>
> You can write code like this:
>
> from operator import add
> print reduce(add, filter(lambda x: x%2==0, xrange(10)))
>
> to print the sum of even numbers less than 10
> instead of:
> tot = 0
> for i in xrange(10):
> if i%2 == 0:
> tot += i
> print tot
>
> ...
>


I would prefer LC anyway:

>>> sum(i for i in range(0,10,2))
20


-- 
~l0nwlf
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] diffrerence between lambda function and ordinary one

2010-08-06 Thread Pradeep Gowda
On Fri, Aug 6, 2010 at 10:45 AM, Shashwat Anand
 wrote:
> I would prefer LC anyway:
>
 sum(i for i in range(0,10,2))
> 20

why use LC at all?
>> sum(range(0,10,2))
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] diffrerence between lambda function and ordinary one

2010-08-06 Thread Shashwat Anand
On Fri, Aug 6, 2010 at 8:37 PM, Pradeep Gowda  wrote:

> On Fri, Aug 6, 2010 at 10:45 AM, Shashwat Anand
>  wrote:
> > I would prefer LC anyway:
> >
>  sum(i for i in range(0,10,2))
> > 20
>
> why use LC at all?
> >> sum(range(0,10,2))
>

Oops. Wrong choice of example.

-- 
~l0nwlf
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] python speed comparison

2010-08-06 Thread Anand Chitipothu
Readability counts. Here is my attempt.

def memoize(f):
cache = {}
def g(a):
if a not in cache:
cache[a] = f(a)
return cache[a]
return g

@memoize
def solve(n):
if n == 1:
return 1
elif n%2 == 0:
return 1 + solve(n/2)
else:
return 1 + solve(3*n+1)

print max((solve(i), i) for i in range(1, 1+100))

$ time python p14.py
(525, 837799)

real0m3.981s
user0m3.728s
sys 0m0.242s

Anand
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] diffrerence between lambda function and ordinary one

2010-08-06 Thread Abhishek Mishra
On Fri, Aug 6, 2010 at 8:37 PM, Pradeep Gowda  wrote:

> On Fri, Aug 6, 2010 at 10:45 AM, Shashwat Anand
>  wrote:
> > I would prefer LC anyway:
> >
>  sum(i for i in range(0,10,2))
> > 20
>
> why use LC at all?
> >> sum(range(0,10,2))
>
>
^ I too have written such "i for i in ..." expressions at times and I feel
enlightened now.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] diffrerence between lambda function and ordinary one

2010-08-06 Thread Abhishek Mishra
This would serve a better example -
>>> sum( int(x) for x in raw_input().split() )
1 2 3 4
10
>>>
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers