Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem 6: [Language] #16

Open
jamiephillips0000 opened this issue Dec 4, 2014 · 8 comments
Open

Problem 6: [Language] #16

jamiephillips0000 opened this issue Dec 4, 2014 · 8 comments
Labels

Comments

@jamiephillips0000
Copy link

i keep getting nailed by "to" and "until"

List.range(0, 100).sum << this is 0 - 99 inclusive
(0 to 100).sum << this is 0 - 100 inclusive

Is there any easy way of distinguishing these - some sort of rule of thumb?

@octonato octonato changed the title Problem 6 : Language Problem 6: [Language] Dec 4, 2014
@octonato
Copy link
Contributor

octonato commented Dec 4, 2014

Hi Jamie,

Well, I think that rule is the english language! :-)
'to' is inclusive, 'until' is exclusive
(and now I'm pretending to teach English to a native speaker)

The question is how to remember that List.range() is equivalent to 'until' and not 'to'?
My answer is: just check the docs.

I never use List.range() and I confess that I would expect it to be inclusive. I just learn that this is not the case. Thanks!

I would probably check the docs if I was about to use it or do a small check using the REPL.

The problem of doing that in the context of Project Euler is that you don't immediately see you picked the wrong one. Because the test may be failing because of another reason and not because you have one more or one less item in your range.

So, if you are looking for a rule of thumb, I can only say in doubt ask the REPL. :-)

@mverbist
Copy link

mverbist commented Dec 4, 2014

        val sum1 = (1 to 100).sum
        sum1 * sum1 - (1 to 100).map(x => x*x).sum

First create a Range (1 to 100), and calculate the sum of that range.
Then create a Range (1 to 100), transform it with map into a Seq of squares, and calculate the sum of that range.
Finally subtract the 2

@samdebacker
Copy link
Member

@jamiephillips0000 maybe this might help in remembering the difference between to and until.
Look at the pseudo code of the typical while and repeat loops:

i := 1
WHILE i != 100 DO
    ...
    i := i + 1
END

and

i := 1
REPEAT
    ...
    i := i + 1
UNTIL i = 100

I'm pretty sure you have no doubt that the body of the while loop is last executed with i = 100 (INCLUSIVE), and that the body of the repeat-until loop is last executed with i = 99 (EXCLUSIVE the 100).

So as a rule of thumb: 1 to 100 is like while ... loop (inclusive) and 1 until 100 is like repeat ... until (exclusive).

@jamiephillips0000
Copy link
Author

Hi Sam

  1. Just for clarity I don't have a problem understanding the semantics differences between "to" and "until" ;-)
  2. Scala goes a long way towards cleaning up and making useful improvements on Java e.g. The Collections API's are much better with more descriptive names. In this instance a more descriptive name for "List.range" would be e.g. List.rangeUntil (or something similar)
  3. To summarize my options are
    a) Learn the difference and remember it
    b) Like Renato said use the REPL
    Thanks for the response guys!

@jamiephillips0000
Copy link
Author

So - just fooling around
val a= Array(1,2,3,4,5)
a.takeWhile(_ %4 == 0 ) > res11: Array[Int] = Array()

Just laughing about this - would have expected this to have Array(4) as a result ;-)

@eloots
Copy link
Contributor

eloots commented Dec 8, 2014

Jamie,

This works as advertised; for the first element in the Array, the predicate _ % 4 == 0 is false. Hence, takeWhile immediately ‘stops’ and returns an empty Array.

On the other hand, an example of a case where the result is non-empty:

scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)

scala> a.takeWhile(el => el < 4)
res7: Array[Int] = Array(1, 2, 3)

Regards, Eric

On 8-dec.-2014, at 14:20, Jamie Phillips notifications@github.com wrote:

So - just fooling around
val a= Array(1,2,3,4,5)
a.takeWhile(_ %4 == 0 ) > res11: Array[Int] = Array()

Just laughing about this - would have expected this to have Array(4) as a result ;-)


Reply to this email directly or view it on GitHub #16 (comment).

@jamiephillips0000
Copy link
Author

Hi Eric
I know - if you take my comment in combination with Sams explanation of while above then - in some way I guess I was laughing at the result - no biggie

@eloots
Copy link
Contributor

eloots commented Dec 8, 2014

Hi Jamie,

Got it ;-)

Eric

On 8-dec.-2014, at 15:04, Jamie Phillips notifications@github.com wrote:

Hi Eric
I know - if you take my comment in combination with Sams explanation of while above then - in some way I guess I was laughing at the result - no biggie


Reply to this email directly or view it on GitHub #16 (comment).

@eloots eloots added the Language label Jan 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants