Skip to content

Instantly share code, notes, and snippets.

View vkostyukov's full-sized avatar

Vladimir Kostyukov vkostyukov

View GitHub Profile
/**
* 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
/**
* 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
* }
@vkostyukov
vkostyukov / ImplicitConnection.scala
Created April 9, 2014 15:49
An example of usage implicit parameters of anonymous functions in Scala: Finagle + RactiveMongo
/**
* 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.
@vkostyukov
vkostyukov / Routing.scala
Last active August 29, 2015 13:58
Another approach for routing reqs to Finagle's services
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))
}
@vkostyukov
vkostyukov / Coconut.scala
Last active August 29, 2015 14:12
CoconutDelivery
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
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)
}
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?
}
@vkostyukov
vkostyukov / finch-quckstart.md
Last active August 29, 2015 14:15
Quickstart
name := "finch-quickstart"

version := "0.0.0"

scalaVersion := "2.11.5"

libraryDependencies ++= Seq(
  "com.github.finagle" %% "finch-core" % "0.4.0"
)
@vkostyukov
vkostyukov / micro-finch.md
Last active August 29, 2015 14:16
Finch in Action (Micro-Finch)

History

There is a bunch of tickets/PRs related to the same underlying problem. It has started with two tickets:

  • [Issue 190][0] - composing routers with filters and services
  • [Issue 172][1] - mixing futures and services in the same endpoint/router

Then Jens has created a [PR 184][2] that indicated a problem of domain types in Finch. So, I created an [issue 204][3] to track the progress on this direction. More importantly, I've posted a first version of possible solution for the "Finch in Action" problem in [PR 206][4]. This document mostly describes an approach called "Micro Finch", that in my opinion solves the original problem.

The "Finch in Action" Problem

@vkostyukov
vkostyukov / prioritised-ops.scala
Last active August 29, 2015 14:16
A sane alternative to F1[A], ..., F22[A, B, ..., V]
// A type for a composite value
case class ~[+A, +B](a: A, b: B)
// An applicative functor
trait F[+A] { self =>
def value: A
def ~[B](that: F[B]): F[A ~ B] = new F[A ~ B] {
def value: A ~ B = new ~(self.value, that.value)
}
}