Skip to content

Instantly share code, notes, and snippets.

View rockjam's full-sized avatar

Nikolai Tatarinov rockjam

View GitHub Profile
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))
@rockjam
rockjam / WebSocketClient.scala
Created August 20, 2016 13:34 — forked from casualjim/WebSocketClient.scala
A Netty based WebSocket client and server in scala
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}
@rockjam
rockjam / Main.scala
Created May 31, 2016 16:26 — forked from jkpl/Main.scala
Ways to pattern match generic types in Scala
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)"
}
// 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)
@rockjam
rockjam / NettyChannelFutureToScalaFuture.scala
Created April 23, 2016 11:00 — forked from viktorklang/NettyChannelFutureToScalaFuture.scala
Shows how you can use the Promise API of SIP-14 to bridge between Netty ChannelFutures and scala.concurrent.Future
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(
@rockjam
rockjam / gist:376f82abc5331dd36ebc
Created February 24, 2016 23:07
YASCS (Yet another SBT Cheat Sheet)
:= 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
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 {
@rockjam
rockjam / Trait
Last active August 29, 2015 14:10
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