Skip to content

Instantly share code, notes, and snippets.

View raymondtay's full-sized avatar
🎯
Focusing

Raymond Tay raymondtay

🎯
Focusing
View GitHub Profile
@raymondtay
raymondtay / gist:2289042
Created April 3, 2012 03:21
Interactive Input
import scala.util.continuations._
object AskMe {
def sleep(delay: Long) = shift{ k: (Unit => Int) =>
val runnable = new Runnable {
var leave = false
def run = {
while( ! leave ) {
Thread.sleep(delay)
val res = k()
@raymondtay
raymondtay / gist:2289212
Created April 3, 2012 04:01
Interactive Input 2
import scala.util.continuations._
object AskMe {
def sleep(delay: Long) = shift[Unit,Int,Unit]{ k: (Unit => Int) =>
val runnable = new Runnable {
var leave = false
def run = {
while( ! leave ) {
Thread.sleep(delay)
val res = k()
@raymondtay
raymondtay / gist:2297182
Created April 4, 2012 02:24
ControlContext signature
final class ControlContext[+A,-B,+C](val fun: (A => B, Exception => B) => C, val x: A) extends Serializable {
// ...
}
@raymondtay
raymondtay / implicit_funkiness.scala
Created April 4, 2012 07:14
implicit funkiness
trait S[M[_]] {
def doS[I](input: I) : I
}
object S {
implicit def convert2S(x : Int) : S[List] = new S[List] {
def doS[I](input: I) : I = { println("convert2S: returning 'input'..."); input }
}
implicit object SLike extends S[List] { def doS[I](i: I) : I = i; def doSLikeStuff[I <: List[_]](l: I) = l(0) }
implicit object SLike2 extends S[Set] { def doS[I](i: I) : I = i; def doSLikeStuff[I <: Set[_]](l: I) = l.headOption }
@raymondtay
raymondtay / stream_exploring.scala
Created April 5, 2012 01:58
Exploring Scala Stream
// Looks like a good idea but ...
val toInfinityNBeyond = Stream.continually{1}
// when you abuse it, it can bring ur REPL to a halt
// and it doesn't do what u think it does anyway so ...
(0 /: toInfinityNBeyond)(_+_)
// you should see ur ALL ur CPUs spike to 100%
// Ctrl+C (kill it)
// If you know how many values to create in the first place, then you can do this ...
val max = 1000
val finiteStreamFromOne = Stream.fill(max)(1)
@raymondtay
raymondtay / testAccS.scala
Created April 5, 2012 04:36
don't repeat the wheel, use fold/foldLeft/foldRight
object TestAccS extends App {
def add(i: Int, j: Int) = i + j
def accS[A](acc: A, s: Stream[A], fn: (A,A) => A) : A = {
if ( s.isEmpty ) return acc
accS( fn(acc, s.head), s.tail, fn)
}
val iS = Stream.iterate(1, 100){_ + 1} // the unbound parameter's draws the value from the immediate closure i.e. 1
println("Sum of elements: " + accS(0, iS, add) )
@raymondtay
raymondtay / lazyhashsearch.scala
Created April 5, 2012 14:29
Hash-based search
// Sample implementation of a Hash-based search
// from a lazy sequence e.g. Streams in Scala
// @author Raymond Tay
// @date 5 April 2012
import collection.mutable._
object LazyHashSearch {
// Functors appear to be type converters for type constructors
trait Functor[T[_]] {
def apply[A](x: A) : T[A]
def map[A,B](x: T[A])(f: A => B) : T[B]
}
// Simple Demo with Functors
object FunctorDemo extends App {
val listInt = List(1,2,3,4,5,6)
// Functors appear to be type converters for type constructors
trait Functor[T[_]] {
def apply[A](x: A) : T[A]
// curried functions (a.k.a partially-applied functions)
def map[A,B](x: T[A])(f: A => B) : T[B]
}
// Monads are very good for flattening types
trait Monad[T[_]] {
def flatten[A](m : T[T[A]]) : T[A]
@raymondtay
raymondtay / novice_type_prog.scala
Created June 26, 2012 02:13
novice trying out type level prog
val rubbishFn =new { type Pred = Int => Boolean; type MA[Pred] = List[Pred]; def test(colFn: MA[Pred])(implicit e: MA[Pred] <:< List[Pred]) = for(fn <- colFn) println(fn(1)) }
scala> def bind[A,B,C](a: A, p: A => B, f: (B, A) => C): C = f(p(a),a)
bind: [A, B, C](a: A, p: A => B, f: (B, A) => C)C
scala> def applyIf[A](a: A, f: A=>A, p: A => Boolean) : A = { val res = p(a); bind(a, p, (res:Boolean,a:A) => res match { case true => f(a); case _ => a} )}
applyIf: [A](a: A, f: A => A, p: A => Boolean)A
def sqrt(n : Double) : Double = {
def f(x : Double) = (x * x ) - n