Hello
pure Ruby:
If a=b=c=d=e is ok, then
a==b==c==e should also work
Any abbrevation for writing that?
thanks Opti
Hello
pure Ruby:
If a=b=c=d=e is ok, then
a==b==c==e should also work
Any abbrevation for writing that?
thanks Opti
Building off of James' [a, b, c, d].uniq.size == 1 solution, you can
actually also do something more along the lines of
[a, b, c, d].all?{|i| i == a}. However, any solution like that is O(n)
instead of O(1). Can you give a use case for this though? There is probably
a different place where your code could be altered to prevent this
comparison from needing to happen. It seems kind of odd to me.
- Eli
On Tue, Jan 10, 2017 at 6:04 PM, James Pacheco <james.pacheco@gmail.com> wrote:
> pure Ruby:
> If a=b=c=d=e is ok, then
> a==b==c==e should also workWhy should it "also work"? They're entirely different operations that just
happen to share some characters. In the first, each assignment returns the
value assigned; it mostly works "by accident". In the second, each will
return a Boolean result of the comparison, making further comparisons
nearly meaningless (unless you're comparing Booleans, and then any time it
worked would largely be by accident).> Any abbrevation for writing that?
Not really that I'm aware of. (a == b) && (b == c) && (c == d) will get
you the intent with minimal repetition. [a,b,c,d].uniq.size == 1 will give
you something kinda like what you want if you want a weird way to do it...-James
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
pure Ruby:
If a=b=c=d=e is ok, then
a==b==c==e should also work
Why should it "also work"? They're entirely different operations that just
happen to share some characters. In the first, each assignment returns the
value assigned; it mostly works "by accident". In the second, each will
return a Boolean result of the comparison, making further comparisons
nearly meaningless (unless you're comparing Booleans, and then any time it
worked would largely be by accident).
Any abbrevation for writing that?
Not really that I'm aware of. (a == b) && (b == c) && (c == d) will get you
the intent with minimal repetition. [a,b,c,d].uniq.size == 1 will give you
something kinda like what you want if you want a weird way to do it...
-James
Hello
pure Ruby:
If a=b=c=d=e is ok, then
a==b==c==e should also work
Any abbrevation for writing that?
"should"... that's a construct from your head you might want to examine further...
Taking your example, here's how the parser sees it raw:
% rake debug R="a=b=c=42"
s(:lasgn, :a, s(:lasgn, :b, s(:lasgn, :c, s(:lit, 42))))
so ruby sees the assignments as nested:
a=(b=(c=42))
1479 % rake debug R="a==b==c==d"
#<Racc::ParseError: env:1 :: parse error on value "==" (tEQ)>
So maybe you're thinking it should work similarly to `=`:
a==(b==(c==d))
Assuming the multi-assignment above (to 42) that would evaluate as:
42==(42==(42==42))
42==(42==true)
42==false
false
So... How do you think it should work for `==` ?
On Jan 10, 2017, at 14:15, Die Optimisten <inform@die-optimisten.net> wrote:
you can actually also do something more along the lines of [a, b, c, d].all?{|i| i == a}.
Pardon my pedanticism, but: [b, c, d].all?{|i| i == a}
I suppose that makes it O(n-1) ?
Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>
Well, it does "work". The question is more: what do you expect it to do?
Cheers
robert
On Tue, Jan 10, 2017 at 11:15 PM, Die Optimisten <inform@die-optimisten.net> wrote:
pure Ruby:
If a=b=c=d=e is ok, then
a==b==c==e should also work
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/
Pardon my pedanticism, but: [b, c, d].all?{|i| i == a}
I suppose that makes it O(n-1) ?
To be just as pedantic, you're supposed to use Big O-notation to denote the *shape* of something as it reaches infinity. That's the same reason why we don't bother with any constant multipliers on n. They don't matter. The shape does, and in this case it is linear. As you reach infinity, it doesn't matter what the slope or offset was.
On Jan 11, 2017, at 02:16, Andy Jones <Andy.Jones@jameshall.co.uk> wrote:
Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. [wikipedia]
To be just as pedantic, you're supposed to use Big O-notation to denote the
*shape* of something as it reaches infinity. That's the same reason why we
don't bother with any constant multipliers on n. They don't matter. The shape
does, and in this case it is linear. As you reach infinity, it doesn't matter
what the slope or offset was.
That is an excellent explanation of something I only had a vague understanding of. I'm afraid "O(n-1)" was a very weak attempt at humour on my part...
Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>
Forget it: I'm stupid.
$ ruby -ce 'if a==b==c then puts 123 end'
-e:1: syntax error, unexpected ==
if a==b==c then puts 123 end
^
I wasn't aware that Ruby prevents this expression already via syntax.
Learn something new every day.
Cheers
robert
On Wed, Jan 11, 2017 at 12:10 PM, Robert Klemme <shortcutter@googlemail.com> wrote:
On Tue, Jan 10, 2017 at 11:15 PM, Die Optimisten > <inform@die-optimisten.net> wrote:
pure Ruby:
If a=b=c=d=e is ok, then
a==b==c==e should also workWell, it does "work". The question is more: what do you expect it to do?
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/
This looks useful... what do you use here to get the syntax tree?
Regards,
Marcus
Am 11.01.2017 um 13:52 schrieb Ryan Davis:
% rake debug R="a=b=c=42"
s(:lasgn, :a, s(:lasgn, :b, s(:lasgn, :c, s(:lit, 42))))
--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A
Actually, O(n-1) is exactly same with O(n).
At 2017-01-11 20:41:32, "Ryan Davis" <ryand-ruby@zenspider.com> wrote:
On Jan 11, 2017, at 02:16, Andy Jones <Andy.Jones@jameshall.co.uk> wrote:
Pardon my pedanticism, but: [b, c, d].all?{|i| i == a}
I suppose that makes it O(n-1) ?
To be just as pedantic, you're supposed to use Big O-notation to denote the *shape* of something as it reaches infinity. That's the same reason why we don't bother with any constant multipliers on n. They don't matter. The shape does, and in this case it is linear. As you reach infinity, it doesn't matter what the slope or offset was.
Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. [wikipedia]
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Marcus,
Looks like he's using some form of his gem RubyParser (
https://github.com/seattlerb/ruby_parser\). RubyParser.new.parse("a=b=c=42")
# => s(:lasgn, :a, s(:lasgn, :b, s(:lasgn, :c, s(:lit, *42*))))
On Wed, Jan 11, 2017 at 5:17 PM, 朱 <zhuran94@163.com> wrote:
Actually, O(n-1) is exactly same with O(n).
At 2017-01-11 20:41:32, "Ryan Davis" <ryand-ruby@zenspider.com> wrote:
>
>> On Jan 11, 2017, at 02:16, Andy Jones <Andy.Jones@jameshall.co.uk> wrote:
>>
>> Pardon my pedanticism, but: [b, c, d].all?{|i| i == a}
>>
>> I suppose that makes it O(n-1) ?
>
>To be just as pedantic, you're supposed to use Big O-notation to denote the *shape* of something as it reaches infinity. That's the same reason why we don't bother with any constant multipliers on n. They don't matter. The shape does, and in this case it is linear. As you reach infinity, it doesn't matter what the slope or offset was.
>
>> Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. [wikipedia]
>
>
>
>Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
><http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>