This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def count8(n: Int) = { | |
| @annotation.tailrec def count8Acc(n: Int, acc: Int): Int = { | |
| n match { | |
| case 0 => acc | |
| case _ => count8Acc(n/10 , if (n %10==8) acc+1 else acc) | |
| } | |
| count8Acc(8188, 0) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def count8(n: Int) = { | |
| @annotation.tailrec def count8Acc(n: Int, acc: Int): Int = { | |
| n match { | |
| case 0 => acc | |
| case _ => count8Acc(n/10 , if (n %10==8) acc+1 else acc) | |
| } | |
| } | |
| count8Acc(8188, 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //counts occurences of 8 as a digit, except that an 8 with another 8 immediately to its left counts double, so //8818 yields 4. | |
| def count8(n: Int) = { | |
| @annotation.tailrec def count8Acc(n: Int, acc: Int): Int = { | |
| n match { | |
| case 0 => acc | |
| case _ => count8Acc(n/10 , if((n/10)%10==8 && n%10==8) acc+2 else if (n%10==8) acc+1 else acc) | |
| } | |
| } | |
| count8Acc(n, 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| The java.awt.Rectangle class has useful methods translate and grow that are unfortunately absent from classes such as java.awt.geom.Ellipse2D. | |
| In Scala, you can fix this problem. Define a trait RectangleLike with concrete methods translate and grow. | |
| Provide any abstract methods that you need for the implementation, so that you can mix in the trait like this: | |
| val egg = new java.awt.geom.Ellipse2D.Double( 5, 10, 20, 30) with RectangleLike | |
| egg.translate( 10, -10) | |
| egg.grow( 10, 20) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait RectangleLike { | |
| def getX(): Double | |
| def getY(): Double | |
| def getHeight(): Double | |
| def getWidth(): Double | |
| def setLocation(x: Double, y: Double, width: Double, height: Double): Unit | |
| def translate(dx: Double, dy: Double){setLocation(getX()+dx, getY()+dy, getWidth(), getHeight)} | |
| def grow(horizontal: Double, vertical: Double){setLocation(getX(), getY(), getWidth()+horizontal, getHeight()+vertical)} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait RectangleLike { | |
| self: RectangularShape => | |
| def getX(): Double | |
| def getY(): Double | |
| def getHeight(): Double | |
| def getWidth(): Double | |
| def translate(dx: Double, dy: Double){setFrame(getX()+dx, getY()+dy, getWidth(), getHeight)} | |
| def grow(horizontal: Double, vertical: Double){setFrame(getX(), getY(), getWidth()+horizontal, getHeight()+vertical)} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait RectangleLike { | |
| self: RectangularShape => | |
| def translate(dx: Double, dy: Double){setFrame(getX()+dx, getY()+dy, getWidth(), getHeight)} | |
| def grow(horizontal: Double, vertical: Double){setFrame(getX(), getY(), getWidth()+horizontal, getHeight()+vertical)} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class OrderedPoint extends java.awt.Point with Ordered[OrderedPoint] { | |
| def compare(that: OrderedPoint):Int = { | |
| if(this.x<that.x) -1 | |
| else if(this.x==that.x && this.y< that.y) -1 | |
| else 0 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def buildUri(nameValuePairs: java.util.ArrayList[BasicNameValuePair], uri: Uri): Uri = { | |
| val builder = uri.buildUpon | |
| nameValuePairs.foreach((p: BasicNameValuePair) => builder.appendQueryParameter(p.getName, p.getValue)) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val httpRequest = verb match { | |
| case GET => new HttpGet | |
| case DELETE => new HttpDelete | |
| case POST => new HttpPost | |
| case PUT => new HttpPut | |
| } | |
| httpRequest match { | |
| case _: HttpGet | _: HttpDelete => attachUriWithQuery(httpRequest, uri.get, params) | |
| case x: HttpPost => { |