Skip to content

Instantly share code, notes, and snippets.

@krishnanraman
Created January 15, 2013 01:25
Show Gist options
  • Save krishnanraman/4535192 to your computer and use it in GitHub Desktop.
Save krishnanraman/4535192 to your computer and use it in GitHub Desktop.
Employee Monoid
case class Monoid(e:Set[Employee]) {
def plus(a:Employee,b: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 identity = Employee(0,0,0,"id")
val theUnluckyOne = e.foldLeft(identity)((a,b)=>plus(a,b))
}
scala> Monoid(empDB.toSet).theUnluckyOne
res2: Employee = Employee(35,6,85079,emp7435)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment