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
//ランダムにユーザーデータを作成 | |
//name⇒20文字以内の英字 | |
//age⇒1~100歳 | |
import org.scalacheck.Gen | |
case class User(name:String, age:Int) | |
val userGen = for{ | |
s <- Gen.choose(1,20) | |
n <- Gen.alphaStr | |
a <- Gen.choose(1,100) | |
}yield User(n.take(s),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
// Compile with Scala 2.10+ or 2.9.1 with -Ydependent-method-types | |
class Ccy { | |
trait AbstractCurrency{ | |
type Currency <: AbstractCurrency | |
def value : Double | |
def make(d:Double) : Currency | |
def +(x: Currency): Currency = make(x.value + value) | |
} |
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 Functor[+A, M[A] <: Functor[A, M]] { | |
def map[B](f: A => B): M[B] | |
def fmap[B] = map[B] _ | |
} | |
trait Pointed[M[_] <: Pointed[M]] { | |
def pure[A](a: A): M[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
scala> trait Forall[F[_]]{ def apply[A]: F[A] } | |
defined trait Forall | |
scala> type ListFun[A] = List[A] => List[A] | |
defined type alias ListFun | |
scala> object Reverse extends Forall[ListFun] { def apply[A] = _.reverse} | |
defined module Reverse | |
scala> Reverse[String](List("1", "2")) |
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
package scalaz.example | |
object Reader extends App { | |
/** | |
* Manual propagation of the environment (in the example, `contextRoot`.) | |
*/ | |
object Config0 { | |
def fragment1(contextRoot: String) = <a href={contextRoot + "/foo"}>foo</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
/* | |
* This is the Towers of Hanoi example from the prolog tutorial [1] | |
* converted into Scala, using implicits to unfold the algorithm at | |
* compile-time. | |
* | |
* [1] http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_3.html | |
*/ | |
object TowersOfHanoi { | |
import scala.reflect.Manifest |