name := "finch-quickstart"
version := "0.0.0"
scalaVersion := "2.11.5"
libraryDependencies ++= Seq(
"com.github.finagle" %% "finch-core" % "0.4.0"
)
This file contains hidden or 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
trait F[A] { | |
def apply(foo: Foo): X[A] | |
def map(fn: A => B): F[B] | |
def flatMap(fn: A => F[B]): F[B] | |
def fooMap(fn: A => X[B]): F[B] // how is this called? constMap? | |
} |
This file contains hidden or 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
trait R[I, O] { def apply(i: I): O } // reader | |
trait ToString[I] { def apply(i: I): String } // converter | |
case class Foo(s: String) { | |
def apply[I](implicit toStr: ToString[I]): R[I, String] = new R[I, String] { | |
def apply(i: I) = s + toStr(i) | |
} | |
def apply[I](i: I)(implicit toStr: ToString[I]): String = (apply(toStr))(i) | |
} |
This file contains hidden or 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
val energy = Console.readLine().toInt | |
val streams: List[(Int, Int, Int)] = | |
Iterator.continually(Console.readLine).takeWhile(_.nonEmpty).map { s: String => | |
val stream = s.split(" ") | |
(stream(0).toInt, stream(1).toInt, stream(2).toInt) | |
} toList | |
val (_, end, _) = streams.maxBy { | |
case (_, to, _) => to |
This file contains hidden or 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 DelimEncoder(delim: Char) extends SimpleChannelHandler { | |
override def writeRequested(ctx: ChannelHandlerContext, evt: MessageEvent) = { | |
val newMessage = evt.getMessage match { | |
case m: String => m + delim | |
case m => m | |
} | |
Channels.write(ctx, evt.getFuture, newMessage, evt.getRemoteAddress) | |
} | |
} |
This file contains hidden or 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
val routes = User.routes orElse | |
Order.routes | |
// would be great having: | |
// val backend = RoutingService.byMethodAndPathObjct(routes) | |
val backend = new RoutingService( | |
new PartialFunction[Request, Service[Request, Response]] { | |
def apply(req: Request) = routes((req.method, Path(req.path))) | |
def isDefinedAt(req: Request) = routes.isDefinedAt((req.method), Path(req.path)) | |
} |
This file contains hidden or 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
/** | |
* All the magic is done by Scala compiler. | |
*/ | |
trait ImplicitMongoConnection[A] { | |
def withConnection(block: MongoConnection => A) | |
(implicit connection: MongoConnection): A = block(s) | |
} | |
/** | |
* This might be a separate file with service implementation. |
This file contains hidden or 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
/** | |
* Reactive Converter of Scala's Future[A] object to com.twitter.util.Future[A] object. | |
* | |
* Usage: | |
* | |
* val s = new Service[Req, Rep] with FutureConverter { | |
* def apply(req: Req): Future[Rep] = { | |
* val f = ... // a Scala Future | |
* f.toTwitterFuture | |
* } |
This file contains hidden or 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
import com.twitter.finagle.builder.ServerBuilder | |
import com.twitter.finagle.http.path._ | |
import com.twitter.finagle.http.service.RoutingService | |
import com.twitter.finagle.Service | |
import com.twitter.finagle.http._ | |
import com.twitter.util.Future | |
import java.net.InetSocketAddress | |
import scala.util.parsing.json.JSONObject | |
object Main extends App { |
This file contains hidden or 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
/** | |
* This is an immutable builder patter implemented in Scala within OO style. | |
* | |
* There is might be a better approach (i.e., pattern matching, case classes, etc) | |
* in implementing this idea within FP style, but the overall picture is robust: | |
* | |
* (a) there is no mutable state | |
* (b) it's a pure FSM (Finite State Machine) | |
* | |
* I'll try to keep this gist updated along with any new ideas regarding this |