Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / Karatsuba.scala
Last active November 24, 2015 19:32
Karatsuba solution in Scala. Works with negative numbers also.
// 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())
@fancellu
fancellu / MergeSort.scala
Created November 25, 2015 12:12
MergeSort in Scala with recursive merge
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)
@fancellu
fancellu / InversionFind.scala
Created November 25, 2015 17:41
Find the inversions in an array
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)
@fancellu
fancellu / NaiveQuickSort.scala
Last active May 25, 2021 18:42
Quicksort in Scala
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.
@fancellu
fancellu / MinHeap.scala
Created November 29, 2015 16:24
Example of using a min heap with case class
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()
@fancellu
fancellu / Inc.scala
Created December 10, 2015 23:09
Breeze UFunc example
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
}
@fancellu
fancellu / Behavior.scala
Created January 11, 2016 17:18
Example of adding optional new behavior to old method without breaking API
// 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
@fancellu
fancellu / ConsumerExample.scala
Last active November 28, 2024 13:00
Kafka Producer/Consumer Example in Scala
import java.util
import org.apache.kafka.clients.consumer.KafkaConsumer
import scala.collection.JavaConverters._
object ConsumerExample extends App {
import java.util.Properties
@fancellu
fancellu / TwitterExport.scala
Created January 14, 2016 19:42
Will export your twitter followers and following lists
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
@fancellu
fancellu / GroupList.scala
Created February 4, 2016 21:04
Example of grouping list items based on custom predicate
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)