Skip to content

Instantly share code, notes, and snippets.

View danieldietrich's full-sized avatar
💭
📡 working from space

Daniel Dietrich danieldietrich

💭
📡 working from space
View GitHub Profile
package lambdas;
import sun.reflect.ConstantPool;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class TypeTest {
@FunctionalInterface
@viktorklang
viktorklang / na.scala
Created December 21, 2011 13:13
Fast, faster, Scala
object Scala extends App {
import scala.collection.mutable.ArrayBuffer
import scala.annotation.tailrec
val items = List("foo", 23, true)
val before = System.currentTimeMillis()
@tailrec def adds(n: Int, absoluteResult: ArrayBuffer[Any] = ArrayBuffer()): ArrayBuffer[Any] = {
def foo(obj : Any) = obj match {
case _:String => "String"
case _:Boolean => "Boolean"
case _:Integer => "Integer"
@sadache
sadache / gist:2939230
Created June 15, 2012 23:37
Parsing progressively a csv like file with Play2 and Iteratees

If your csv doesn't contain escaped newlines then it is pretty easy to do a progressive parsing without putting the whole file into memory. The iteratee library comes with a method search inside play.api.libs.iteratee.Parsing :

def search (needle: Array[Byte]): Enumeratee[Array[Byte], MatchInfo[Array[Byte]]]

which will partition your stream into Matched[Array[Byte]] and Unmatched[Array[Byte]]

Then you can combine a first iteratee that takes a header and another that will fold into the umatched results. This should look like the following code:

// break at each match and concat unmatches and drop the last received element (the match)
object Sql {
import scala.reflect.makro._
import language.experimental.macros
case class Query[R](/*sql: String*/)
def execute[R](q: Query[R]): Seq[R] = sys.error("implement me")
def sqlImpl(c: Context)(s: c.Expr[String]): c.Expr[Any] = {
import c.universe._
@mandubian
mandubian / gist:3377514
Created August 17, 2012 09:48
Play2 new plugin: File NonBlocking/Async API - Copying a file
"copy file" in {
var i = 0
val fileGenerator = Enumerator.fromCallback( () =>
if(i<1000){ i+=1; Future.successful(Some((new java.util.Date).getTime.toString + "\n")) } else Future(None)
)
val f = FileChannel("/tmp/testwrite.txt").delete.writing.create
val f2 = FileChannel("/tmp/testwrite2.txt").delete.writing.create
fileGenerator // generates data
@tonymorris
tonymorris / Balance.scala
Created September 23, 2012 01:43
Balance Parentheses
// Missing support libraries
object MissingLibraries {
case class State[S, +A](run: S => (A, S)) {
def map[B](f: A => B): State[S, B] =
State(s => {
val (a, t) = run(s)
(f(a), t)
})
def flatMap[B](f: A => State[S, B]): State[S, B] =
@tonymorris
tonymorris / FList.scala
Last active December 10, 2015 02:18
foldMap is equivalent to scala.List
trait Semigroup[A] {
def op(a1: A, a2: A): A
}
trait Monoid[A] extends Semigroup[A] {
def id: A
}
object Monoid {
implicit def ListMonoid[A]: Monoid[List[A]] =
@rokgerzelj
rokgerzelj / Shell
Last active December 10, 2015 20:08
# to install the latest stable version:
brew install play
# to install play-2.1-RC2:
brew install https://raw.github.com/gist/4485598/play.rb
# to switch versions (from https://github.com/mxcl/homebrew/wiki/External-Commands):
brew switch play 2.0.4
brew switch play 2.1-RC2
@bigtoast
bigtoast / Shell
Last active December 11, 2015 23:18
# to install the latest stable version:
brew install play
# to install play-2.1-RC3:
# if you already have a version of play installed you will need to unlink it.
brew unlink play
brew install https://raw.github.com/gist/4675514/play.rb
# to switch versions (from https://github.com/mxcl/homebrew/wiki/External-Commands):
brew switch play 2.0.4
@asflierl
asflierl / scala_2.10_
Created January 30, 2013 19:44
Dependent method types in Scala 2.10.... ooooOOOO yeah!
Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_11).
Type in expressions to have them evaluated.
Type :help for more information.
scala> trait Meep { type Moop; def f: Moop }
defined trait Meep
scala> def fnord(a: Meep): a.Moop = a.f
fnord: (a: Meep)a.Moop