This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> empDB.minBy(e=>(-e.tenure,-e.age,e.salary)) | |
res1: Employee = Employee(35,6,85079,emp7435) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> val m = Map(1->"one",2->"one",3->"two") | |
scala> m.filter(pair=>pair._2 == "one").head._1 | |
res32: Int = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... | |
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!!!") |