Re: map, filter & reduce...
From: John Roth (newsgroups_at_jhrothjr.com)
Date: 11/16/03
- Next message: Peter Otten: "Re: map, filter & reduce..."
- Previous message: Ben: "map, filter & reduce..."
- In reply to: Ben: "map, filter & reduce..."
- Next in thread: Peter Otten: "Re: map, filter & reduce..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 16 Nov 2003 07:55:21 -0500
"Ben" <crescent_au@yahoo.com> wrote in message
news:d99e1341.0311160416.20f8cf18@posting.google.com...
> Hi all,
>
> I'm trying to figure out how how complex map, filter and reduce work
> based on the following piece of code from
> http://www-106.ibm.com/developerworks/linux/library/l-prog.html :
>
> bigmuls = lambda xs,ys: filter(lambda (x,y):x*y > 25, combine(xs,ys))
> combine = lambda xs,ys: map(None, xs*len(ys), dupelms(ys,len(xs)))
> dupelms = lambda lst,n: reduce(lambda s,t:s+t, map(lambda l,n=n:
> [l]*n, lst))
> print bigmuls((1,2,3,4),(10,15,3,22))
>
> The solution generated by the above code is: [(3, 10), (4, 10), (2,
> 15), (3, 15), (4, 15), (2, 22), (3, 22), (4, 22)]
>
> I'm stuck on the second line in "map(None, xs*len(ys),
> dupelms(ys,len(xs))"... Can someone explain me how the map function
> evaluates??
In this case, map is (almost) a synonym for zip. It's going to
create a list where each entry is a two item list containing the
corresponding elements from each of the inputs. The first
input is, of course, the result of the multiply, and the second
is whatever the dupelms() call produced.
John Roth
>
> Thanks
> Ben
- Next message: Peter Otten: "Re: map, filter & reduce..."
- Previous message: Ben: "map, filter & reduce..."
- In reply to: Ben: "map, filter & reduce..."
- Next in thread: Peter Otten: "Re: map, filter & reduce..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|