This file contains 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 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() |
This file contains 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 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() |
This file contains 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
final class ControlContext[+A,-B,+C](val fun: (A => B, Exception => B) => C, val x: A) extends Serializable { | |
// ... | |
} |
This file contains 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 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 } |
This file contains 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
// 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) |
This file contains 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 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) ) |
This file contains 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
// 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 { |
This file contains 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
// 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) |
This file contains 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
// 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] |
This file contains 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 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 |
OlderNewer