Skip to content

Instantly share code, notes, and snippets.

@soc
soc / gist:1224225
Created September 17, 2011 18:54
Scala migration-update
% ./scala
scala> collection.mutable.Buffer(1,2,3,4) - 3
res0: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 4)
scala> List(1, 2, 3, 4).scanRight(0)(_ + _)
res1: List[Int] = List(10, 9, 7, 4, 0)
% ./scala -Xmigration
Testing interpreter and backend
No such file or class on classpath: Test
testing: [...]/files/run/t4990-mig-28.scala [FAILED]
No such file or class on classpath: Test
testing: [...]/files/run/t4990-mig.scala [FAILED]
No such file or class on classpath: Test
testing: [...]/files/run/t4990-mig-29.scala [FAILED]
Directory '/home/soc/Entwicklung/scala-new/test/files/script' not found
3 of 3 tests failed (elapsed time: 00:00:17)
@soc
soc / gist:1240991
Created September 25, 2011 19:14
Thoughts about Units of Measurement
Units of Measurements
Features:
- Computations: 4s + 5s == 9s
- Conversions
between dimensions: 5m + 10cm == ...
between metric systems: 5ft + 10cm = ...
- Dimensions: 5m * 5m == 25m² || 10m/s * 1s == 10m || 10m² / 5m == 5m
- Everything above combined!
val length: Length[Int] = 2 m
val time: Time[Int] = 1 s
val speed = length * 2 / time
Exception in thread "main" java.lang.StackOverflowError
at scala.math.Numeric$IntIsIntegral$class.fromInt(Numeric.scala:49)
at scala.math.Numeric$IntIsIntegral$.fromInt(Numeric.scala:55)
at scala.math.Numeric$IntIsIntegral$.fromInt(Numeric.scala:55)
at scala.math.Numeric$class.one(Numeric.scala:199)
at scala.math.Numeric$IntIsIntegral$.one(Numeric.scala:55)
at metascala.Units$Quantity.<init>(Units.scala:39)
at metascala.Units$Quantity.<init>(Units.scala:39)
at metascala.Units$Quantity.<init>(Units.scala:39)
at metascala.Units$Quantity.<init>(Units.scala:39)
val mass: Mass[Double] = kg[Double] * 4.0
val length: Length[Int] = m[Int] * 5
val time: Time[Int] = s[Int]
val temperature: Temperature[BigDecimal] = k[BigDecimal] * BigDecimal("22.22")
val speed = length / time
val area = length * length
val volume: Volume[Int] = m[Int] * m[Int] * m[Int] * 23
val accel = m[Double] * 10.0 / (s[Double] * s[Double])
val length = (2.0 m) * 5.0
val length2 = length + (2.0 m) * 5.0
val time = (5.0 s) * 5.0
val speed = length2 * 2.0 / time
val mass = (5.0 kg) + (5.0 kg)
case class Quantity[M <: MInt, KG <: MInt, S <: MInt, A <: MInt, K <: MInt, Mol <: MInt, CD <: MInt, T : Numeric](value: T) {
type This = Quantity[M, KG, S, A, K, Mol, CD, T]
def +(m: This) = Quantity[M, KG, S, A, K, Mol, CD, T](implicitly[Numeric[T]].plus(value, m.value))
def -(m: This) = Quantity[M, KG, S, A, K, Mol, CD, T](implicitly[Numeric[T]].minus(value, m.value))
def *[M2 <: MInt, KG2 <: MInt, S2 <: MInt, A2 <: MInt, K2 <: MInt, Mol2 <: MInt, CD2 <: MInt](m : Quantity[M2, KG2, S2, A2, K2, Mol2, CD2, T]) = Quantity[M + M2, KG + KG2, S + S2, A + A2, K + K2, Mol + Mol2, CD + CD2, T](implicitly[Numeric[T]].times(value, m.value))
def /[M2 <: MInt, KG2 <: MInt, S2 <: MInt, A2 <: MInt, K2 <: MInt, Mol2 <: MInt, CD2 <: MInt](m : Quantity[M2, KG2, S2, A2, K2, Mol2, CD2, T]) = Quantity[M - M2, KG - KG2, S - S2, A - A2, K - K2, Mol - Mol2, CD - CD2, T](implicitly[Numeric[T]].times(value, m.value))
def apply(v: T) = Quantity[M, KG, S, A, K, Mol, CD, T](implicitly[Numeric[T]].tim
package metascala.example
object UnitExample extends App {
import metascala.Units._
val length = 5.0 *~ (2.0 m)
val length2 = length + (2.0 m) *~ 5.0
val time = (5.0 s) *~ 5.0
val speed = length2 *~ 2.0 /~ time /~ 4.0
val mass = (5.0 kg) + (5.0 kg)
package metascala
object Units {
import Integers._
import Utils._
import Addables._
import Subtractables._
trait Unit {
type M <: MInt