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 MapReduce[E, R] { | |
| type S | |
| def map(e: E): S | |
| def reduce(s1: S, s2: S): S | |
| def finish(s: S): R | |
| } | |
| def linear[E, R](seq: Array[E], mr: MapReduce[E, R]): R = | |
| mr.finish(seq.map(mr.map).reduceLeft(mr.reduce)) |
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 example | |
| import com.google.common.base.Function | |
| import com.google.common.collect.MapMaker | |
| object MemoUtil { | |
| import scalaz._ | |
| import Scalaz._ | |
| /** |
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 actors.Actor | |
| import actors.Actor._ | |
| object RingTopologySimulation extends Application { | |
| case class Message(count: Int) | |
| class Node(id: Int, stops: Int) extends Actor { | |
| var next: Node = null |
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 Exclude[Scope, X] | |
| object Exclude { | |
| implicit def $$NOT_FOR_USER_CODE$$[X, A]: Exclude[X, A] = new Exclude[X, A] {} | |
| def list[X] = new { | |
| def apply[A] = $$NOT_FOR_USER_CODE$$[X, A] | |
| } | |
| } | |
| sealed trait BoolOrInt |
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
| // Tony Morris' Optional Exercises | |
| // see http://scala-programming-language.1934581.n4.nabble.com/Further-understanding-scala-Option-td2334540.html | |
| // Scala version 2.8.0.final | |
| // http://scalacheck.googlecode.com/files/scalacheck_2.8.0-1.8-SNAPSHOT.jar | |
| /* | |
| Below are 15 exercises. The task is to emulate the scala.Option API | |
| without using Some/None subtypes, but instead using a fold (called a | |
| catamorphism). |
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
| #define HEXCHAR2INT(x, y) ((x >= 'a' ? x-87 : x-48) * 16 + (y >= 'a' ? y-87 : y-48)) | |
| float * _parse_color(const char * str){ | |
| int size = strlen(str); | |
| float * ret = new float[4]; | |
| switch(size){ | |
| case 4: // #rgb | |
| ret[0] = HEXCHAR2INT(str[1], str[1]) / 255.0; | |
| ret[1] = HEXCHAR2INT(str[2], str[2]) / 255.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.concurrent.multimap | |
| import scala.reflect.Manifest | |
| import java.util.concurrent.{ConcurrentSkipListSet, ConcurrentHashMap} | |
| import java.util.{Set => JSet} | |
| import scala.collection.JavaConversions._ | |
| import annotation.tailrec | |
| class Index[K <: AnyRef,V <: AnyRef : Manifest] { |
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
| scala> def apply[A, B](a: A)(f: A => B) = f(a) | |
| apply: [A,B](a: A)(f: (A) => B)B | |
| scala> apply(1)(_ * 2) | |
| res0: Int = 2 | |
| scala> def apply[A, B](a: A, f: A => B) = f(a) | |
| apply: [A,B](a: A,f: (A) => B)B | |
| scala> apply(1, _ * 2) |
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
| scala> // Problem: Some java apis return nulls. | |
| scala> System.getenv("PROBABLY_NOT_DEFINED_RUBISH_NAME") | |
| res0: java.lang.String = null | |
| scala> System.getenv("PROBABLY_NOT_DEFINED_RUBISH_NAME").split(",") | |
| java.lang.NullPointerException | |
| at .<init>(<console>:7) | |
| at .<clinit>(<console>) | |
| at RequestResult$.<init>(<console>:9) |
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
| makeTrade :: [(String, Maybe String)] -> Maybe Trade | |
| -- using applicative | |
| makeTrade alist = | |
| Trade <$> lookup1 "account" alist | |
| <*> lookup1 "instrument" alist | |
| <*> (read <$> (lookup1 "market" alist)) | |
| <*> lookup1 "ref_no" alist | |
| <*> (read <$> (lookup1 "unit_price" alist)) | |
| <*> (read <$> (lookup1 "quantity" alist)) |