Wednesday, January 13, 2016

Functional Thinking




public Map wordFreq(String words) {
    TreeMap<String, Integer> wordMap = new TreeMap<>();
    regexToList(words, "\\w+").stream()
            .map(w -> w.toLowerCase())
            .filter(w -> !NON_WORDS.contains(w))
            .forEach(w -> wordMap.put(w, wordMap.getOrDefault(w, 0) + 1));
    return wordMap;
}

I can perform the required operations one after the other, in the same way that I think about the problem.
Imperative programming often forces you to complect your tasks so that you can fit them all within a single loop, for efficiency. Functional programming via higher-order functions such as map() and filter() allows you to elevate your level of abstraction, seeing problems with better clarity.

Life’s too short for malloc.

OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts.

OO: encapsulation, scoping, visibility, and other mechanisms exist to exert fine-grained control over who can see and change state
def firstIndexOfAny(input : String, searchChars : Seq[Char]) : Option[Int] = {
  def indexedInput = (0 until input.length).zip(input)
  val result = for (pair <- indexedInput;
                    char <- searchChars;
                    if (char == pair._2)) yield (pair._1)
  if (result.isEmpty)
    None
  else
    Some(result.head)
}

Returning a lazy list of matches
def indexOfAny(input : String, searchChars : Seq[Char]) : Seq[Int] = {
  def indexedInput = (0 until input.length).zip(input)
  for (pair <- indexedInput;
       char <- searchChars;
       if (char == pair._2)) yield (pair._1)
}
val result = employees
  .filter(_.length() > 1)
  .map(_.capitalize)
  .reduce(_ + "," + _)

names.stream().filter(name -> name != null).filter(name -> name.length() > 1).map(name -> capitalize(name)).collect(Collectors.joining(","));
Scala:  .par, Java: .parallelStream()

One of Clojure’s selling points is that it removes concurrency as a developer concern just as Java removed garbage collection. The fact that Clojure developers use map instead of iteration means that they automatically receive upgraded abilities.

Focus on results over steps.
    public static IntStream factorsOf(int number) {
        return range(1, number + 1)
                .filter(potential -> number % potential == 0);
    }

    public static int aliquotSum(int number) {
        return factorsOf(number).sum() - number;
    }

The reduce function is generally used when you need to supply an initial value for the accumulator, whereas fold starts with nothing in the accumulator.
factors.foldLeft(fj.function.Integers.add, 0)

