The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Chapter 3 page 90 length question

2 replies on 1 page. Most recent reply: Sep 13, 2010 7:57 AM by Douglas Viltrakis

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Douglas Viltrakis

Posts: 10
Nickname: douglasv
Registered: Aug, 2010

Chapter 3 page 90 length question Posted: Sep 1, 2010 5:42 AM
Reply to this message Reply
Advertisement
at bottom of page

def widthOfLength(s: String) = s.length.toString.length

before invoking toString, is the cryptic reference
by the console to the element, actually an address to the element on the disk or memory?Console returns address or value

1)So is "s.length" the length of these addresses?

2)And then is toString.length the length of the human readable string? If so, why call both?


Graham Kelly

Posts: 6
Nickname: gmkelly
Registered: Jul, 2010

Re: Chapter 3 page 90 length question Posted: Sep 13, 2010 2:24 AM
Reply to this message Reply
>
> def widthOfLength(s: String) = s.length.toString.length
>

Not having the book immediately to hand, I can't remember the context of the code, but the code above will give you the powers of 10 of the length.

For example

val s = "Here is a sentence"
val sLen = s.length
val pow = sLen.toString.length

gives: sLen of 18, but a pow of 2, so if, for example, I were formatting a column of output, I would know how much space to allocate for the largest value of 's'.

Douglas Viltrakis

Posts: 10
Nickname: douglasv
Registered: Aug, 2010

Re: Chapter 3 page 90 length question Posted: Sep 13, 2010 7:57 AM
Reply to this message Reply
thank you

Flat View: This topic has 2 replies on 1 page
Topic: I want exercises Previous Topic   Next Topic Topic: Hardcopy book quality

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use