Skip to content

Instantly share code, notes, and snippets.

View olafurpg's full-sized avatar

Ólafur Páll Geirsson olafurpg

View GitHub Profile
object CC {
def connectedComponent(board: Board, color: Cell, toVisit: List[Point], component: List[Point] = List[Point]()): List[Point] = {
require(board.isValid &&
board.isValidPoints(toVisit) &&
board.isValidPoints(component) &&
isComponent(board, addValidElements(board, component, toVisit), color)
)
if (toVisit.isEmpty)
component
else if (component.contains(toVisit.head))
object CC {
def connectedComponent(board: Board, color: Cell, toVisit: List[Point], component: List[Point] = List[Point]()): List[Point] = {
require(board.isValid &&
board.isValidPoints(toVisit) &&
board.isValidPoints(component) &&
isComponent(board, addValidElements(board, component, toVisit), color)
)
if (toVisit.isEmpty)
component
else if (component.contains(toVisit.head))
package go.core
import go.util.conversions._
import go.util.Logic._
import go.collection.GoSet
import go.core.definitions._
import leon.annotation.library
import leon.lang._
import leon.collection._
import leon.annotation._
@ def numberOfStatements(tree: Tree): Int = {
var n = 0
def loop(x: Tree): Unit = {
x match {
case t: Source => n += t.stats.length
case t: internal.ast.Pkg => n += t.stats.length
case t: internal.ast.Term.Match => n += t.cases.length
case t: internal.ast.Term.PartialFunction => n += t.cases.length
case t: internal.ast.Term.Block => n += t.stats.length
case t: internal.ast.Template if t.stats.isDefined =>
@olafurpg
olafurpg / PrettyPrintHack.scala
Last active March 3, 2016 12:38
Pretty-print/reformat AST trees in Scala using clang-format
ollie-scalafmt@ import scala.sys.process._
import scala.sys.process._
ollie-scalafmt@ import java.io.ByteArrayInputStream
import java.io.ByteArrayInputStream
ollie-scalafmt@ formatMe
res28: String = """
Defn.Object(Nil, Term.Name("State"), Template(Nil, Seq(Ctor.Ref.Name("ScalaFmtLogger")), Term.Param(Nil, Name.Anonymous(), None, None), Some(Seq(Defn.Val(Nil, Seq(Pat.Var.Term(Term.Name("start"))), None, Term.Apply(Term.Name("State"), Seq(Lit(0), Term.Name("identity"), Term.ApplyType(Term.Select(Term.Name("Vector"), Term.Name("empty")), Seq(Type.Name("Split"))), Term.ApplyType(Term.Select(Term.Name("Vector"), Term.Name("empty")), Seq(Type.Name("State"))), Lit(0), Term.ApplyType(Term.Select(Term.Name("Vector"), Term.Name("empty")), Seq(Type.Apply(Type.Name("Indent"), Seq(Type.Name("Num"))))), Lit(0)))), Defn.Def(Nil, Term.Name("reconstructPath"), Nil, Seq(Seq(Term.Param(Nil, Term.Name("toks"), Some(Type.Apply(Type.Name("Array"), Seq(Type.Name("FormatToken")))), None), Term.Param(Nil, Term.Name("splits"), Some(T
ollie-scalameta@ """case x: (Int, Int) => 1""".parse[Case]
scala.meta.parsers.common.ParseException: identifier expected but integer literal found at 22..23
ollie-scalameta@ """def macroTransform(annottees: Any*) = macro Cached.cachedImpl""".parse[Stat]
scala.meta.parsers.common.ParseException: macros must have explicitly specified return types at 4..18
ollie-scalameta@ """for { ident @ (__ : Int) <- List(1, 2) } yield ident + 1""".parse[Stat]
scala.meta.parsers.common.ParseException: ) expected but : found at 18..19
ollie-scalameta@ """import Predef.{ any2stringadd => _, _ => _ }""".parse[Stat]
@olafurpg
olafurpg / more-scalameta-bugs.scala
Last active April 15, 2016 16:11
Remaining bugs in scala.meta parser
ollie-scalafmt@ """foo(1, b = true, js = xs: _*)""".parse[Stat]
scala.meta.parsers.common.ParseException: ) expected but identifier found at 27..28
ollie-scalafmt@ """case a: Array[_] ⇒ format(t, (a map (_.asInstanceOf[AnyRef]): _*))""".parse[Case]
scala.meta.parsers.common.ParseException: ) expected but identifier found at 63..64
ollie-scalafmt@ """(new t1785)[Int](1)""".parse[Stat]
scala.meta.parsers.common.ParseException: ; expected but [ found at 11..12
def genArrayInstanceTests(tree: LinkedClass): js.Tree = {
import Definitions._
import TreeDSL._
implicit val pos = tree.pos
val className = tree.name.name
val displayName = decodeClassName(className)
val objParam = js.ParamDef(Ident("obj"), rest = false)
@olafurpg
olafurpg / &&.scala
Created May 11, 2016 13:27
Having fun with Scala extractor objects. The && combinator can be used to merge multiple extractors into one MEGA extractor.
object && {
def unapply[A](e: A) = Some(e -> e)
def test() {
val greaterThan10 = Find[Int](_ > 10)
List(1, 2, 11, 3) match {
// All must succeed
case greaterThan10(ten) && Last(last) && Head(head) =>
println(ten) // 11
println(last) // 3
object Test extends App {
import scala.meta._
import scala.meta.tokens.Token.Comment
val tree = """
|package io.buildo.baseexample
|
|package enums
|
|import ingredients.caseenum.annotations.enum
|