right folds can operate on infinite lists whereas left folds cannot.

    static public int addOnlyOddNumbersIn(List<Integer> numbers) {
        return numbers.foldLeft(new F2<Integer, Integer, Integer>() {
            public Integer f(Integer i1, Integer i2) {
                return (!(i2 % 2 == 0)) ? i1 + i2 : i1;
            }
        }, 0);

List(1, 2, 3, -4, 5, 6, 7, 8, 9, 10) takeWhile (_ > 0)
words dropWhile (_ startsWith "t")
List(List(1, 2, 3), List(4, 5, 6), List(7, 8, 9)) flatMap (_.toList)
words flatMap (_.toList)

List.range(1, 10) reduceLeft((a, b) => a + b)
List.range(1, 10).reduceLeft(0)(_ + _)
List.range(1, 10) reduceRight(_ - _)
words.reduceLeft((a, b) => if (a.length > b.length) a else b)
List.range(1, 10).foldLeft(0)(_ + _)
In Scala, the signature of reduceLeft[B >: A](op: (B, A) => B): B shows that the only parameter that’s expected is the function to combine elements. The initial value is expected to be the first value in the collection. In contrast, the signature of foldLeft[B](z: B)(op: (B, A) => B): B indicates an initial seed value for the result, so you can return types that are different from the type of the list elements.

http://www.ruanyifeng.com/blog/2012/04/functional_programming.html
它属于"结构化编程"的一种,主要思想是把运算过程尽量写成一系列嵌套的函数调用。举例来说,现在有这样一个数学表达式:
  (1 + 2) * 3 - 4
传统的过程式编程,可能这样写:
  var a = 1 + 2;
  var b = a * 3;
  var c = b - 4;
函数式编程要求使用函数,我们可以把运算过程定义为不同的函数,然后写成下面这样:
  var result = subtract(multiply(add(1,2), 3), 4);
1. 函数是"第一等公民"
所谓"第一等公民"(first class),指的是函数与其他数据类型一样,处于平等地位,可以赋值给其他变量,也可以作为参数,传入另一个函数,或者作为别的函数的返回值。
2. 只用"表达式",不用"语句"
"表达式"(expression)是一个单纯的运算过程,总是有返回值;"语句"(statement)是执行某种操作,没有返回值。函数式编程要求,只使用表达式,不使用语句。也就是说,每一步都是单纯的运算,而且都有返回值。
原因是函数式编程的开发动机,一开始就是为了处理运算(computation),不考虑系统的读写(I/O)。"语句"属于对系统的读写操作,所以就被排斥在外。
当然,实际应用中,不做I/O是不可能的。因此,编程过程中,函数式编程只要求把I/O限制到最小,不要有不必要的读写行为,保持计算过程的单纯性。
3. 没有"副作用"
所谓"副作用"(side effect),指的是函数内部与外部互动(最典型的情况,就是修改全局变量的值),产生运算以外的其他结果。
函数式编程强调没有"副作用",意味着函数要保持独立,所有功能就是返回一个新的值,没有其他行为,尤其是不得修改外部变量的值。
4. 不修改状态
上一点已经提到,函数式编程只是返回新的值,不修改系统变量。因此,不修改变量,也是它的一个重要特点。
在其他类型的语言中,变量往往用来保存"状态"(state)。不修改变量,意味着状态不能保存在变量中。函数式编程使用参数保存状态,最好的例子就是递归。下面的代码是一个将字符串逆序排列的函数,它演示了不同的参数如何决定了运算所处的"状态"。
  function reverse(string) {
    if(string.length == 0) {
      return string;
    } else {
      return reverse(string.substring(1, string.length)) + string.substring(0, 1);
    }
  }
由于使用了递归,函数式语言的运行速度比较慢,这是它长期不能在业界推广的主要原因。
5. 引用透明
引用透明(Referential transparency),指的是函数的运行不依赖于外部变量或"状态",只依赖于输入的参数,任何时候只要参数相同,引用函数所得到的返回值总是相同的。
1. 代码简洁,开发快速
2. 接近自然语言,易于理解

4. 易于"并发编程"
函数式编程不需要考虑"死锁"(deadlock),因为它不修改变量,所以根本不存在"锁"线程的问题。不必担心一个线程的数据,被另一个线程修改,所以可以很放心地把工作分摊到多个线程,部署"并发编程"(concurrency)。
请看下面的代码:
  var s1 = Op1();
  var s2 = Op2();
  var s3 = concat(s1, s2);
由于s1和s2互不干扰,不会修改变量,谁先执行是无所谓的,所以可以放心地增加线程,把它们分配在两个线程上完成。其他类型的语言就做不到这一点,因为s1可能会修改系统状态,而s2可能会用到这些状态,所以必须保证s2在s1之后运行,自然也就不能部署到其他线程上了。
5. 代码的热升级

函数式编程没有副作用,只要保证接口不变,内部实现是外部无关的。所以,可以在运行状态下直接升级代码,不需要重启,也不需要停机。Erlang语言早就证明了这一点,它是瑞典爱立信公司为了管理电话系统而开发的,电话系统的升级当然是不能停机的。
http://nxlhero.blog.51cto.com/962631/1661813
Java 8增加了lambda表达式的支持,连C++都已经支持lambda表达式和闭包。 有越来越多的用函数式编程语言写的开源软件在被广泛使用,最有名的,用Scala写的SparkErlang写的RabbitMQ,当然还有用Lisp写的Emacs。函数式编程语言已经出现四十多年了,为什么现在才火起来,是因为函数式编程语言天然的适合多核编程,或者分布式编程。GoogleMapReduce框架就是受到函数式编程的启发而出现的,其实GoogleMapReduce和函数式编程的map reduce本质上是一样的。
a = [1,2,3,4,5]
sum = reduce(lambda x,y:x+y, a,0)
有一个累计值acc0reduceacc=acc+y应用到每个a的元素上,最后就得到了a的各个元素的和。

GoogleMapReduce就是受函数式编程语言的mapreduce启发而产生的(reduce在有的语言中叫fold,就是把列表折叠一下)
Word count:
val words = List("fuck","bitch","fuck")
println("fuck:" + words.map( x=>if(x=="fuck") 1 else 0).reduce((x,y)=>x+y))


Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts