tag:blogger.com,1999:blog-7317245822288637185.post8961007953941577308..comments2023-03-28T10:19:42.467-07:00Comments on Graham Hacking Scala: Scala: Beyond Hello WorldGraham Leahttp://www.blogger.com/profile/07862939329898536954noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-7317245822288637185.post-13630848110521656522010-11-09T18:09:11.376-08:002010-11-09T18:09:11.376-08:00Hey VivaceVivo, thanks heaps for posting your solu...Hey VivaceVivo, thanks heaps for posting your solution. I hadn't seen that '*' operator on String before - very cool! It seems we've taken fairly different approaches to the problem. I guess the power in the APIs allows us to take many different paths and each of them can still produce succinct, readable code.Grazerhttps://www.blogger.com/profile/00435359638152521056noreply@blogger.comtag:blogger.com,1999:blog-7317245822288637185.post-43405888673696109612010-11-09T14:20:18.840-08:002010-11-09T14:20:18.840-08:00Hi Graham,
nice Blog!
I'm a Scalastarter and -...Hi Graham,<br />nice Blog!<br />I'm a Scalastarter and - asumably - about as skillful as you were a year ago...<br />First thing I noticed when scanning your implementation was this feeling: "there must be a way to express this shorter"<br />anyway - I made some improvements:<br /><br />1. in Main:<br />...<br />println(("."*30))<br />val w2 = wrap2(s, 30)<br />w2.foreach(println)<br /><br />2. now my wrap:<br />def wrap2(stringToWrap : String, lineWidth : Int):List[String] = {<br /> if(stringToWrap.isEmpty){<br /> Nil<br /> }else if(stringToWrap.length()<=lineWidth){<br /> List(stringToWrap)<br /> }else{<br /> val parts = stringToWrap.splitAt(lineWidth+1) // parts = lineMax, restOfString <br /> val blankIndex = parts._1.lastIndexOf(" ") + 1;<br /> val line = parts._1.splitAt(if(blankIndex>0)blankIndex else lineWidth);<br /> val restOfString = line._2.concat(parts._2)<br /> line._1 :: wrap2(restOfString, lineWidth);<br /> }<br /> }<br /><br />----<br />and still I hope this can be shortened without becomming unreadable.VivaceVivohttps://www.blogger.com/profile/16671347017556570969noreply@blogger.com