Skip to content

Instantly share code, notes, and snippets.

View lildata's full-sized avatar

Programming is fun lildata

View GitHub Profile
new Iterator[String] {
def hasNext = resultSet.next()
def next() = resultSet.getString(1)
}.toStream
object x {
// stream a sql query
def sql( str:String ):Stream[ResultSet] = withStatement { s =>
val rs = s executeQuery str
new Iterator[ResultSet] { def hasNext = rs.next ; def next = rs }.toStream
}
// loan a sql statement
@lildata
lildata / ReadFile.scala
Created May 18, 2015 19:08
Read a file, no ceremony :-D
println(scala.io.Source.fromFile("some.txt").mkString)
@lildata
lildata / doXtimesImplicit.scala
Created May 19, 2015 20:12
how to create a X times syntax with implicits, also cool for DSLs
implicit def intWithTimes(n: Int) = new {
def times(f: => Unit) = 1 to n foreach {_ => f}
}
5 times {
println("Hello World")
}
@lildata
lildata / companionOf.scala
Last active August 29, 2015 14:21 — forked from piotrga/gist:5928581
Get companion object instance with new Scala reflection API
trait ReflectionSugars{
import scala.reflect.runtime.{universe => ru}
private lazy val universeMirror = ru.runtimeMirror(getClass.getClassLoader)
def companionOf[T](implicit tt: ru.TypeTag[T]) = {
val companionMirror = universeMirror.reflectModule(ru.typeOf[T].typeSymbol.companionSymbol.asModule)
companionMirror.instance
}
}
@lildata
lildata / scalaColoredREPL.sh
Created May 24, 2015 18:42
Enable coloring in the Scala REPL
$ scala -Dscala.color
scala> val sqlc = new org.apache.spark.sql.SQLContext(sc)
scala> import sqlc.implicits._ //to implicitly convert an RDD to a DataFrame
scala> val df1 = sqlc.load("/home/....json","json")
scala> df1.printSchema
scala> df1.select("name").show
scala> df1.select("name", df1("age") + 1).show
scala> df1.filter(df1("age") > 10).show
scala> df1.groupBy("age").count().show
@lildata
lildata / unless.scala
Created June 4, 2015 17:51
Funny, defining an "unless" construct
def unless(expr: Boolean)(perform: ()=>Unit) { if(!expr) perform() }
unless(6 == 5) { ()=>println("Hello world") }
@lildata
lildata / prepostfix.scala
Created June 4, 2015 18:24
Prefix (unary_) & Postfix are cool for DSLs
case object CONNECTION {
def unary_! = DISCONNECTION
def REMOVE = DISCONNECTION
}
case object DISCONNECTION
println(!CONNECTION == DISCONNECTION) //true
println((CONNECTION REMOVE) == DISCONNECTION) //true
; A REPL-based, annotated Seesaw tutorial
; Please visit https://github.com/daveray/seesaw for more info
;
; This is a very basic intro to Seesaw, a Clojure UI toolkit. It covers
; Seesaw's basic features and philosophy, but only scratches the surface
; of what's available. It only assumes knowledge of Clojure. No Swing or
; Java experience is needed.
;
; This material was first presented in a talk at @CraftsmanGuild in
; Ann Arbor, MI.