Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/lsp4you/public_html/connect.php on line 2 LSP4YOU - Learner's Support Publications

Mapreduce


object FunctionsDay2 {
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
  
  
  // Mapreduce1
  def mapreduce1(
  			map: Int =>Int,
  			reduce: (Int, Int) => Int,
  			iden: Int,
  			x: Int,
  			y: Int
  		) = {
  		def iter(n:Int, acc:Int) : Int =
  		if(n > y) acc else iter(n+1, reduce(map(n), acc))
  		iter(x,iden)
  		
  		}                                 //> mapreduce1: (map: Int => Int, reduce: (Int, Int) => Int, iden: Int, x: Int,
                                                  //|  y: Int)Int
  		mapreduce1(a=>a, (a,b) => a+b,0,1,10)
                                                  //> res7: Int = 55
  		mapreduce1(a=>a*a, (a,b) => a+b,0,1,10)
                                                  //> res8: Int = 385
  		mapreduce1(a=>a, (a,b) => a*b,1,1,5)
                                                  //> res9: Int = 120
 // Mapreduce2
  def mapreduce2(
  			map: Int =>Int,
  			reduce: (Int, Int) => Int,
  			iden: Int)
  			(x: Int,
  			y: Int) = {
  		def iter(n:Int, acc:Int) : Int =
  		if(n > y) acc else iter(n+1, reduce(map(n), acc))
  		iter(x,iden)
  		
  }                                               //> mapreduce2: (map: Int => Int, reduce: (Int, Int) => Int, iden: Int)(x: Int,
                                                  //|  y: Int)Int
  mapreduce2(a=>a, (a,b) => a+b,0)(1,10)          //> res10: Int = 55
  mapreduce2(a=>a*a, (a,b) => a+b,0)(1,10)        //> res11: Int = 385
  mapreduce2(a=>a, (a,b) => a*b,1)(1,5)           //> res12: Int = 120


 // Mapreduce3
  def mapreduce3(
  			map: Int =>Int,
  			reduce: (Int, Int) => Int,
  			iden: Int)
  			:(Int,Int) => Int = {
  		def fn(x:Int, y:Int) = {
  			
  		def iter(n:Int, acc:Int) : Int =
  		if(n > y) acc else iter(n+1, reduce(map(n), acc))
  		iter(x,iden)
  		}
  		fn
  		
  }                                               //> mapreduce3: (map: Int => Int, reduce: (Int, Int) => Int, iden: Int)(Int, In
                                                  //| t) => Int
  val sum2 = mapreduce3(a=>a, (a,b) => a+b,0)     //> sum2  : (Int, Int) => Int = FunctionsDay2$$$Lambda$26/1531333864@7e0ea639
  val sumSq2 = mapreduce3(a=>a*a, (a,b) => a+b,0) //> sumSq2  : (Int, Int) => Int = FunctionsDay2$$$Lambda$26/1531333864@7a0ac6e3
                                                  //| 
  val fact2 = mapreduce3(a=>a, (a,b) => a*b,1)    //> fact2  : (Int, Int) => Int = FunctionsDay2$$$Lambda$26/1531333864@17f6480

	sum2(1,10)                                //> res13: Int = 55
	sumSq2(1,10)                              //> res14: Int = 385
	fact2(1,5)                                //> res15: Int = 120
}