try to understand matching brackets & evaluation.

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1515 Views - Last Post: 30 January 2014 - 05:09 PM Rate Topic: -----

#16 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7731
  • View blog
  • Posts: 13,058
  • Joined: 19-March 11

Re: try to understand matching brackets & evaluation.

Posted 29 January 2014 - 03:20 PM

View Postandrewsw, on 29 January 2014 - 05:07 PM, said:

jon said:

One more thing that's not obvious: the result of the last evaluation is returned. This is not hugely weird, but it's certainly not obvious.

This is one thing that I really like, it's kinda cool ;)/>/>. I think there are other languages that do this as well(?).



I think it's pretty standard in lisps, like scheme:

(define (max x y)
(cond (( > x y) x)
      (else y)))



and in most functional languages. Scala, for example:


def max(x:Int, y:Int): Int = {
  if (x>y) x
  else y
}



Quote

Quote

In Ruby only false and nil are falsey. Everything else is truthy (yes, even 0 is truthy).

.. this must catch a lot of people out as well.


Frankly, I kind of hate truthy-falsey evaluation. Must be my java background, but I really get tired of trying to remember which things are going to be false in this language. PHP is about the worst, of course, but that's usually true.

This post has been edited by jon.kiparsky: 29 January 2014 - 03:21 PM

Was This Post Helpful? 1
  • +
  • -

#17 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3459
  • View blog
  • Posts: 11,711
  • Joined: 12-December 12

Re: try to understand matching brackets & evaluation.

Posted 29 January 2014 - 03:29 PM

Coming from Javascript I really like truthy/falsy-ness. What catches people out though (in JS and probably elsewhere) is a 0 value. I think JS could borrow the suffix idea from Ruby to use something like:

if (!whatever^) {

I've chosen a character (^) at random to mean any falsy value other than 0.

Anyway, I'm going off-topic now ;)

This post has been edited by andrewsw: 29 January 2014 - 03:38 PM

Was This Post Helpful? 1
  • +
  • -

#18 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 904
  • View blog
  • Posts: 3,165
  • Joined: 12-May 09

Re: try to understand matching brackets & evaluation.

Posted 29 January 2014 - 03:49 PM

View Postandrewsw, on 29 January 2014 - 05:07 PM, said:

Quote

The double use of key was very confusing.

Yes, that threw us all.

jon said:

One more thing that's not obvious: the result of the last evaluation is returned. This is not hugely weird, but it's certainly not obvious.

This is one thing that I really like, it's kinda cool ;)/>. I think there are other languages that do this as well(?).

Clojure does something like this.

Quote




Quote

In Ruby only false and nil are falsey. Everything else is truthy (yes, even 0 is truthy).

from here

.. this must catch a lot of people out as well.

0 as an object doesn't need any special meaning if you have a boolean type already, so it makes sense for 0 to be truthy because it's just another object with a value, like -1, 1, 2...

View Postjon.kiparsky, on 29 January 2014 - 05:20 PM, said:

Frankly, I kind of hate truthy-falsey evaluation.

I dig it, probably because of my Java background.

Quote

PHP is about the worst, of course, but that's usually true.

QFT
Was This Post Helpful? 3
  • +
  • -

#19 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7731
  • View blog
  • Posts: 13,058
  • Joined: 19-March 11

Re: try to understand matching brackets & evaluation.

Posted 29 January 2014 - 03:58 PM

View Postxclite, on 29 January 2014 - 05:47 PM, said:

0 as an object doesn't need any special meaning if you have a boolean type already, so it makes sense for 0 to be truthy because it's just another object with a value, like -1, 1, 2...


That's the problem, though. There are too many ways to look at it that make sense. Ruby's way makes sense in terms of objects and what they're like (what, this thing is suddenly false because I subtracted seventeen from it??? WTF???), javascript's approach is sensible from the point of view of, well, zero is false. PHP's point of view is sensible if, well, if you're on crack.

Then there's python, which is special in its own way:

>>> False * 45
0
>>> True * 45
45
>>> 




Ummm... say what?


Who wants to keep track of all that? That's why I like the Java approach: if it's not a boolean, it's not true and it's not false, and that's that.
Was This Post Helpful? 2
  • +
  • -

#20 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,433
  • Joined: 28-November 09

Re: try to understand matching brackets & evaluation.

Posted 29 January 2014 - 05:42 PM

So what is the actual use case you're going for here? If it's not immediately obvious what you're trying to do, chances are you're going about it the wrong way.

The best I can figure is you're trying to make a bracket matcher, in which case that's a really odd way to go about it.

As far as Ruby being odd, it really depends on the context. It behaves 99% of the time exactly how you think it will if you know the behavior of enumerable.
Was This Post Helpful? 0
  • +
  • -

#21 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: try to understand matching brackets & evaluation.

Posted 29 January 2014 - 07:05 PM

Just a program that determines the validity of any particular bracket notation combination inputed as a string. That's all this program was made for.
Was This Post Helpful? 0
  • +
  • -

#22 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,433
  • Joined: 28-November 09

Re: try to understand matching brackets & evaluation.

Posted 29 January 2014 - 07:29 PM

It's one of those things that sounds really esoteric. Unless there's a reason, I really wouldn't bother with something like this as a gem like Nokogiri will parse html tags and other things like this probably exist.

The difference between a Junior and Senior developer is that a Junior will always try and make it without looking, and the senior will look everywhere before making it.

I'm not saying don't experiment, I'm saying think through things in a larger context.
Was This Post Helpful? 0
  • +
  • -

#23 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7731
  • View blog
  • Posts: 13,058
  • Joined: 19-March 11

Re: try to understand matching brackets & evaluation.

Posted 29 January 2014 - 07:37 PM

That's true, but if you're trying to learn the language, it's worth implementing stuff like this to figure out how things go. There's one good reason to reinvent wheels, and that's if you're trying to learn how to make a nice round wheel.
Once you've done that, you will usually find it's simpler and more effective to source your wheels from somebody who specializes in making wheels. (although in the case of bicycles, this isn't actually true: it's generally cheaper and easier to make a good wheel from parts than to buy one)
Was This Post Helpful? 0
  • +
  • -

#24 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: try to understand matching brackets & evaluation.

Posted 29 January 2014 - 10:09 PM

Exactly. At this point I am simply trying absorb coding principals.
Was This Post Helpful? 1
  • +
  • -

#25 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 904
  • View blog
  • Posts: 3,165
  • Joined: 12-May 09

Re: try to understand matching brackets & evaluation.

Posted 30 January 2014 - 11:39 AM

This is also a common interview question.
Was This Post Helpful? 0
  • +
  • -

#26 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,433
  • Joined: 28-November 09

Re: try to understand matching brackets & evaluation.

Posted 30 January 2014 - 05:09 PM

View Postxclite, on 30 January 2014 - 12:39 PM, said:

This is also a common interview question.


Really now? Well go figure. The more you know.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2