Last active
December 26, 2015 02:29
-
-
Save orionll/7079552 to your computer and use it in GitHub Desktop.
This file contains 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
object World extends App { | |
val pnt = Point(10, 10) | |
val str: String = pnt(toString) | |
val x: Int = pnt(getX) | |
val pnt2: Point = pnt(setX)(20) | |
val pnt3: Point = pnt(setXY)(20, 19) | |
println(str) | |
println(x) | |
println(pnt2) | |
println(pnt3) | |
def toString(point: Point): String = point.toString | |
/** Sets only x */ | |
def setX(point: Point)(newX: Int) = point.copy(x = newX) | |
/** Sets x and y */ | |
def setXY(point: Point)(newXY: (Int, Int)): Point = point.copy(x = newXY._1, y = newXY._2) | |
/** Gets x */ | |
def getX(point: Point): Int = point.x | |
} | |
case class Point(x: Int, y: Int) { | |
def apply[A](f: Point => A): A = f(this) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment