Skip to content

Instantly share code, notes, and snippets.

//https://bitbucket.org/snippets/centaur/6K4kR
case class Point(x: Int, y: Int)
def isSquare(p1: Point, p2: Point, p3: Point, p4: Point): Boolean = {
val l = List(p1, p2, p3, p4).combinations(2).map{
case List(a, b) =>
val delta_x = a.x - b.x
val delta_y = a.y - b.y
delta_x * delta_x + delta_y * delta_y
}.toList.sorted
@binshuohu
binshuohu / demo.scala
Created May 6, 2016 05:39
how to modularize scala code properly?
trait FooComponent {
def fooService: FooService
trait FooService {
def query: String
}
}
trait ConcreteFooComponent extends FooComponent {
val fooService = ???
@binshuohu
binshuohu / worksheet.scala
Last active April 29, 2016 07:13
merge rx observable
import rx.lang.scala._
import rx.lang.scala.subjects.ReplaySubject
import scala.concurrent.Promise
import scala.concurrent.ExecutionContext.Implicits.global
val r = ReplaySubject[Int]()
r.onNext(1)
r.onNext(2)
@binshuohu
binshuohu / Main.scala
Last active December 8, 2015 13:13
separate computation from io
package com.vpon.slickit
import java.util.concurrent.Executors
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext, Future}
sealed trait Task {
def execute(parameter: String): String
}
@binshuohu
binshuohu / 1-source.scala
Created December 8, 2015 13:01
refactor a function
import spray.http._
object Foo {
def collectQueryParameters(query: Uri.Query, headers: List[HttpHeader]) = {
val contentType = headers.filter(_.name == "Content-Type")
val referrer = headers.filter(_.name == "Referrer")
(contentType.isEmpty, referrer.isEmpty) match {
case (false, false) => {
val cookie = headers.filter(_.name == "Cookie")
val contentTypeHead = contentType.head
@binshuohu
binshuohu / what-happens.md
Created December 8, 2015 12:58
what happens when typing a url in a browser and hit return.
  1. 输入url.
  2. 浏览器尝试解析域名得到响应的ip. 浏览器会依次在浏览器, OS, 路由器上查询DNS缓存, 如果找到就可以停止继续查询. 如果没有找到对应域名的DNS缓存, 会发起一个对DNS查询的递归搜索.
  3. 获得对应服务器的IP后, 浏览器尝试建立一个TCP连接.
  4. 连接建立好之后, 浏览器发起一个HTTP GET请求, header中的Content-Type为'text/html'.
  5. web server接到这个HTTP请求后, 会尝试将请求转换为响应, 这中间可能包含了其他业务逻辑. 5.1 如果web server配置了反向代理, web server会根据uri将此请求路由到响应的服务器, 等待其响应后再将对应的HTTP Response转发返回给浏览器.
  6. 浏览器拿到HTTP响应后, 检查其Status Code, Content-type, encoding等包含在header中的信息, 然后尝试从body中解析html.
  7. 浏览器解析完毕html的基本结构后, 根据情况会发送额外的HTTP请求去获取其它还需要的资源, 例如css, js以及图片.

从WireShark抓包来看, 访问www.vpon.com.tw时, 在第一条HTTP Response返回了200状态码后, 浏览器又通过额外的HTTP GET去获取了js, css以及图片等静态资源. 此外还发送了两条POST请求.

@binshuohu
binshuohu / akk-stream-actor-integration-bug?.scala
Last active November 29, 2015 12:10
Status.Success doesn't complete the stream
package sample.stream
import akka.actor.ActorSystem
import akka.stream.{ OverflowStrategy, ActorMaterializer }
import akka.stream.scaladsl._
import akka.actor.Status
import scala.util.{ Failure, Success }
object BasicTransformation {
@binshuohu
binshuohu / main.cpp
Created November 27, 2015 13:28
simple object pool
#include <memory>
#include <vector>
#include <functional>
#include <iostream>
template <class T>
class SimpleObjectPool
{
public:
using DeleterType = std::function<void(T*)>;
@binshuohu
binshuohu / exp.scala
Created November 25, 2015 06:27
test whether implicit evidence needs both covariance and contravariance
object Crap extends App {
sealed abstract class <=<[From, +To] extends (From => To) {
}
implicit def conforms[A]: <=<[A, A] = new <=<[A, A] {
def apply(from: A): A = ???
}
class A[T] {
def foo[U](u: U)(implicit ev: <=<[U, T]) = ()
@binshuohu
binshuohu / 0.explanation.md
Last active November 21, 2015 10:53
A simple akka streams example

下面是如何使用 Akka Streams 过滤日志并汇总上传总量的代码示例