推荐一篇介绍函数式编程(functional programming)的文章

说实话,对于函数式编程(functional programming),我一直云里雾里,不大明白。今天读到一篇文章:An introduction to functional programming,感觉写的不错,读完之后,至少觉得自己入门了。我把其中最核心的一段话摘录出来,并做了翻译:

When people talk about functional programming, they mention a dizzying number of “functional” characteristics. They mention immutable data, first class functions and tail call optimisation. These are language features that aid functional programming. They mention mapping, reducing, pipelining, recursing, currying and the use of higher order functions. These are programming techniques used to write functional code. They mention parallelization, lazy evaluation and determinism. These are advantageous properties of functional programs.  

Ignore all that. Functional code is characterised by one thing: the absence of side effects. It doesn’t rely on data outside the current function, and it doesn’t change data that exists outside the current function. Every other “functional” thing can be derived from this property. Use it as a guide rope as you learn.

译文:

当人们谈论起函数式编程,他们总会提到一堆令人眼花缭乱的“函数式”特性。他们提到“不可变数据”,“`first class functions`”和“尾调用优化”。这些是辅助函数式编程的语言特性。他们提到“`map/reduce`”,“流水线”,“递归”,“`currying`”和“高阶函数”。这些是写函数式代码的编程技术。他们提到“并行”,“惰性求值”和“确定性”。这些是函数式程序的优良特性。  

忽略上面提到的这些,函数式代码被一个特性所决定:没有副作用,即它不依赖于也不会改变当前函数以外的数据状态。其它所有“函数式特性”均由此特性衍生出来。  

使用screen命令产生并管理多个工作窗口

Unix/Linux下的screen命令一个很重要的功能就是可以在一个物理连接(SSH/telnet)终端(terminal)上创建并管理多个工作窗口(也称之为“virtual terminal”)。达到此目的可以有以下两种方式:

(1)启动screen命令进入一个工作窗口,进行一番操作后,使用ctrl-a d命令离开这个窗口,回到真实的终端。然后在真实终端再次启动screen命令,就可以启动下一个工作窗口。如下例所示:

[root@localhost ~]# screen
[detached from 5362.pts-0.localhost]
[root@localhost ~]# screen
[detached from 5378.pts-0.localhost]
[root@localhost ~]#
[root@localhost ~]# ps -ef | grep -i SCREEN
root      5362     1  0 06:19 ?        00:00:00 SCREEN
root      5378     1  0 06:20 ?        00:00:00 SCREEN
root      5394  5334  0 06:20 pts/0    00:00:00 grep --color=auto -i SCREEN
[root@localhost ~]# screen -ls
There are screens on:
        5378.pts-0.localhost    (Detached)
        5362.pts-0.localhost    (Detached)
2 Sockets in /var/run/screen/S-root.
[root@localhost ~]# ps -ef | grep 5378
root      5378     1  0 06:20 ?        00:00:00 SCREEN
root      5379  5378  0 06:20 pts/2    00:00:00 /bin/bash
root      5745  5722  0 21:45 pts/0    00:00:00 grep --color=auto 5378
[root@localhost ~]# ps -ef | grep 5362
root      5362     1  0 06:19 ?        00:00:00 SCREEN
root      5363  5362  0 06:19 pts/1    00:00:00 /bin/bash
root      5749  5722  0 21:50 pts/0    00:00:00 grep --color=auto 5362

可以看到会有两个SCREEN进程(进程号分别为53625378),同时每个进程都有一个/bin/bash的子进程,而这个/bin/bash子进程就是工作窗口(“virtual terminal”)。

(2)利用嵌套screen命令,也就是启动screen命令进入一个工作窗口以后,不会返回真正的终端,而是不断在当前的工作窗口中递归地调用screen命令。如下例所示:

[root@localhost ~]# screen
[detached from 5756.pts-0.localhost]
[root@localhost ~]# ps -ef | grep -i SCREEN
root      5756     1  0 21:52 ?        00:00:00 SCREEN
root      5817  5722  0 21:52 pts/0    00:00:00 grep --color=auto -i SCREEN
[root@localhost ~]# ps -ef | grep 5756
root      5756     1  0 21:52 ?        00:00:00 SCREEN
root      5757  5756  0 21:52 pts/1    00:00:00 /bin/bash
root      5772  5756  0 21:52 pts/2    00:00:00 /bin/bash
root      5787  5756  0 21:52 pts/3    00:00:00 /bin/bash
root      5802  5756  0 21:52 pts/4    00:00:00 /bin/bash
root      5819  5722  0 21:53 pts/0    00:00:00 grep --color=auto 5756

可以看到这种方式只会有一个SCREEN进程,所有的工作窗口(“virtual terminal”)都是由这一个进程产生的。

参考资料:
(1)Why is there only one SCREEN process in nested screen session?
(2)10 Screen Command Examples to Manage Linux Terminals