The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Last example in chapter 3 issues..

1 reply on 1 page. Most recent reply: Aug 24, 2008 9:33 PM by Bill Venners

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 1 reply on 1 page
Alan Mortensen

Posts: 5
Nickname: 57845
Registered: Aug, 2008

Last example in chapter 3 issues.. Posted: Aug 22, 2008 1:33 PM
Reply to this message Reply
Advertisement
Why do you get the length of every number in text form twice? IE why call line.length.toString.length twice for every line?

It would seem to me that there has to be at least one simple way to avoid this. I'm not sure the best way to do it but it seems like it should be simple.. Step through widths while you step through lines in the for loop. One way would be to output a list of tuples when using lines.map that includes the width, the length, and the line.

I guess part of me just really rankles at the recalculations and all the walking of the lists. I feel it makes the functional approach look bad. It definitely didn't make me want to switch over to a more functional approach (though I have other reasons to switch on my own).

Alan


Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Last example in chapter 3 issues.. Posted: Aug 24, 2008 9:33 PM
Reply to this message Reply
Hi Alan,

Here's how I'd avoid calculating widthOfLength twice:


import scala.io.Source

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

if (args.length > 0) {
val lines = Source.fromFile(args(0)).getLines.toList
val widths = lines.map(widthOfLength)
val maxWidth = widths.reduceLeft((a, b) => a.max(b))
val zipped = lines zip widths
for ((line, widthOfLength) <- zipped) {
val numSpaces = maxWidth - widthOfLength
val padding = " " * numSpaces
print(padding + line.length +" | "+ line)
}
}
else
Console.err.println("Please enter filename")


You can zip the widths list together with the lines list, then use a pattern in the for expression to pull them apart. The reason I didn't do something like that in the book was that we hadn't explained patterns yet. We try very hard to show each new thing based on things that came before, and just introduce one new idea at a time, where possible.

The other thing to consider, though, is that although I've reduced by half the number of times I calculate the width of length of each line here, I've at the same time increased the memory used by the script, because I have added the zipped list. I'd doubt there would be any perceptible difference between the performance of either script for most files.

Flat View: This topic has 1 reply on 1 page
Topic: scala sort performance? Previous Topic   Next Topic Topic: PrePrint v4 released

Sponsored Links



Google
  Web Artima.com   

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