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 / fpin_scala.scala
Created June 28, 2012 02:39
Functional Programming in Scala (MEAP) Runar, Tony, Paul
scala> def mod_list[A](list: List[A])(f: A => A) : List[A] =
| list match {
| case Nil => Nil
| case h :: t => f(h) :: mod_list(t)(f) }
mod_list: [A](list: List[A])(f: A => A)List[A]
scala> mod_list((1 to 100).toList)(x => x + 1)
res0: List[Int] = List(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101)
scala> implicit def dToStr(d: Double): String = d + ""
@raymondtay
raymondtay / lazy_val_demo.scala
Created July 2, 2012 02:53
how lazy vals are implemented
class Top
class X(v: => Top) extends Top {
lazy val _v = v
def v() :Top = _v
}
class Y extends Top
object Demo extends App {
val x: X = new X(x)
}
object LazyValDeadLock extends App {
println((new Z).Y)
}
class Z {
lazy val X = 0
lazy val Y = {
for(i <- 0 until 2 par) yield {
println(i)
X
@raymondtay
raymondtay / rpn.scala
Created July 5, 2012 16:04
Reverse Polish Notation
object solveRPN extends App {
override def main(args: Array[String] ) = {
val l = List("10", "4", "3", "+", "2", "*", "-")
val r = l.foldLeft(List.empty[String])(foldingFunction)
println(foldingFunction(r,""))
}
// List(10, 4, 3, +, 2, *, -)
def foldingFunction(expr : List[String], ele: String): List[String] = {
@raymondtay
raymondtay / trampoline.scala
Created July 18, 2012 03:57
trampoline.scala
// this source is extract from TailCalls.scala in Scala's trunk repo.
object TailCalls {
/** This class represents a tailcalling computation
*/
abstract class TailRec[+A] {
/** Returns the result of the tailcalling computation.
*/
def result: A = {
def loop(body: TailRec[A]): A = body match {
@raymondtay
raymondtay / tail_call_infinite_loop.scala
Created July 19, 2012 02:46
playing with tail call in scala
// following code runs into infinite loop
// attempting to find a more generic way to write a optimized tail-call & generic function
scala> val embed = new Function2[Int,Int,Int] { override def apply(n:Int, acc:Int) = if (n <=0) acc else apply(n -1 , acc *n) }
scala> @tailrec def tailrecurse(cond: => Boolean)(whenTrue: => Any)(f: => Int ): Any = if (cond) whenTrue else tailrecurse(cond)(whenTrue)(f)
scala> var n = 12; var acc = 1;
scala> tailrecurse(n<=0)(acc)(embed(n,acc))
// the following works better w/o running into stackoverflow and captures type of result instead of 'Any'
// cannot use '@tailrec' on 'notailrecurse' as there's no tail-call
@raymondtay
raymondtay / reduction.cl
Created August 20, 2012 08:23
Reduction in OpenCL
__kernel void super_reduction(__global float16* data,
__local float4* partial_sums, __global float* output) {
int lid = get_local_id(0);
int group_size = get_local_size(0);
float16 localData = data[get_global_id(0)];
float4 t; t += localData.s0123;
t += localData.s4567;
t += localData.s89ab;
@raymondtay
raymondtay / ch4.hs
Created August 23, 2012 07:47
playing with haskell
-- Compile this with the following command
-- "runghc --make Ch4-real-world-haskell.hs"
-- Run it like this:
-- "./Ch4-real-world-haskell <input file> <output file>"
-- import the function 'getArgs' from module 'System.Environment'
import System.Environment (getArgs)
interactWith function inputFile outputFile = do
input <- readFile inputFile
@raymondtay
raymondtay / chapter3.scala
Created February 4, 2013 01:55
Working through Chapter 3 of Functional Programming
import annotation._
object Chapter3 {
// Variadic functions in Scala
// -------------------------------
// The function List.apply in the listing above is a variadic function,
// meaning it accepts zero or more arguments of type A. For data types, it
// is a common idiom to have a variadic apply method in the companion object
// to conveniently construct instances of the data type. By calling this
// function 'apply' and placing it in the companion object, we can
@raymondtay
raymondtay / chapter4_option.scala
Created February 6, 2013 04:04
working through chapter 4 of the MEAP - Functional Programming In Scala
object Chapter4 {
// The general rule of thumb is that we use exceptions only if no
// reasonable program would ever catch the exception - if for some
// callers the exception might be a recoverable error, we use Option to
// give them flexibility
sealed trait Option[+A] {
def map[B](f: A => B) : Option[B] = this match {
case Some(v) => Some(f(v))