Skip to content

Instantly share code, notes, and snippets.

View folone's full-sized avatar
🏔️

George Leontiev folone

🏔️
View GitHub Profile
scala> import play.api.libs.json._
import play.api.libs.json._
scala> case class Test[A: Writes](a: A)
defined class Test
scala> implicit def testWrites[A: Writes] = Json.writes[Test[A]]
<console>:12: error: No apply function found matching unapply parameters
implicit def testWrites[A: Writes] = Json.writes[Test[A]]
^
@folone
folone / 2.10.4.scala
Created May 23, 2014 12:36
quasiquote patternmatch for comprehension
> ++2.10.4
[info] Setting version to 2.10.4
[info] Set current project to wartremover (in build file:/Users/georgii/workspace/wartremover/)
> console
[info] Updating {file:/Users/georgii/workspace/wartremover/}wartremover...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 24 Scala sources to /Users/georgii/workspace/wartremover/target/scala-2.10/classes...
[info] Starting scala interpreter...
[info]
scala> trait Assoc[K] { type V ; val value: V }
defined trait Assoc
scala> def mkAssoc[K, V0](k: K, v: V0): Assoc[k.type] { type V = V0 } =
| new Assoc[k.type] { type V = V0 ; val value = v }
mkAssoc: [K, V0](k: K, v: V0)Assoc[k.type]{type V = V0}
scala> def lookup[K](k: K)(implicit assoc: Assoc[k.type]): assoc.V = assoc.value
lookup: [K](k: K)(implicit assoc: Assoc[k.type])assoc.V
@folone
folone / Bf.scala
Created June 30, 2011 07:21
Brainfuck Parser, using parser combinators
import scala.util.parsing.combinator._
import scala.collection.mutable._
object Bf extends Application {
private val data = ArrayBuffer[Char]() // TODO fill
private var pointer = 0
private var in: String = ""
private var _init = false
private def getNextInput() = {
trait Monad[+M[_]] {
def unit[A](a: A): M[A]
def bind[A, B](m: M[A])(f: A => M[B]): M[B]
}
// probably only works in Scala 2.8
implicit def monadicSyntax[M[_], A](m: M[A])(implicit tc: Monad[M]) = new {
private val bind = tc.bind(m) _
def map[B](f: A => B) = bind(f compose tc.unit)
trait ~>[F[_],G[_]] {
def apply[A](a: F[A]): G[A]
}
type Id[A] = A
def apply[B](f: Id ~> List, b: B, s: String): (List[B], List[String]) = (f(b), f(s))
@folone
folone / atd.scala
Created July 11, 2011 07:41
emulating ADT with case classes in scala
// haskell:
// data Bool = False | True
// scala
sealed abstract class Bool
case object True extends Bool
case object False extends Bool
-- First
return a >>= k = k a
-- Second
m >>= return = m
-- Third
m >>= (\x -> f x >>= g) = (m >>= f) >>= g
reset {
// A
shift { cf: (Int=>Int) =>
// B
val eleven = cf(10)
// E
println(eleven)
val oneHundredOne = cf(100)
// H
println(oneHundredOne)
@folone
folone / gist:1188506
Created September 2, 2011 12:36
Factorial with type magic. There's more here: http://www.willamette.edu/~fruehr/haskell/evolution.html
-- static Peano constructors and numerals
data Zero
data Succ n
type One = Succ Zero
type Two = Succ One
type Three = Succ Two
type Four = Succ Three