Skip to content

Instantly share code, notes, and snippets.

class Solution {
struct Ctx
{
int* array;
int len;
};
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
if((m+n) == 0)
return 0;
@binshuohu
binshuohu / akk-stream-graph-cycles.md
Last active November 17, 2015 14:23
Akka Stream Graph Cycles Illustrated

今天阅读akka streams文档时,Graph一节里最后一小段关于cycles, liveness and deadlocks的内容非常有趣。初次阅读,第一时间没有理解这部分内容,仔细想了一下才反应过来。这里通过画图来说明会比较容易理解。

原文档中的例子如下(稍稍修改了一下,方便讲解)

// WARNING! The graph below deadlocks!
RunnableGraph.fromGraph(FlowGraph.create() { implicit b =>
  import FlowGraph.Implicits._
  
  val merge = b.add(Merge[Int](2).withAttributes(Attributes.inputBuffer(1, 1)))       //use extremely small buffer
 val bcast = b.add(Broadcast[Int](2).withAttributes(Attributes.inputBuffer(1, 1))) //to better expose the problem
@binshuohu
binshuohu / 0.explanation.md
Last active November 21, 2015 10:53
A simple akka streams example

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

@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 / 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 / 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 / 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 / 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 / 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)