scalaz/scalaz — pull request #48 by miguel-negrao (2011-11-18 17:09:09+00:00)
Title: Add Group typeclass

Added the Group typeclass.

Added instances for most numeric types which are groups: Short, Long, Int, Float, Double and Unit

Added multiplicative instances for Float and Double as instances of Group

I couldn't really come up with any non-numeric use of Group, so it might be the case that this only makes sense for numeric types in practice.

Couldn't find a file for Map in the typeclass instances. Is it going to be implemented ? I tried to implement it but didn't manage to get it compiling...

This is my first experience of trying to write typeclasses, so I might have made mistakes somewhere even if it seems to work in my tests. Please check carefully.

Examples:

import scalaz._; import Scalaz._; import Tags._

scala> 2.0.inverse
res1: Double = -2.0

scala> Tag[Double,Multiplication](2.0).inverse
res2: scalaz.package.@@[Double,scalaz.Tags.Multiplication] = 0.5

scala> some(2).inverse
res3: Option[Int] = Some(-2)

scala> { x:Double => x +1 }.inverse
res4: Double => Double =

scala> res4.apply(1)
res5: Double = -2.0

scala> ({ x:Int => x +1 } |-| { x:Int => x + 2 }).apply(1)
res1: Int = -1

scala> (2,3)|-| (4,5)
res0: (Int, Int) = (-2,-2)

scala> (2,3,3) |-| (4,5,4)
res1: (Int, Int, Int) = (-2,-2,-1)