Monday, October 7, 2013

Play Framework and Functional Programming

Check this post if you are interested in the Play Framework and how functional programming is used in a web framework. This is an example of composable data manipulation:

val linkedInProfiles: List[Profile] = …
// 1: Keep only profiles of young people
val youngProfiles: List[Profile] = linkedInProfiles.filter (p => p.age < 25)
// 2: Group these profiles by skill
val skillToProfile: List[(Skill, Profile)] = for {
p <- youngProfiles
s <- p.skills
} yield (s, p)
val profilesBySkill: Map[Skill, Profile] =
skillToProfile
.groupBy{ case (skill, profile) => skill) }
.mapValues{ case (skill, profiles) => profiles }
// 3: For each skill determine the number of profiles
val skillsWithPopularity: List[(Skill,Int)] =
profilesBySkill.mapValues( profiles => profiles.count )
// 4: Order results by number of profiles and take top 10 skills
val top10Skills: List[(Skill, Int)] =
skillsWithPopularity
.sortBy{ case (skill, count) => - count}
.take(10)

If you are confused by the Iteratees mentioned in previous post, take a look at this post.

No comments:

Post a Comment