def scale
(value
: A, scale
: B
): A
}
def scale
(value
: Int, scale
: Int
) = value
* scale
}
def scale
(value
: Int, scale
: Double
) = (value.
toDouble * scale
).
toInt }
def scale
(value
: Double, scale
: Double
) = value
* scale
}
def scale
(value
: Double, scale
: Int
) = value
* scale
}
// TODO: Provide more instances
}
class Matrix
[A
](val rowsCount
: Int,
val columnsCount
: Int
) (implicit numOps
: Numeric
[A
], classTag
: ClassTag
[A
]) {
def apply
(coords
: (Int, Int
)) = { val (row, column
) = coords
data(row * columnsCount + column)
}
def update
(coords
: (Int, Int
), newVal
: A
) = { val (row, column
) = coords
data.update(row * columnsCount + column, newVal)
}
def scaledBy
[B
](scale
: B
)(implicit scalesBy
: ScalesBy
[A, B
]) = { val result
= new Matrix
(rowsCount, columnsCount
) for (r
<-
0 until rowsCount
; c
<-
0 until columnsCount
) { val scaledCell
= scalesBy.
scale(cell, scale
) result.update((r, c), scaledCell)
}
result
}
}
def main
(args
: Array
[String
]) = { // your code goes here
val intMx
= new Matrix
[Int
](3,
2) val doubleMx
= new Matrix
[Double
](3,
2) intMx((1, 2)) = 3
doubleMx((1, 2)) = 3
println("Int cell before: " + intMx((1, 2)))
println("Double cell before: " + doubleMx((1, 2)))
val scaledIntMx
= intMx.
scaledBy(2.5) println("Int cell scaled by 2.5: " + scaledIntMx((1, 2)))
val scaledDoubleMx
= doubleMx.
scaledBy(2.5) println("Double cell scaled by 2.5: " + scaledDoubleMx((1, 2)))
}
}
aW1wb3J0IHNjYWxhLm1hdGguTnVtZXJpYwppbXBvcnQgc2NhbGEucmVmbGVjdC5DbGFzc1RhZwoKdHJhaXQgU2NhbGVzQnlbQSwgQl0gewoJZGVmIHNjYWxlKHZhbHVlOiBBLCBzY2FsZTogQik6IEEKfQoKb2JqZWN0IFNjYWxlc0J5SW5zdGFuY2VzIHsKICBpbXBsaWNpdCB2YWwgc2NhbGVzSW50QnlJbnQgPSBuZXcgU2NhbGVzQnlbSW50LCBJbnRdIHsKICAgIGRlZiBzY2Fs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