ok, it didn't look like nested methods. But I made to believe that
this:
sum<=square*deviation|a
is exactly the same as this:
sum<=(square*(deviation|(a)))
So if this is true, then still a question remains.
Here's the original context again:
module Functional
def compose(f)
if self.respond_to?(:arity) && self.arity == 1
lambda { |*args| self[f[*args]] }
else
lambda {|*args| self[*f[*args]] }
end
end
alias * compose
def apply(enum)
enum.map &self
end
alias | apply
def reduce(enum)
enum.inject &self
end
alias <= reduce
end
class Proc
include Functional
end
#client code
a = [1,2,3]
sum = lambda { |x,y| x+y }
mean = (sum<=a)/a.size
deviation = lambda { |x| x-mean }
square = lambda { |x| x*x }
standardDeviation = Math.sqrt((sum<=square*deviation|a)/(a.size-1))
On the last line, this executes first:
deviation|a
this returns a new array of how far each of elements are from the
mean.
Then this array gets passed to * which is invoked on square (a lambda
object):
square*returned_array
That calls compose where f parameter is the returned array from above.
So then this line is returned by compose since the array object doesnt
respond to arity:
lambda {|*args| self[*f[*args]] }
So it appears the return value of compose is the lambda object. That
presents a problem because <= expects an enum argument.
sum<=this_should_be_an_enum