This file contains hidden or 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 combine[K,V](map1:Map[K,Seq[V]], map2:Map[K,Seq[V]]) = | |
| (map1.toList ++ map2.toList).groupBy(_._1).mapValues(_.map(_._2).flatten).map(identity) |
This file contains hidden or 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 Person(name:String,age:Int) | |
| val dino=Person("dino",48) //> dino : stuff.Person = Person(dino,48) | |
| // get tuple from case class, decompose | |
| val (name,age)=Person.unapply(dino).get //> name : String = dino | |
| //| age : Int = 48 | |
| // create case class from composed tuple | |
| val dino2=Person.tupled((name,age)) //> dino2 : stuff.Person = Person(dino,48) |
This file contains hidden or 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
| // Broken apart to ease understanding. | |
| val s1=Seq(1,1,1,2,2,2,3,3,3,3,4,4,4,4,1) | |
| val groups=s1.groupBy { identity } | |
| val sizes=groups.mapValues { _.size} | |
| val maxSize=sizes.values.max | |
| val maxUsers=sizes.filter(_._2==maxSize).keys.toSet | |
| val pop=s1.filter(maxUsers).zipWithIndex | |
| val lastPlaces=pop.groupBy(_._1).mapValues(_.last) |
This file contains hidden or 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
| http://aperiodic.net/phil/scala/s-99/p16.scala | |
| Mine | |
| def dropNth[A](n: Int, ls: List[A])= | |
| ls.grouped(n).flatMap(_.take(n-1)).toList | |
| --------- | |
| val li=List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k) |
This file contains hidden or 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
| Alternate solution for | |
| http://aperiodic.net/phil/scala/s-99/p19.scala | |
| def rotate[A](n: Int, ls: List[A])= | |
| if (ls.isEmpty) ls | |
| else{ | |
| val (first,last)=ls.splitAt((if (n>=0) n else ls.length+n)%ls.length) | |
| last:::first | |
| } |
This file contains hidden or 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
| Alternative for http://aperiodic.net/phil/scala/s-99/p21.scala | |
| def insertAt[A](e: A, n: Int, ls: List[A]) = | |
| ls.patch(n, List(e), 0) | |
| insertAt('new,1,li) //> res1: List[Symbol] = List('a, 'new, 'b, 'c, 'd) | |
| insertAt('new,0,li) //> res2: List[Symbol] = List('new, 'a, 'b, 'c, 'd) | |
| insertAt('new,99,li) //> res3: List[Symbol] = List('a, 'b, 'c, 'd, 'new) |
This file contains hidden or 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
| // Just run this inside the spark shell, no more chatty logs | |
| import org.apache.log4j.Logger | |
| import org.apache.log4j.Level | |
| Logger.getLogger("org").setLevel(Level.OFF) | |
| Logger.getLogger("akka").setLevel(Level.OFF) |
This file contains hidden or 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
| val li=List(1,2,10,20,30) //> li : List[Int] = List(1, 2, 10, 20, 30) | |
| def insert[T](li:List[T],x:T)(implicit cmp:Ordering[T])={ | |
| val (first,last)=li.partition {cmp.lteq(_, x) } | |
| first:::x::last | |
| } //> insert: [T](li: List[T], x: T)(implicit cmp: Ordering[T])List[T] | |
| insert(li,11) //> res0: List[Int] = List(1, 2, 10, 11, 20, 30) | |
| insert(li,10) //> res1: List[Int] = List(1, 2, 10, 10, 20, 30) | |
| insert(li,9) //> res2: List[Int] = List(1, 2, 9, 10, 20, 30) |
This file contains hidden or 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
| package example | |
| import org.scalajs.dom | |
| import scalajs.js.annotation.JSExport | |
| import scala.scalajs.concurrent.JSExecutionContext.Implicits.runNow | |
| import dom.ext.Ajax | |
| import scalajs.js | |
| import js.Dynamic.literal | |
| import com.felstar.scalajs.ractive._ | |
| @JSExport |
This file contains hidden or 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
| object Checkout extends App { | |
| val rulesString = """ | |
| A,50,3 for 130 | |
| B,30,2 for 45 | |
| C,20 | |
| D,15 | |
| """ | |
| import CheckoutRuleEngine._ |