Skip to content

Instantly share code, notes, and snippets.

View krishnanraman's full-sized avatar

Krishnan Raman krishnanraman

View GitHub Profile
@krishnanraman
krishnanraman / gist:4535171
Created January 15, 2013 01:22
Min tuple
scala> empDB.minBy(e=>(-e.tenure,-e.age,e.salary))
res1: Employee = Employee(35,6,85079,emp7435)
@krishnanraman
krishnanraman / gist:4535192
Created January 15, 2013 01:25
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
}
@krishnanraman
krishnanraman / gist:4535898
Created January 15, 2013 03:49
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
@krishnanraman
krishnanraman / gist:4551886
Created January 16, 2013 23:09
First key that maps to a value in a scala map
scala> val m = Map(1->"one",2->"one",3->"two")
scala> m.filter(pair=>pair._2 == "one").head._1
res32: Int = 1
@krishnanraman
krishnanraman / SpokenEnglish.scala
Created January 24, 2013 00:26
Convert integers in the interval [0-1 BILLION] to spoken english & vice versa
/**
Convert integers in the interval [0-1 BILLION] to spoken english & vice versa
examples -
six hundred ninety one million one hundred forty five thousand eight hundred twenty five is 691145825
six hundred ninety one million one hundred fifty eight thousand one hundred seventy is 691158170
six hundred ninety one million one hundred seventy thousand five hundred fifteen is 691170515
six hundred ninety one million one hundred eighty two thousand eight hundred sixty is 691182860
six hundred ninety one million one hundred ninety five thousand two hundred five is 691195205
six hundred ninety one million two hundred seven thousand five hundred fifty is 691207550
def f[T](a:Map[String,T],b:Map[String,T]) = {
val res= (a.keys++b.keys).toList.sorted.mkString(",")
if (res.length == 0) None else Some(res)
}
@krishnanraman
krishnanraman / USPopulation
Last active June 13, 2017 03:47
Goal: Find THE FASTEST GROWING COUNTY IN THE UNITED STATES over the 1969-2011 timeframe.
Goal: Process the 12 million plus records
from: http://seer.cancer.gov/popdata/download.html
using: a Scala API atop Cascading, aka SCALDING ( Inventors: Avi Bryant, Oscar Boykin, Argyris )
to find:
THE FASTEST GROWING COUNTY IN THE UNITED STATES over the 1969-2011 timeframe.
-----------------------------------------------------------------------------
RESULTS: Scroll to the very bottom.
First, the scalding source...
---
@krishnanraman
krishnanraman / sexratio.scala
Created February 6, 2013 01:13
Maximize your chances of hooking up, with Scalding :))
Goal: You are a male between the age of (20,40).
You want to hook up with a lady, perhaps get married.
Where should you go to maximize your chances ?
Scalding code: sexratio(2011,(20,40),people,fipspipe)
where sexratio as below:
def sexratio(baseYear:Int, agerange:(Int,Int), people:Pipe, fipspipe:Pipe) = {
people.filter('year,'age) {
@krishnanraman
krishnanraman / race.scala
Last active December 12, 2015 05:09
Race Relations in the US of A: 2011
Goal: Using Scalding, find the county with the highest ratio of whites to the total population.
Do the same for blacks.
Results: fastforward to the very end.
/*
Want: white people in an agegroup
*/
race(2011,(20,40),people,fipspipe,true)
@krishnanraman
krishnanraman / ProportionMonoid
Last active December 12, 2015 07:18
ProportionMonoid
RESULT:
[tw-mbp13-kraman algebird (develop)]$ scala ProportionMonoid
List(1.0, 3.0)=>List(0.25, 0.75)
List(1.0, 2.0, 5.0, 2.0, 6.0, 4.0)=>List(0.05, 0.1, 0.25, 0.1, 0.3, 0.2)
SOURCE:
case class ProportionMonoid(x:Seq[Double]) {
private val n = x.size
if (n < 2) throw new RuntimeException("No proportions to compute!!!")