Skip to content

Instantly share code, notes, and snippets.

View ymnk's full-sized avatar

Atsuhiko Yamanaka ymnk

View GitHub Profile
@raymondtay
raymondtay / gist:2289042
Created April 3, 2012 03:21
Interactive Input
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()
@rirakkumya
rirakkumya / scalaCheckGeneratorSample.scala
Created January 24, 2012 05:39
ScalaCheckGeneratorSample
//ランダムにユーザーデータを作成
//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)
@retronym
retronym / currency.scala
Created December 14, 2011 23:46
Currency
// 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)
}
@gakuzzzz
gakuzzzz / 1_types.scala
Created December 12, 2011 02:22
型クラスを使わないMonadic
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]
@retronym
retronym / forall.scala
Created November 26, 2011 18:21
universal quantification
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"))
@retronym
retronym / config.scala
Last active May 9, 2018 05:47
Styles of config propagation: Manual, Implicits, DynamicVariable, Reader
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>
@jrudolph
jrudolph / TowersOfHanoi.scala
Created February 19, 2009 13:51
Scala-Metaprogramming: Towers of Hanoi
/*
* 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