16 Aug 2012 22:40
val vs def
hi there,
when writing code, i often have the choice between this:
def rotatedLeft(xy: XY) = {
def update = {
def xyRotatedLeft = xy -> myWheels(xy).rotatedLeft
def touchedRotatedRight = xy.touched.flatMap(touched =>
myWheels.get(touched).map(wheel => touched -> wheel.rotatedRight))
xyRotatedLeft :: touchedRotatedRight
}
Wheels(myWheels ++ update)
and this:
def rotatedLeft(xy: XY) = {
val update = {
val xyRotatedLeft = xy -> myWheels(xy).rotatedLeft
val touchedRotatedRight = xy.touched.flatMap(touched =>
myWheels.get(touched).map(wheel => touched -> wheel.rotatedRight))
xyRotatedLeft :: touchedRotatedRight
}
Wheels(myWheels ++ update)
is there any reason to prefer one over the other if every method is only
called once/every val is only used once?
RSS Feed