Skip to content

Instantly share code, notes, and snippets.

@4e6
4e6 / Example.scala
Created October 30, 2014 10:24
Null Driven Development. Use AnyRef with for comprehensions.
class Pojo {
def doStuff(): Unit = println("rocket launched!")
}
object Example {
import NullDrivenDevelopment._
def none: Pojo = null
def pojo: Pojo = new Pojo
@4e6
4e6 / o.scala
Created August 24, 2014 09:11
Scala typeclass example
case class Object1(field: String)
case class Object2(fields: List[String])
trait CanBeMasked[T] {
def mask(o: T, f: String ⇒ String): T
}
object CanBeMasked {
implicit object O1 extends CanBeMasked[Object1] {
def mask(o: Object1, f: String ⇒ String) = o.copy(field = f(o.field))
@4e6
4e6 / console
Last active August 29, 2015 14:04
Scala subtype check
scala> val fs = Field("string")
fs: Field[String] = Field(string)
scala> val fi = Field(1)
fi: Field[Int] = Field(1)
scala> val O = Update[Object]
O: Update[Object] = sparken.Update$$anon$1@4b41956d
// Runtime check
@4e6
4e6 / gist:5488205
Created April 30, 2013 11:46
Check if type is concrete
def f[T]: Unit = macro f_impl[T]
def f_impl[T: c.WeakTypeTag](c: Context): c.Expr[Unit] = {
import c.universe._
def isConcreteTypeSymbol(sym: Symbol) =
sym.isType && !sym.asType.isAbstractType
def isConcreteType(tpe: Type): Boolean = tpe match {
case NoPrefix =>
@4e6
4e6 / jsglr-layout
Created March 22, 2013 18:13
jsglr_layout ParseTableManager
jsglr-layout [scala-ast]± scala -cp org.spoofax.terms_1.0.0.201105211714.jar:bin/
Welcome to Scala version 2.10.0-20121205-235900-18481cef9b (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_17).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val tableMgr = new org.spoofax.jsglr_layout.io.ParseTableManager()
tableMgr: org.spoofax.jsglr_layout.io.ParseTableManager = org.spoofax.jsglr_layout.io.ParseTableManager@1b3821a6
scala> import java.io.File
import java.io.File
@4e6
4e6 / t6591_4.scala
Last active December 10, 2015 05:28
//import scala.reflect.runtime.universe._
import scala.reflect.runtime.{ currentMirror => m }
import scala.tools.reflect.ToolBox
import scala.tools.reflect.Eval
object Test extends App {
val tb = m.mkToolBox(options = "-Xprint:typer -Yshow-trees -Ydebug -Ytyper-debug")
class O { class I }
class A extends O {
@4e6
4e6 / Scraper.java
Created November 26, 2011 14:53
Test task from HireRight
package hr.the4e6;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
@4e6
4e6 / DuplicatesFinder.scala
Created August 8, 2011 23:45
Scala script to find duplicate resources in android projects
/*
* to run from console:
* $scala DuplicatesFinder.scala /path/to/android/project
*/
import java.io.File
import scala.xml._
val folder = args(0)
val project = new File(folder)