Skip to content

Instantly share code, notes, and snippets.

@krishnanraman
Created January 15, 2013 03:49
Show Gist options
  • Select an option

  • Save krishnanraman/4535898 to your computer and use it in GitHub Desktop.

Select an option

Save krishnanraman/4535898 to your computer and use it in GitHub Desktop.
scalaz employee monoid
import scalaz.Monoid
case class EmpMonoid(e:Set[Employee]) extends Monoid[Employee] {
override val zero:Employee = Employee(0,0,0,"emp0")
override def append(a:Employee, b: =>Employee):Employee = {
if(a.age > b.age) a else
if(b.age > a.age) b else
if(a.tenure > b.tenure) a else
if(b.tenure > a.tenure) b else
if (a.salary > b.salary) b else
if(b.salary > a.salary) a else
if( math.random > 0.5) a else b
}
val theUnluckyOne:Employee = e.foldLeft(zero)((a:Employee, b:Employee)=>append(a,b))
}
scala> EmpMonoid( empDB.toSet).theUnluckyOne
res12: Employee = Employee(35,6,85079,emp7435)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment