Scala

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Scala

Post by ismetteren »

*Edit* i have changed the code for the Vector2 class. It is now a case class.

I was going to post this in the code snippets forum, as a response to zied(s or 's... im not a native english speaker :P) 2d vector class, but i realized it was more a post about scala than 2d vectors.

Anyways, i have tried to port zied's(im just gonna go with one of them here ;) ) 2d vector class to scala. The biggest difference is that this one is immutable, since scala partly is a functional language. I think it is pretty cool it can be done in so few lines:

Code: Select all

case class Vector2(val x: Double, val y: Double) {
	import scala.Math
	
	val magnitude = Math.sqrt(Math.pow(x, 2)+Math.pow(y, 2))
	def normalize = {if (magnitude != 0) Vector2(x/magnitude, y/magnitude) else Vector2(1,0)}
	
	def unary_- = {Vector2(-x, -y)}
	
	def + (v: Vector2) = {Vector2(x+v.x, y+v.y)}
	def - (v: Vector2) = {Vector2(x-v.x, y-v.y)}
	def dot (v: Vector2) = {(x*v.x) + (y*v.y)}
	
	def * (s: Double) = {Vector2(x*s, y*s)}
	def / (s: Double) = {Vector2(x/s, y/s)}
}
I have made this class that uses all the functions:

Code: Select all

object Test {
	def main(args: Array[String]) {
		val v1 = new Vector2(1,3)
		
		val s1 = v1.magnitude
		val v2 = v1.normalize
		
		val b1 = v1 == v2
		val b2 = v1 != v2
		
		val v3 = -v2
		
		val v4 = v2 + v3
		val v5 = v3 - v4
		val v6 = v4 dot v5
		
		val s2 = v6 * s1
		val s3 = v6 / s2
	}
}
Scala can guess what type a value should have, so you don't have to explicitly type it. Here every value i prefaced with v if it is a Vector2, s if it is a scalar(Double) and b if it is a boolean.

Scala do not have operator overloading, but you can give functions all sorts of crazy names, and a function that takes one argument(and i think it also has to return something) can be called like this: [object] [functionName] [argument] instead of [object].[functionName]([argument])

A funny thing is, that i could have implemented magnitude like this: def magnitude = {Math.sqrt(Math.pow(x, 2)+Math.pow(y, 2))} and the class could be used in the exact same way, the only difference would be, that magnitude would be calculated every time it was called. Actually i first did it like that.

The way you do unary operators (like -) is a bit strange. You have to preface it with unary_ but you can only do that on a small amount of selected symbols.

One of the best features of scala is that it compiles to java bytecode, and runs everywhere java does. Java libraries can also be called easily from scala and scala code can be called from Java, but sometimes it gets a bit tricky when you do that.

You can read moare about scala here: http://www.scala-lang.org/
Image ImageImage Image
Post Reply