From: "David A. Black" Date: 2009-01-12T11:35:58+09:00 Subject: Re: functional programming Hi -- On Mon, 12 Jan 2009, William James wrote: > Joel VanderWerf wrote: > >> Mike Gold wrote: >>> Brian Candler wrote: >>>> (2) to understand Mike's assertion >>>> that Ruby lambdas are not first-class functions (i.e. "The >> difference >> between lambda { } and a real first-class function is >> quite profound"). >>> >>> Ruby: >>> >>> mean = lambda { |*args| >>> (args.inject(0) { |acc, x| acc + x })/args.size.to_f >>> } >>> data = [[1,2,3], [4,5,6,7]] >>> >>> data.map(&mean) # fail >>> data.map(&:mean) # fail >>> data.map { |*numbers| mean.call(*numbers) } # fail >>> data.map { |numbers| mean.call(*numbers) } # phew, found it >>> >>> Haskell: >>> >>> map mean data >>> >>> On this example alone, I think my characterization of "quite >>> profound" is justified. >> >> Why isn't this the ruby candidate for comparison: >> >> mean = lambda { |ary| >> (ary.inject { |acc, x| acc + x })/ary.size.to_f >> } >> data.map(&mean) # => [2.0, 5.5] > > mean = proc{|a| a.inject{|s,x| s+x} / a.size.to_f } > ==># > data.map &mean > ==>[2.0, 5.5] I believe that's the same as what Joel just posted (give or take some cosmetics). Or is my eye skipping over something? David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) http://www.wishsight.com => Independent, social wishlist management!