Skip to content

Instantly share code, notes, and snippets.

View lihaoyi's full-sized avatar

Li Haoyi lihaoyi

View GitHub Profile
package fasterparser
import scala.collection.mutable
object Parse {
def main(args: Array[String]): Unit = {
def hello[_:Ctx] = P( "hello" )
def world[_:Ctx] = P( "world" )
def helloWorld[_:Ctx] = P( hello.! ~ (" " | ",").rep ~ world.! )
java.lang.IllegalArgumentException: Could not find proxy for i: Int in List(value i, method apply$mcVI$sp, <$anon: Function1>, method doPhi, class Thread, package rt, package metascala, package <root>) (currentOwner= value x1 )
at scala.tools.nsc.transform.LambdaLift$LambdaLifter.scala$tools$nsc$transform$LambdaLift$LambdaLifter$$searchIn$1(LambdaLift.scala:326)
at scala.tools.nsc.transform.LambdaLift$LambdaLifter.scala$tools$nsc$transform$LambdaLift$LambdaLifter$$searchIn$1(LambdaLift.scala:331)
at scala.tools.nsc.transform.LambdaLift$LambdaLifter.scala$tools$nsc$transform$LambdaLift$LambdaLifter$$searchIn$1(LambdaLift.scala:331)
at scala.tools.nsc.transform.LambdaLift$LambdaLifter.scala$tools$nsc$transform$LambdaLift$LambdaLifter$$searchIn$1(LambdaLift.scala:331)
at scala.tools.nsc.transform.LambdaLift$LambdaLifter.scala$tools$nsc$transform$LambdaLift$LambdaLifter$$searchIn$1(LambdaLift.scala:331)
at scala.tools.nsc.transform.LambdaLift$LambdaLifter.scala$tools$nsc$transform$LambdaLift$LambdaLifter$$s
@lihaoyi
lihaoyi / gist:76929bfe2120732ebc9357abe7dcc92b
Created May 29, 2017 08:14
SBT ClasspathFilter can't load java.lang.String classfiles?
lihaoyi test$ tree
.
└── Foo.scala
0 directories, 1 file
lihaoyi test$ cat Foo.scala
object Main{
def main(args: Array[String]): Unit = {
println(getClass.getClassLoader.getResourceAsStream("java/lang/String.class"))
println(getClass.getClassLoader.getClass)
lihaoyi test$ cat Test.scala
object Test{
def main(args: Array[String]): Unit = {
println(List(1, 2, 3).map(_ + 1))
println(List(1, 2, 3).map(_ + 1))
println(List(1, 2, 3).map(_ + 1))
println(List(1, 2, 3).map(_ + 1))
println(List(1, 2, 3).map(_ + 1))
}
}
package hello
import java.io.ByteArrayOutputStream
import chisel3._
import chisel3.iotesters.{Driver, PeekPokeTester}
sealed trait Op
object Op{
case class GotoIfZero(dest: Byte, valueAddr: Byte) extends Op
@lihaoyi
lihaoyi / debug.scala
Last active April 10, 2019 23:39
Scala.js pprint debug
def debug[T](value: sourcecode.Text[T], tag: String = "")
(implicit pprinter: pprint.PPrint[T],
path: sourcecode.Enclosing,
line: sourcecode.Line,
cfg: Config = pprint.Config.Colors.PPrintConfig)= {
val titleIter = path.value.split(" |#|\\.").filter(!_.contains("$")).last
val tagIter =
if (tag == "") ""
else " " + pprint.tokenize(tag).mkString
@lihaoyi
lihaoyi / gist:1ebdd89b065a741a1ea72b5d780b9ab1
Last active January 2, 2017 20:01
Setting up a "Walk through pre-loaded example snippets every time you press spacebar" experience in the Ammonite REPL
lihaoyi Ammonite$ amm
Loading...
Welcome to the Ammonite Repl 0.8.0
(Scala 2.11.8 Java 1.8.0_112)
@ {
val funnyFilter = ammonite.terminal.Filter.simple(" ")((b, c, m) =>
if (FunnyFrontEnd.nextLine == Nil) (b.take(c) ++ " " ++ b.drop(c), c+1)
else {
val head :: tail = FunnyFrontEnd.nextLine
FunnyFrontEnd.nextReader = new java.io.StringReader(head)
package demo
import scala.tools.nsc.io.AbstractFile
import scala.tools.nsc.{Global, Phase}
import scala.tools.nsc.plugins.{Plugin, PluginComponent}
class DemoPlugin(val global: Global) extends Plugin {
import global._
override def init(options: List[String], error: String => Unit): Boolean = true

PSA: if anyone is wondering to how to add debuggability to your Scalatags fragments to figure out where stuff comes from, you can do it with the SourceCode library (which is already pulled in by Scalatags):

  def tagged(implicit name: sourcecode.Enclosing, line: sourcecode.Line) =
    data.enclosing := (name.value + ":" + line.value)

Use e.g.

val searchForm = form(
 tagged,
package test
package grepgit.core
import scala.collection.mutable
import scala.reflect.ClassTag
/**
* A Generator of elements of type [[A]].
*
* [[Generator]] is basically the inverse of