再见,H公司

今天是我在H公司工作的最后一天。从20081月毕业以来,这是我第三家公司。与前两次离职是我主动寻求改变不同,这次是由于公司调整,把整个部门关掉了。

我在H公司工作了20个月,是我工作过的三家公司里时间最短的,但是收获却不少。H公司的确是一家很不错的公司,做事厚道,充分尊重员工并给予员工很大的自由度。而我所在的部门更像是一个大学实验室,同事之间关系融洽,有充足的资源和世界上性能最强大的服务器供我学习和使用。现在回头看看,这20个月自身的收获主要有以下几个方面:
(1)Performnace tuning的知识;
(2)操作系统相关知识:主要是Linux kernel开发,此外也接触和使用了FreeBSD
(3)Container技术:学习了DockerMesos的使用,以及Docker SwarmSwarmkit的源代码;
(4)函数式编程的相关知识。

最后,希望H公司可以越来越好!

P.S.,如果您有合适的system/performance software engineer的工作职位,欢迎联系我(nan#chinadtrace.org,把#换成@),谢谢!

Go语言的http.FileServer

看一下下面这段代码:

package main

import (
    "net/http"
)

func main() {
    // To serve a directory on disk (/tmp) under an alternate URL
    // path (/tmpfiles/), use StripPrefix to modify the request
    // URL's path before the FileServer sees it:
    http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
}

(1)http.Dir("/tmp")是利用本地tmp目录实现一个文件系统;
(2)http.FileServer(http.Dir("/tmp"))返回一个Handler,其用来处理访问本地"/tmp"文件夹的HTTP请求;
(3)http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp")))返回一个新的Handler,这个Handler用来处理HTTP请求(移除前缀是"/tmpfiles/"后的URL)。
总结一下,当要访问http://localhost/tmpfiles/a文件,实际访问的是本地/tmp/a文件。

Scala笔记(5)——apply method

本文选自Deeper Look at the Apply Method in Scala

In Scala, there is a language feature generally referred to as “The Apply Method” that has the following rules:

Any object that has an apply method can be called with the .apply omitted.
Functions are no more than objects.

Let’s take a look at an example. Given the following abbreviated definition of class Array, and an instance a,

class Array{
def apply(index:Int) = { …some code to get from the array… }
}

val a = new Array(whatever)

Then the following calls are essentially equivalent:

a.apply(7)
a(7)

什么是“Pure Function”

下列文字摘自The Definition of “Pure Function”

A pure function is a function that depends only on its declared inputs and its internal algorithm to produce its output. It does not read any other values from “the outside world” — the world outside of the function’s scope — and it does not modify any values in the outside world.

1-Pure-Function-Equation

C语言中的“#”和“##”

参考stackoverflow上的这篇帖子

#include <stdio.h>
#define f(a,b) a##b
#define g(a)   #a
#define h(a) g(a)

int main()
{
        printf("%s\n",h(f(1,2)));
        printf("%s\n",g(f(1,2)));
        return 0;
}

运行结果如下:

12
f(1,2)

#a会创建一个字符串:"a"a##b会创建一个新的tokenab。关于macro的处理,参考下列解释:

An occurrence of a parameter in a function-like macro, unless it is the operand of # or ##, is expanded before substituting it and rescanning the whole for further expansion. Because g’s parameter is the operand of #, the argument is not expanded but instead immediately stringified (“f(1,2)”). Because h’s parameter is not the operand of # nor ##, the argument is first expanded (12), then substituted (g(12)), then rescanning and further expansion occurs (“12”).

总结一下,对于function-like macro的参数,如果是#或者##的操作数,则会马上扩展为相应的字符串;否则会先扩展,再替换,循环此操作,直到完成。因此对于g(f(1,2)),会马上替换成"f(1,2)"这个字符串。而对于h(f(1,2)),由于其参数不是#..或者..#..,所以会先把f(1,2)扩展成12,然后替换变成g(12),最后变成"12"

FreeBSD kernel 笔记(13)——delaying execution

4delaying execution方法(选自:FreeBSD Device Drivers):

Sleeping Sleeping is done when you must wait for something to occur before you can proceed.
Event Handlers Event handlers let you register one or more functions to be executed when an event occurs.
Callouts Callouts let you perform asynchronous code execution. Callouts are used to execute your functions at a specific time.
Taskqueues Taskqueues also let you perform asynchronous code execution. Taskqueues are used for deferred work.

什么是“lambda”

下列文字摘自What is This “Lambda” You Speak Of?

lambda means “anonymous function,” and calculus means “a formal system”

Therefore, the term lambda calculus refers to “a formal way to think about functions.”

That same Wikipedia link states this:

“Lambda calculus provides a theoretical framework for describing functions and their evaluation. Although it is a mathematical abstraction rather than a programming language, it forms the basis of almost all functional programming languages today.”

When I first started learning about functional programming, I found these terms to be a little intimidating, but as with most FP terms, they’re just uncommon words for talking about “functions and their evaluation.”