Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / combine.scala
Last active August 29, 2015 14:17
Combines 2 maps of sequences. Have seen this asked a few times. This is a fairly concise solution.
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)
@fancellu
fancellu / CaseTuple.scala
Created March 19, 2015 23:14
Case to tuple and back
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)
@fancellu
fancellu / FirstToFinishMost.scala
Last active August 29, 2015 14:17
Quick coding test. Given tasks to people, find person with most tasks, that finished first
// 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)
@fancellu
fancellu / DropNth.scala
Created July 30, 2015 18:46
Drop Nth element from List alternative soltuion
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)
@fancellu
fancellu / RotateN.scala
Last active August 29, 2015 14:26
RotateN to left : Alternate solution for http://aperiodic.net/phil/scala/s-99/p19.scala
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
}
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)
@fancellu
fancellu / SparkShellDebugOff.scala
Created August 6, 2015 11:20
Spark shell too chatty, want to turn off logging and can't be bothered to edit log4j files?
// 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)
@fancellu
fancellu / Insert.scala
Created August 30, 2015 15:03
Simple insertion into an ordered List
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)
@fancellu
fancellu / T1.scala
Last active September 10, 2015 15:27
ScalaJS ractive example - Live demo http://dinofancellu.com/demo/scalajsRactive/
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
@fancellu
fancellu / Checkout.scala
Last active October 9, 2015 11:19
Checkout Kata CodeTest in Scala
object Checkout extends App {
val rulesString = """
A,50,3 for 130
B,30,2 for 45
C,20
D,15
"""
import CheckoutRuleEngine._