[Rails] Re: evaluating expressions left to right

2012-12-27 Thread John Merlino
That was my hunch. Thanks for clarifying.

On Wednesday, December 26, 2012 6:48:18 PM UTC-5, Matt Jones wrote:
>
>
>
> On Tuesday, 25 December 2012 20:13:16 UTC-5, John Merlino wrote:
>>
>> 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. 
>>
>
>  That's not how it parses, thanks to operator precedence - the same reason 
> that 2+5*10+3 parses as 2.+((5.*(10)).+(3)) and not 2.+(5.*(10.+(3))).
>
>  You can use a tool like Ripper (
> http://www.rubyinside.com/using-ripper-to-see-how-ruby-is-parsing-your-code-5270.html)
>  
> to see exactly how something is being parsed. Trying your expression yields:
>
> [:program,
>  [[:binary,
>[:vcall, [:@ident, "sum", [1, 0]]],
>:<=,
>[:binary,
> [:binary,
>  [:vcall, [:@ident, "square", [1, 5]]],
>  :*,
>  [:vcall, [:@ident, "deviation", [1, 12,
> :|,
> [:vcall, [:@ident, "a", [1, 22]]]
>
> Or, distilled back to a fully-parenthized code version:
>
> sum <= ((square*deviation) | a)
>
> With the method calls written out explicitly:
>
> sum.<=((square.*(deviation)).|(a))
>
> Essentially, this creates a function that calculates the squared deviation 
> from the mean (square*deviation), applies it to the list a, and then sums 
> the resulting values.
>
> This sort of confusion is why most people recommend avoiding operator 
> overloading in most cases - there are a bunch of precedence rules built 
> into the language, and you're essentially stuck with them.
>
> --Matt Jones
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-talk/-/Td1UvO4YivgJ.
For more options, visit https://groups.google.com/groups/opt_out.




[Rails] Re: evaluating expressions left to right

2012-12-26 Thread Matt Jones


On Tuesday, 25 December 2012 20:13:16 UTC-5, John Merlino wrote:
>
> 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. 
>

 That's not how it parses, thanks to operator precedence - the same reason 
that 2+5*10+3 parses as 2.+((5.*(10)).+(3)) and not 2.+(5.*(10.+(3))).

 You can use a tool like Ripper 
(http://www.rubyinside.com/using-ripper-to-see-how-ruby-is-parsing-your-code-5270.html)
 
to see exactly how something is being parsed. Trying your expression yields:

[:program,
 [[:binary,
   [:vcall, [:@ident, "sum", [1, 0]]],
   :<=,
   [:binary,
[:binary,
 [:vcall, [:@ident, "square", [1, 5]]],
 :*,
 [:vcall, [:@ident, "deviation", [1, 12,
:|,
[:vcall, [:@ident, "a", [1, 22]]]

Or, distilled back to a fully-parenthized code version:

sum <= ((square*deviation) | a)

With the method calls written out explicitly:

sum.<=((square.*(deviation)).|(a))

Essentially, this creates a function that calculates the squared deviation 
from the mean (square*deviation), applies it to the list a, and then sums 
the resulting values.

This sort of confusion is why most people recommend avoiding operator 
overloading in most cases - there are a bunch of precedence rules built 
into the language, and you're essentially stuck with them.

--Matt Jones

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-talk/-/i8Zt0_c0fRsJ.
For more options, visit https://groups.google.com/groups/opt_out.




[Rails] Re: evaluating expressions left to right

2012-12-25 Thread John Merlino
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


On Dec 23, 3:14 am, 7stud --  wrote:
> In this method call:
>
> meth1(meth2(meth3))
>
> ...which value has has to be computed first so that meth1 can return?
>
> --
> Posted viahttp://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [Rails] Re: evaluating expressions left to right

2012-12-23 Thread Colin Law
If you think about it there is only one possible answer to that
question.  How could it evaluate meth2(meth3) without evaluating meth3
first, in order to pass the result to meth2?  Similarly how could it
call meth1 before it evaluated the parameter to pass to it?

Colin

On 23 December 2012 08:14, 7stud --  wrote:
> In this method call:
>
> meth1(meth2(meth3))
>
> ...which value has has to be computed first so that meth1 can return?
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to 
> rubyonrails-talk+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [Rails] Re: evaluating expressions left to right

2012-12-23 Thread Jordon Bedwell
meth3 -> (meth2) -> (meth1)

The logic is that meth3 has to return so that meth2 can accept,
process and return so that meth1 can accept, process and return.
Don't read nested methods like you read a book, with nested methods
the last to be nested is the first to be executed.

On Sun, Dec 23, 2012 at 2:14 AM, 7stud --  wrote:
> In this method call:
>
> meth1(meth2(meth3))
>
> ...which value has has to be computed first so that meth1 can return?
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to 
> rubyonrails-talk+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[Rails] Re: evaluating expressions left to right

2012-12-23 Thread 7stud --
In this method call:

meth1(meth2(meth3))

...which value has has to be computed first so that meth1 can return?

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.