Skip to content

Instantly share code, notes, and snippets.

View lihaoyi's full-sized avatar

Li Haoyi lihaoyi

View GitHub Profile
@ 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]
>>> class Foo:
... x = 1
... y = (x, lambda: x)
...
>>> Foo().x
1
>>> Foo().y
(1, <function <lambda> at 0x10754d0c8>)
>>> Foo().y[0]
1
>>> def foo():
... x = 1
... y = (x, lambda: x)
... return (x, y)
...
>>> foo()[1][1]()
1
>>> class Foo:
... x = 1
... y = x
... print y
... class Bar:
... z = y
... print z
...
1
Traceback (most recent call last):
@lihaoyi
lihaoyi / method.py
Last active November 14, 2015 05:44
>>> a = 1
>>> class Foo:
... a = 2
... print a
... def bar(self):
... print a
...
2
>>> Foo().bar()
1