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
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 + "" |
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
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) | |
} |
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 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 |
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 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] = { |
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
// 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 { |
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
// 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 |
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
__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; |
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
-- 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 |
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 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 |
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 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)) |