Skip to content

Instantly share code, notes, and snippets.

View gclaramunt's full-sized avatar

Gabriel Claramunt gclaramunt

View GitHub Profile
@gclaramunt
gclaramunt / LuhnCheck.scala
Created November 15, 2011 19:21
Luhn credit card verification check in a couple of lines of Scala
object LuhnCheck {
def digitToInt(x:Char)=x.toInt-'0'.toInt
def digitSeqToInt(ds:Seq[Char])= ds map digitToInt _
def double ( xs:Seq[Int]) = xs zip (0 to xs.size-1) map { xy => xy._1+xy._1*((xy._2+1) %2) }
def isValid(cc:String) = (digitSeqToInt (double ( digitSeqToInt (cc)) mkString("") ) sum) % 10 == 0
@gclaramunt
gclaramunt / Circularbuffer.scala
Created November 23, 2011 17:33
circular buffer
package my.collections
class CirculrBufferIterator[T](buffer:Array[T], start:Int) extends Iterator[T]{
var idx=0
override def hasNext = idx<buffer.size
override def next()={
val i=idx
idx=idx+1
buffer(i)
}
@gclaramunt
gclaramunt / ListZipper.scala
Created January 18, 2012 02:46
Simple list zipper
case class ListZipper[T](rev:List[T], fwd:List[T]){
override def toString = rev.reverse.toString ++" " ++ fwd.toString
def read:T= fwd.head
def add(c:T)=ListZipper(rev,c::fwd)
def update(c:T)=ListZipper(rev,c::fwd.tail)
def remove()=ListZipper(rev,fwd.tail)
def back=ListZipper(rev.tail,rev.head::fwd)
def forward=ListZipper(fwd.head::rev,fwd.tail)
import io.Source
import java.io.File
/**
* Template for predictive text suggester
*/
object Suggest {
type FreqMap= Map[String, Int];
type FreqMaps = (FreqMap, FreqMap)
/**
@gclaramunt
gclaramunt / comprehensible.scala
Created March 12, 2012 01:46 — forked from chrislewis/comprehensible.scala
general type and implicit conversion to add support for using a general Monad in a for comprehension
trait Monad[M[_]] {
def unit[A](a: => A): M[A]
def fmap[A, B](ma: M[A], f: A => B): M[B] = bind(ma, (a:A) => unit(f(a)))
def bind[A, B](ma: M[A], f: A => M[B]): M[B]
}
@gclaramunt
gclaramunt / Event.java
Created March 28, 2012 18:13
Fluent state machine in Java
package statemachine;
public class Event {
final private String label;
public Event(String newLabel) {
label = newLabel;
}
/**
val ins=List("ejp mysljylc kd kxveddknmc re jsicpdrysi","rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd","de kr kd eoya kw aej tysr re ujdr lkgc jv")
val outs=List("our language is impossible to understand","there are twenty six factorial possibilities","so it is okay if you want to just give up")
val dict=Map() ++ ins.zip(outs).flatMap( x=>x._1.toList.zip(x._2.toList))+('a'->'y')+('o'->'e')+('z'->'q')
@gclaramunt
gclaramunt / latency.txt
Created June 5, 2012 20:26 — forked from jboner/latency.txt
Latency numbers every programmer should know
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms
Read 4K randomly from SSD 150,000 ns 0.15 ms
Read 1 MB sequentially from memory 250,000 ns 0.25 ms
Round trip within same datacenter 500,000 ns 0.5 ms
@gclaramunt
gclaramunt / planes.scala
Created June 6, 2012 18:08
Very simple phantom types example
trait FlightStatus
trait Flying extends FlightStatus
trait Landed extends FlightStatus
case class Plane[Status <: FlightStatus]()
def land(p:Plane[Flying])=Plane[Landed]()
def takeOff(p:Plane[Landed])= Plane[Flying]()
val plane = new Plane[Landed]()
@gclaramunt
gclaramunt / AirTrafficController.java
Created June 12, 2012 19:34
Phantom types in Java
public class AirTrafficController {
public static Plane<Landed> land(Plane<Flying> p) {
return new Plane<Landed>(p);
}
public static Plane<Flying> takeOff(Plane<Landed> p) {
return new Plane<Flying>(p);
}
public static void main(String[] args){