Skip to content

Instantly share code, notes, and snippets.

View lihaoyi's full-sized avatar

Li Haoyi lihaoyi

View GitHub Profile
View gist:7c997a2e7cedc91a8efd0356299c91a0
lihaoyi mill$ javap -c -l out/integration/feature/codesig-simple/local/workspaceDir.dest/out/mill-build/compile.dest/classes/millbuild/build\$crossOuter\$\$anonfun\$7.class
Compiled from "build.sc"
public final class millbuild.build$crossOuter$$anonfun$7 extends scala.runtime.AbstractFunction1<java.lang.Object, scala.Tuple2<java.lang.Class<millbuild.build$crossOuter$$anonfun$7$CrossOuter$macro$9$1>, scala.Function1<mill.define.Ctx, millbuild.build$crossOuter$$anonfun$7$CrossOuter$macro$9$1>>> implements java.io.Serializable {
public final scala.Tuple2<java.lang.Class<millbuild.build$CrossOuter>, scala.Function1<mill.define.Ctx, millbuild.build$CrossOuter>> apply(int);
Code:
0: new #39 // class scala/Tuple2
3: dup
4: ldc #26 // class millbuild/build$crossOuter$$anonfun$7$CrossOuter$macro$9$1
6: aload_0
7: iload_1
View Main.scala
import java.io.File
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import scala.io.Source
import collection.mutable
case class Decl(val index: Int,
val cls: String,
val name: String,
View QuillProblem.sc
import $ivy.`io.getquill::quill-jdbc:3.5.1`, io.getquill._
val ctx: PostgresJdbcContext[LowerCase.type] = ???
import ctx._
case class CountryLanguage(countryCode: String,
language: String,
isOfficial: Boolean,
percentage: Double)
case class City(id: Int,
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 October 30, 2017 13:49
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"))
)
)
}