Skip to content

Instantly share code, notes, and snippets.

Li Haoyi lihaoyi

Block or report user

Report or block lihaoyi

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:5ae1d6b544d65fb8190534e8c13b8de7

Provisional benchmarks of AST-free serialization puts my WIP branch of uPickle about ~40% faster than circe on my current set of ad-hoc benchmarks, if the encoders/decoders are cached (bigger numbers is better)

playJson Read 2761067
playJson Write 3412630
circe Read 6005895
circe Write 5205007
upickleDefault Read 4543628
upickleDefault Write 3814459
upickleLegacy Read 8393416
View ParseArgs.scala
package mill.main
import mill.util.EitherOps
import fastparse.all._
import mill.define.Segment
import mill.main.ParseArgs.Fragment
object ParseArgs {
def apply(scriptArgs: Seq[String])
View compile.scala
def compileScala(scalaVersion: String,
sources: Path,
compileClasspath: Seq[Path],
scalacOptions: Seq[String],
javacOptions: Seq[String],
outputPath: Path): PathRef = {
val compileClasspathFiles = compileClasspath.map(_.toIO).toArray
pprint.log(compileClasspathFiles)
val binaryScalaVersion = scalaVersion.split('.').dropRight(1).mkString(".")
View Foo.sc
val compilerCache = new CompilerCache(10)
def compileScala(scalaVersion: String,
sources: Path,
compileClasspath: Seq[Path],
outputPath: Path): PathRef = {
val binaryScalaVersion = scalaVersion.split('.').dropRight(1).mkString(".")
def grepJar(s: String) = {
compileClasspath
.find(_.toString.endsWith(s))
.getOrElse(throw new Exception("Cannot find " + s))
View Foo.sc
@ case class Cross[T](items: T*){
def flatMap[V](f: T => Cross[V]): Cross[(T, V)] = {
val flattened = for{
i <- items
k <- f(i).items
} yield (i, k)
Cross(flattened:_*)
}
def map[V](f: T => V): Cross[(T, V)] = {
Cross(items.map(i => i -> f(i)):_*)
View Applicative.scala
import scala.reflect.macros.blackbox.Context
import language.experimental.macros
object Applicative {
def apply[T](t: T): Option[T] = macro Applicative.impl[T]
def impl[T: c.WeakTypeTag](c: Context)(t: c.Expr[T]): c.Expr[Option[T]] = {
import c.universe._
def rec(t: Tree): Iterator[c.Tree] = Iterator(t) ++ t.children.flatMap(rec(_))
val bound = collection.mutable.Buffer.empty[(c.Tree, ValDef)]
val defsWithinMacro = rec(t.tree).filter(_.isDef).map(_.symbol).toSet
@lihaoyi
lihaoyi / Test.scala
Created Oct 30, 2017
Resolving jars with Coursier and compiling Scala with the Zinc incremental compiler, in a self-contained Ammonite script
View Test.scala
import play.api.libs.json.Json
object Main{
def main(args: Array[String]): Unit = {
println(
Json.prettyPrint(
Json.toJson(Seq("Hello", "World", "Cow"))
)
)
}
View gist:51e4b50e550d32154a62c3e7cd401efd
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.! )
View gist:6d70882591494b76cc1e572c1f46319c
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
SBT ClasspathFilter can't load java.lang.String classfiles?
View gist:76929bfe2120732ebc9357abe7dcc92b
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)
You can’t perform that action at this time.