Skip to content

Instantly share code, notes, and snippets.

View vkostyukov's full-sized avatar

Vladimir Kostyukov vkostyukov

View GitHub Profile
trait R[+A] { self =>
type In
def apply(in: In): A
def map[B](f: A => B): R[B] = new R[B] {
type In = self.In
def apply(in: In) = f(self(in))
}
@vkostyukov
vkostyukov / commutative-ap.scala
Created March 23, 2015 01:57
Commutative Applicative Ops
trait M[R, A] { self =>
def apply(r: R): A
def map[B](f: A => B): M[R, B] = new M[R, B] {
def apply(r: R): B = f(self(r))
}
// left associativity
def flatMap[S, B](f: A => M[S, B])(implicit ev: S <:< R): M[S, B] =
new M[S, B] {
@vkostyukov
vkostyukov / Heap.scala
Last active August 29, 2015 14:18
Standard Binary Heap in a Functional Setting
/**
* A standard binary min-heap based on http://arxiv.org/pdf/1312.4666v1.pdf
*/
sealed trait Heap[+A] {
def min: A
def left: Heap[A]
def right: Heap[A]
def isEmpty: Boolean
@vkostyukov
vkostyukov / Playground.scala
Created May 12, 2015 17:12
Coproduct Routers in the Playground
package io.finch.playground
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.twitter.finagle.{Service, Filter, Httpx}
import com.twitter.util.{Future, Await}
import io.finch.{Endpoint => _, _}
import io.finch.micro._
import io.finch.request._
@vkostyukov
vkostyukov / ff.md
Last active August 29, 2015 14:23
Future Finch

Future Finch

Finch has just reached version 0.7 and we're still months from the first "stable" version (in quotes, since Finch has always been pretty stable: minimal and reasonable API changes, no major bugs/regressions found since 0.1) but's a very good time for us to look around and see where to swim.

This write-up is not a roadmap for 1.0, it's mostly an attempt to analyze Finch's Future and to choose a direction in wich the roadmap to 1.0 will be defined.

Low-Level Finch

While, Finch is built using purely functional abstractions, it's still might be described as "low-level" framework or library. Finch doesn't give the developers an answer on very simple question: "How to organize my codebase?". Frameworks usually answer it pretty well - "Here is the controller class. Implement those methods and you're all set.". Helping the developers with this sort of questions is clearly a major priority for Finch. Although, this doesn't mean that Finch will [be a framework][1] one day. The project philosophy wi

@vkostyukov
vkostyukov / perf.md
Last active October 13, 2015 04:08
Finch 0.8.5 Performace

GET JSON ARRAY

Benchmark                                   Mode Cnt          Score       Error   Unit

FinchCirce.getArr                           avgt  10         24.846 ±     0.678  ms/op
FinchCirce.getArr:·gc.alloc.rate.norm       avgt  10  183890756.229 ± 11616.394   B/op
FinagleJackson.getArr                       avgt  10         16.358 ±     1.075  ms/op
FinagleJackson.getArr:·gc.alloc.rate.norm   avgt  10  166581859.626 ± 14922.873   B/op
@vkostyukov
vkostyukov / finch-auth.scala
Last active October 17, 2015 17:57
Finch Auth
def auth0[A, B](doAuth: Request => Future[A])(e: Endpoint[B])(implicit
adjoin: PairAdjoin[A, B]
): Endpoint[adjoin.Out] = new Endpoint[adjoin.Out] {
def apply(input: Input): Option[(Input, () => Future[Output[adjoin.Out]])] =
e(input).map {
case (remainder, output) =>
(remainder, () => for {
a <- doAuth(input.request)
ob <- output()
} yield b.copy(value = adjoin(a, b.value)))
@vkostyukov
vkostyukov / HelloWorld.scala
Created November 11, 2015 13:30
HelloWorld
import com.twitter.finagle.Http
import com.twitter.util.{Future, Await}
import io.finch._
object Test1 extends App {
implicit val encodeMap: EncodeResponse[Map[String, String]] =
EncodeResponse("text/plain")(map =>
Buf.Utf8(map.toSeq.map(kv => kv._1 + ":" + kv._2).mkString("\n"))
)
@vkostyukov
vkostyukov / BigInteger.java
Last active December 11, 2015 05:49
Patch for java.math.BigInteger class that increases multiply performance by using Karatsuba algorithm
...
public BigInteger multiply(BigInteger val) {
if (val.signum == 0 || signum == 0)
return ZERO;
int n = Math.max(bitLength(), val.bitLength());
if (n > 1000)
return multiplyKaratsuba(val, n);
int[] result = multiplyToLen(mag, mag.length,
@vkostyukov
vkostyukov / Permutation.scala
Created January 26, 2013 08:54
Puzzle Example from CCI.
object Permutation extends App {
def generate(s: String): Array[String] = s.length() match {
case 1 => Array(s)
case 2 => Array("" + s(0) + s(1), "" + s(1) + s(0))
case _ =>
val first = s.head
var perms = generate(s.tail)
var result: Array[String] = Array.fill(perms.length * (perms(0).length() + 1)) { "" }
var offset = 0