Skip to content

Instantly share code, notes, and snippets.

View lihaoyi's full-sized avatar

Li Haoyi lihaoyi

View GitHub Profile
haoyi-mbp:workbench-example-app haoyi$ cat ~/Dropbox\ \(Personal\)/Archive/Coding/scala-js-docs/build.sbt
libraryDependencies += "org.jsoup" % "jsoup" % "1.7.3"
haoyi-mbp:workbench-example-app haoyi$ cat ~/Dropbox\ \(Personal\)/Archive/Coding/scala-js-docs/src/main/scala/Main.scala
import java.io.File
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import scala.io.Source
import collection.mutable
val space = R( CharSets(" \n").rep1 )
val digits = R( CharSets('0' to '9').rep1 )
val exponent = R( CharSets("eE") ~ CharSets("+-").? ~ digits )
val fractional = R( "." ~ digits )
val integral = R( "0" | CharSets('1' to '9') ~ digits.rep )
val number = R( "?".? ~ integral ~ fractional.? ~ exponent.? )
val `null` = R( "null" )
val `false` = R( "false" )
val `true` = R( "true" )
val hexDigit = R( CharSets('0'to'9', 'a'to'f', 'A'to'F') )
@ load.ivy("com.lihaoyi" %% "fastparse" % "0.1.4")
@ import fastparse._
import fastparse._
@ val parser = P( "a".rep1.!.flatMap(s => ("b" * s.length) ~ ("c" * s.length)) )
parser: fastparse.Parser[Unit] = parser
@ parser.parse("aaa")
res4: fastparse.core.Result[Unit] = Failure(parser:0 / "bbb":3 ..."", false)
find ~ -name test3* -ok rm {}\;
find ~ -name test3* -ok rm {}\;
{
import javax.sound.midi._
val synth = MidiSystem.getSynthesizer()
synth.open()
def note(i: Int) = {
val channels = synth.getChannels()
channels(0).noteOn(i, 100)
Thread.sleep(250)
channels(0).noteOff(i)
}
Welcome to Scala version 2.11.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import scala.reflect.macros.blackbox.Context; import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
import scala.language.experimental.macros
scala> def applyImpl(c: Context)(expr: c.Expr[Unit]): c.Expr[Nothing] = {
| import c.universe._
Bad
def ++(xs: GenTraversableOnce[A]): ArrayBuffer[A]
def ++[B](that: GenTraversableOnce[B]): ArrayBuffer[B]
def ++:[B >: A, That](that: collection.Traversable[B])(implicit bf: CanBuildFrom[ArrayBuffer[A], B, That]): That
def ++:[B](that: TraversableOnce[B]): ArrayBuffer[B]
def +:(elem: A): ArrayBuffer[A]
def -(elem1: A, elem2: A, elems: A*): ArrayBuffer[A]
def -(elem: A): ArrayBuffer[A]
def --(xs: GenTraversableOnce[A]): ArrayBuffer[A]
import tools.nsc.plugins.{Plugin, PluginComponent}
import tools.nsc.Global
import tools.nsc.transform.{TypingTransformers, InfoTransform, Transform}
import tools.nsc.symtab.Flags
class MyPlugin(val global: Global) extends Plugin {
val name = "annotations-inject-implicit"
val description = "generates code which adds an implicit parameter of type BindingModule when an AutoInjectable trait is mixed in"
val components = List[PluginComponent](AnnotationsInjectComponent)
>>> class Foo:
... x = 1
... y = (x, lambda: x)
...
>>> Foo().x
1
>>> Foo().y
(1, <function <lambda> at 0x10754d0c8>)
>>> Foo().y[0]
1