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 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 |
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 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) | |
} |
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 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) |
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 io.Source | |
import java.io.File | |
/** | |
* Template for predictive text suggester | |
*/ | |
object Suggest { | |
type FreqMap= Map[String, Int]; | |
type FreqMaps = (FreqMap, FreqMap) | |
/** |
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
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] | |
} |
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 statemachine; | |
public class Event { | |
final private String label; | |
public Event(String newLabel) { | |
label = newLabel; | |
} | |
/** |
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 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') |
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
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 |
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
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]() |
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
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){ |