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
// Karatsuba solution in Scala. Works with negative numbers also. | |
// https://en.wikipedia.org/wiki/Karatsuba_algorithm | |
object Karatsuba { | |
import scala.math._ | |
def split(x:Int, y:Int)={ | |
val absx=abs(x) | |
val absy=abs(y) | |
val m=max(absx.toString.length(),absy.toString.length()) |
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 MergeSort { | |
// recursive merge of 2 sorted lists | |
def merge(left: List[Int], right: List[Int]): List[Int] = | |
(left, right) match { | |
case(left, Nil) => left | |
case(Nil, right) => right | |
case(leftHead :: leftTail, rightHead :: rightTail) => | |
if (leftHead < rightHead) leftHead::merge(leftTail, right) | |
else rightHead :: merge(left, rightTail) |
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 InversionFind { | |
type Inversion=(Int,Int) | |
type Inversions=List[Inversion] | |
// recursive merge of 2 sorted lists, whilst also returning the Inversions | |
def merge(left: List[Int], right: List[Int]): (List[Int], Inversions) = | |
(left, right) match { | |
case (left, Nil) => (left, Nil) | |
case (Nil, right) => (right, Nil) |
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 NaiveQuickSort { | |
// naive quicksort | |
def sort(li:List[Int]):List[Int]={ | |
if (li.size<2) return li | |
val pivot=li.head | |
val (left,right)=li.partition(_< pivot) | |
println(left,pivot,right.tail) // debug to help make the algo clearer. |
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 MinHeap { | |
case class Dog(age:Int, name:String) extends Ordered[Dog]{ | |
def compare(that: Dog) = - this.age.compareTo(that.age) | |
} | |
val minHeap = scala.collection.mutable.PriorityQueue.empty[Dog] | |
//> minHeap : scala.collection.mutable.PriorityQueue[MinHeap.Dog] = PriorityQue | |
//| ue() | |
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 com.felstar.breeze.numerics | |
import breeze.generic.UFunc | |
import breeze.generic.MappingUFunc | |
object inc extends UFunc with MappingUFunc{ | |
implicit object implDouble extends Impl[Double, Double] { | |
def apply(a: Double) = a+1 | |
} |
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
// say you have a method | |
def hgetall(key:String,value:String) | |
// but you want to optionally alter its behaviour, whilst keeping old behaviour as default, hence breaking no existing code | |
sealed trait Behavior | |
object Behaviors{ | |
implicit case object NewB extends Behavior | |
implicit case object OldB extends Behavior |
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
import java.util | |
import org.apache.kafka.clients.consumer.KafkaConsumer | |
import scala.collection.JavaConverters._ | |
object ConsumerExample extends App { | |
import java.util.Properties |
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 twitter | |
import scala.collection.mutable.ArrayBuffer | |
import twitter4j._ | |
import twitter4j.conf._ | |
import scala.collection.JavaConverters._ | |
object TwitterExport extends App { | |
// http://twitter4j.org/en/code-examples.html |
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(2, 3, 4, 5, 6, 10, 11,20, 21, 22) //> li : List[Int] = List(2, 3, 4, 5, 6, 10, 11, 20, 21, 22) | |
def groupList[T](list: List[T],inGroup: (T,T)=>Boolean) = { | |
def group(lst: List[T], acc: List[T]): List[List[T]] = (lst,acc) match { | |
case (Nil,_) => acc.reverse :: Nil | |
case (h :: t,Nil) => group(t, h :: acc) | |
case (h :: t,ah :: _) if inGroup(h,ah) => group(t, h :: acc) | |
case (h :: t,_) => acc.reverse :: group(t, h :: Nil) | |
} | |
group(list, List.empty) |