Skip to content

Instantly share code, notes, and snippets.

// see https://github.com/gseitz/Lensed
case class CoState[A, B](put: A => B, pos: A)
object CoState {
type Store[A, B] = CoState[A, B]
}
// fused get/set
case class Lens[A, B](lens: A => CoState[B, A]) {
@mxcl
mxcl / uninstall_homebrew.sh
Created August 26, 2011 11:25
Uninstall Homebrew
#!/bin/sh
# Just copy and paste the lines below (all at once, it won't work line by line!)
# MAKE SURE YOU ARE HAPPY WITH WHAT IT DOES FIRST! THERE IS NO WARRANTY!
function abort {
echo "$1"
exit 1
}
set -e
@kmizu
kmizu / TypeConstructorCurrying.scala
Created September 25, 2011 15:03
Scala's type constructor is not "curried" form. In this example, it is explained that we can emulate type constructor currying using abstract type member.
trait TF {
type Apply[A]
}
type Curry2[F[_, _]] = TF { type Apply[X] = TF { type Apply[Y] = F[X, Y] } }
type Curry3[F[_, _, _]] = TF { type Apply[X] = Curry2[(TF { type Apply[Y, Z] = F[X, Y, Z] })#Apply] }
// ...
type CurriedMap = Curry2[Map]
val x: CMap#Apply[String]#Apply[Int] = Map("x" -> 1, "y" -> 1) //It is valid code.
@hishidama
hishidama / gist:1256333
Created October 1, 2011 17:06
ScalaのREPLの仮想ディレクトリー内のファイルを保存するスクリプト
:power
import java.io._
import scala.tools.nsc.io.AbstractFile
def save(af: AbstractFile, dir: File) {
val f = new File(dir, af.name)
if (af.isDirectory) {
f.mkdirs()
af.par.foreach(a => save(a, f))
@einblicker
einblicker / gist:1325753
Created October 30, 2011 10:05
type-level FizzBuzz
trait Nat {
type IsZero <: Bool
type Prev <: Nat
type Divisable[A <: Nat] = DivisableImpl[A, A]
type DivisableImpl[A <: Nat, B <: Nat] <: Bool
}
trait Z extends Nat {
type IsZero = True
type Prev = Nothing
@tototoshi
tototoshi / Twitter4jStreamingAPISample.scala
Created November 4, 2011 03:24
Twitter4jでストリーミングAPIを使ってみる
import twitter4j._
import conf._
/* build.sbt
scalaVersion := "2.9.1"
libraryDependencies ++= Seq(
"org.twitter4j" % "twitter4j-core" % "2.2.5",
"org.twitter4j" % "twitter4j-stream" % "2.2.5"
@pchiusano
pchiusano / GADTs.scala
Created November 16, 2011 04:30
GADT support in Scala
/** GADTs in Scala and their limitations */
/** Background: what is an algebraic data type (ADT) ?
* ADT: (possibly) recursive datatype with sums and products
* In scala - a trait with case classes (case class is product, subtyping is sum)
*/
/** Motivation: untyped embedded DSL doesn't prevent nonsensical expressions */
sealed trait Expr {
def apply(other: Expr) = Ap(this, other)
@takezoe
takezoe / gist:1449859
Created December 9, 2011 02:29
XML response with Unfiltered
import unfiltered.request._
import unfiltered.response._
import scala.xml._
case class Xml(nodes: NodeSeq)
extends ComposeResponse(TextXmlContent ~> ResponseString(nodes.toString))
// Usage
def intent = {
case GET(Path(Seg("hello" :: name :: Nil))) => {
@retronym
retronym / type-class-type-member.scala
Created December 11, 2011 07:02
Type Classes using type members, rather than type parameters
trait Semigroup_ {
type F
def append(a: F, b: => F): F
}
trait Monoid_ extends Semigroup_ {
def zero: F
}
trait Functor_ {
@tototoshi
tototoshi / KanjiNumberParser.scala
Created December 18, 2011 05:50
漢数字パーサだよ
import scalaz._
import Scalaz._
import scala.util.parsing.combinator._
object KanjiNumberParser extends RegexParsers {
def one = "一" ^^ { _ => 1L }
def two = "二" ^^ { _ => 2L }
def three = "三" ^^ { _ => 3L }
def four = "四" ^^ { _ => 4L }
def five = "五" ^^ { _ => 5L }