class Proc
def apply(enum)
enum.map &self
end
alias | apply
def reduce(enum)
enum.inject &self
end
alias <= reduce
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
end
sum = lambda {|x,y| x+y } # A function to add two numbers
mean = (sum<=a)/a.size # Or sum.reduce(a) or a.inject(&sum)
deviation = lambda {|x| x-mean } # Function to compute difference from
mean
square = lambda {|x| xx } # Function to square a number
standardDeviation = Math.sqrt((sum<=squaredeviation|a)/(a.size-1))
Ok so now I have another example from this book which does not contain
parentheses around deviation|a. So then why is this part:
(sum<=square*deviation|a)
not evaluated from left to right.
Apparently, it evaluates this part:
deviation|a
first
and this part second:
square*deviation|a
and then finally:
sum<=
So it looks like it is evaluating from right to left. Even though it
should be evaluating from left to right…