This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed trait Fragment | |
object Fragment { | |
case class Keep(value: String) extends Fragment | |
case class Expand(values: Seq[Fragment]) extends Fragment | |
} | |
def simpleParser = { | |
import fastparse.all._ | |
// there should be element inside brace expansion. word, or another brace expansion | |
def insideBraces = P(CharsWhile(c => c != ',' && c != '{' && c != '}').!.map(Fragment.Keep)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mojolly.io | |
import org.jboss.netty.bootstrap.ClientBootstrap | |
import org.jboss.netty.channel._ | |
import socket.nio.NioClientSocketChannelFactory | |
import java.util.concurrent.Executors | |
import org.jboss.netty.handler.codec.http._ | |
import collection.JavaConversions._ | |
import websocketx._ | |
import java.net.{InetSocketAddress, URI} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Main extends App { | |
AvoidLosingGenericType.run() | |
AvoidMatchingOnGenericTypeParams.run() | |
TypeableExample.run() | |
TypeTagExample.run() | |
} | |
class Funky[A, B](val foo: A, val bar: B) { | |
override def toString: String = s"Funky($foo, $bar)" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Overcoming type erasure with TypeTag, snippet from http://www.cakesolutions.net/teamblogs/ways-to-pattern-match-generic-types-in-scala | |
import scala.reflect.runtime.universe._ | |
def handle[A: TypeTag](a: A): Unit = | |
typeOf[A] match { | |
case t if t =:= typeOf[List[String]] => | |
// list is a string list | |
val r = a.asInstanceOf[List[String]].map(_.length).sum | |
println("strings: " + r) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object NettyFutureBridge { | |
import scala.concurrent.{ Promise, Future } | |
import scala.util.Try | |
import java.util.concurrent.CancellationException | |
import org.jboss.netty.channel.{ Channel, ChannelFuture, ChannelFutureListener } | |
def apply(nettyFuture: ChannelFuture): Future[Channel] = { | |
val p = Promise[Channel]() | |
nettyFuture.addListener(new ChannelFutureListener { | |
def operationComplete(future: ChannelFuture): Unit = p complete Try( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:= replaces key with another setting | |
+= appends a single element | |
++= concatenate another sequence | |
~= applies a function to the setting's previous value, producing a new value of the same type | |
<<= lets you compute a new value using the value(s) of arbitrary other keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Client { | |
val z:Face = new Impl() | |
val result = z.foo(1,"21") | |
} | |
trait Face { | |
def foo(x:Int, y:String):String | |
} | |
class Impl extends Face with Helpers { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo { | |
private val x = new Trait1 {} | |
def bar(x:Int, y:String, z:Boolean) = { | |
x.bar(x, y, z) | |
} | |
} | |
trait Trait1 { | |
def bar(x:Int, y:String, z:Boolean) = //implementation | |
def helper1(x:Int) = //implementation |