{"id":6305,"date":"2012-04-16T22:38:21","date_gmt":"2012-04-16T14:38:21","guid":{"rendered":"http:\/\/ihower.tw\/blog\/?p=6305"},"modified":"2012-04-17T10:08:29","modified_gmt":"2012-04-17T02:08:29","slug":"functional-programming-for-java-developers-%e8%ae%80%e6%9b%b8%e6%91%98%e8%a6%81","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/6305-functional-programming-for-java-developers-%e8%ae%80%e6%9b%b8%e6%91%98%e8%a6%81","title":{"rendered":"Functional Programming for Java Developers \u8b80\u66f8\u6458\u8981"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/akamaicovers.oreilly.com\/images\/0636920021667\/cat.gif\" alt=\"\" \/><\/p>\n<p>\u9019\u662f\u6211\u4e4b\u524d\u5ff5 <a href=\"http:\/\/shop.oreilly.com\/product\/0636920021667.do\">Functional Programming for Java Developers<\/a> \u4e00\u66f8\u7684\u6458\u8981\u8a18\u9304\u3002\u9019\u672c\u66f8\u5f88\u8584\u53ea\u670990\u9801\uff0c\u662f\u4e00\u672c\u883b\u4e0d\u932f\u7684 Functional Programming \u6982\u5ff5\u5165\u9580\u52f8\u6557\u66f8\u3002<\/p>\n<p>\u8fd1\u4f86 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Functional_programming\">Functional Programming<\/a> (\u51fd\u6578\u5f0f\u7de8\u7a0b\uff0c\u4ee5\u4e0b\u7c21\u7a31FP) \u7684\u91cd\u8981\u6027\u63d0\u6607\u5c31\u662f\u70ba\u4e86\u56e0\u61c9 Concurrency \u7684\u9700\u6c42\u3002CPU \u671d\u5411\u591a\u6838\u67b6\u69cb\u767c\u5c55\uff0cOOP\u7684\u7a0b\u5f0f\u958b\u767c\u65b9\u5f0f\u7269\u4ef6\u5145\u6eff\u53ef\u8b8a\u72c0\u614b\uff0c\u9020\u6210\u64b0\u5beb Concurrency \u7a0b\u5f0f\u6642\u9677\u9631\u591a\u591a\u3002<\/p>\n<p>\u7576\u7136\uff0c\u8981\u5bebFP\u4e0d\u4ee3\u8868\u4e00\u5b9a\u8981\u7528FP\u8a9e\u8a00\uff0c\u9084\u662f\u53ef\u4ee5\u7528\u73fe\u6210\u7684OO\u8a9e\u8a00\u53bb\u5beb\u3002\u4f46\u5c31\u50cf\u7528\u6c92\u6709OO\u652f\u63f4\u7684C\u53bb\u5bebOO\u98a8\u683c\uff0c\u7528\u5c08\u9580\u7684FP\u8a9e\u8a00\u9084\u662f\u6bd4\u8f03\u65b9\u4fbf\u3002<\/p>\n<h2>\u70ba\u4f55\u8981FP?<\/h2>\n<p>\u4f5c\u8005\u8aaa\u96d6\u7136\u662f\u56e0\u70ba Concurrency \u800c\u5b78 FP\uff0c\u4f46\u662f\u5f8c\u4f86\u537b\u5f88\u4eab\u53d7\u9019\u7a2e Paradigm Shift (\u5178\u7bc4\u8f49\u79fb)\u3002OO \u88ab\u767c\u660e\u56e0\u70ba GUI\uff0c\u5f8c\u4f86\u4eba\u5011\u767c\u73fe\u53ef\u4ee5\u7528\u4f86\u61c9\u7528\u5728\u5404\u7a2e\u9818\u57df\u4e0a\u3002OO \u548c FP \u90fd\u662f\u5de5\u5177\uff0c\u5404\u6709\u512a\u7f3a\u9ede\uff0c\u4f46\u662f\u73fe\u5728\u4eba\u5011\u78b0\u5230\u6240\u6709\u554f\u984c\u90fd\u7528 OO \u53bb\u89e3\uff0c\u5c31\u50cf<a href=\"http:\/\/en.wikipedia.org\/wiki\/Law_of_the_instrument\">\u4f60\u624b\u4e0a\u6709\u8457\u6425\u5b50\uff0c\u5728\u4f60\u773c\u4e2d\u4ec0\u9ebc\u90fd\u770b\u8d77\u4f86\u50cf\u91d8\u5b50<\/a>\u3002<\/p>\n<p>FP \u4e0d\u4ee3\u8868\u6bd4 OO \u512a\u8d8a\uff0c\u7562\u7adf OO \u7684\u597d\u8655\u5df2\u7d93\u88ab\u8b49\u5be6\u4e14\u5ee3\u6cdb\u61c9\u7528\u3002\u800c\u662f\u76ee\u524d\u6642\u4ee3\u4e0d\u540c\u4e86\uff0cOO \u7684\u7f3a\u9ede\u5728\u67d0\u4e9b\u9818\u57df\u5df2\u7d93\u5230\u4e86\u4e0d\u53ef\u5ffd\u8996\u7684\u5730\u6b65\uff0c\u6709\u4e9b\u6311\u6230\u6027\u7684\u554f\u984c\u7528 FP \u89e3\u66f4\u70ba\u9069\u5408\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><!--more--><\/p>\n<p>1. Concurrent<\/p>\n<p>\u4ee5\u5f80\u6211\u5011\u7e3d\u662f\u8b93\u6700\u8070\u660e\u7684\u4eba\u53bb\u89e3 Concurrent \u554f\u984c\uff0c\u5c0f\u5fc3\u5730\u6ce8\u610f Synchronized access to shared\u3002\u56e0\u6b64\u7d55\u5927\u90e8\u5206\u7684\u958b\u767c\u8005\u4e0d\u9700\u8981\u7169\u60f1\u3002\u4f46\u662f\u4eca\u65e5CPU\u591a\u6838\uff0cConcurrent \u9700\u6c42\u5927\u589e\uff0cFP \u53ef\u4ee5\u7d66\u4f60\u6b63\u78ba\u65b9\u5f0f\u548c\u66f4\u9ad8\u968e\u7684 Concurrency \u62bd\u8c61\u6a5f\u5236\u4f86\u8b93\u9019\u4ef6\u4e8b\u66f4\u5bb9\u6613\u3002<\/p>\n<p>2. Big data (\u5927\u90e8\u5206\u7684\u7a0b\u5f0f\u53ea\u662f\u8cc7\u6599\u8655\u7406\u554f\u984c)<\/p>\n<p>\u7576\u4f60\u9700\u8981\u8655\u7406 terabytes \u7b49\u7d1a\u7684\u8cc7\u6599\u6642\uff0c\u4f60\u7d55\u5c0d\u627f\u53d7\u4e0d\u4e86 Object \u7684 overhead\uff0c\u4f60\u9700\u8981\u66f4\u6709\u6548\u7387\u6709\u6700\u5c11 overhead \u7684\u8cc7\u6599\u7d50\u69cb\u8ddf\u6f14\u7b97\u6cd5\u3002ORM \u5728\u9019\u554f\u984c\u4e0a\u662f\u7121\u7528\u7684\uff0c\u4efb\u4f55\u8f49\u63db Relational data \u7684\u62bd\u8c61\u6a5f\u5236\u662f\u7121\u7528\u7684\u3002FP \u53ef\u4ee5\u7ed9\u4f60\u6700\u5c11\u7684 overhead \u4f86\u64cd\u4f5c\u9019\u4e9b\u539f\u59cb\u8cc7\u6599\uff0c\u53c8\u53ef\u4ee5\u505a\u5230 DRY \u8ddf Reuse \u6027\u3002\u7528 FP \u76f4\u63a5\u8655\u7406\u539f\u59cb\u8cc7\u6599\uff0c\u4f60\u4e0d\u9700\u8981 OO \u7684 overhead\u3002<\/p>\n<p>3. Modular<\/p>\n<p>OO \u7576\u521d\u7684\u9858\u666f\u5305\u62ec Reusable components\uff0c\u53ef\u4ee5\u76f4\u63a5\u653e\u5230\u4f60\u7684 app \u4e2d\u3002\u4f46\u662f\u6210\u529f\u7684 Library, Framework \u6848\u4f8b\u90fd\u662f\u4f60\u5fc5\u9808 follow \u4ed6\u5011\u7684\u898f\u5247\u4f86\u8d70\uff0c\u5f88\u591a code \u9084\u662f\u5fc5\u9808\u91cd\u5beb\u3002OO \u4e0d\u7b97\u662f\u975e\u5e38\u6210\u529f\u5728 &ldquo;Component assembly&rdquo;<br \/>\nOO \u7121\u9650\u5236\u7684\u5f48\u6027\u7834\u58de\u4e86 Reuse\uff0c\u56e0\u70ba\u5982\u4f55\u8ddf\u4e00\u500b\u7269\u4ef6\u4e92\u52d5\u7684\u65b9\u5f0f\u592a\u591a\u4e86\u3002\u4e00\u500b\u7cfb\u7d71\u6709\u597d\u7684\u9650\u5236\u53cd\u800c\u6bd4\u8f03 Modular\uff0c\u5c31\u50cf PC \u7684\u6210\u529f\u5728\u65bc IBM \u8a2d\u8a08\u51fa PC \u67b6\u69cb\u3002Web \u7684\u6210\u529f\u5728\u65bc HTTP \u7684\u7c21\u55ae\u5354\u5b9a\u3002<\/p>\n<p>FP \u7528\u6a19\u6e96\u7684 List, map, set \u4f86\u7d44\u7e54\u8cc7\u6599\uff0cFP \u7684 functions \u907f\u514d side effect\u3002\u53bb\u9664 dependencies \u8b93 function \u53ef\u4ee5\u5728\u4e0d\u540c contexts \u90fd\u53ef\u4ee5\u76f4\u63a5 resue\u3002<\/p>\n<p>4. Work Faster and Faster<\/p>\n<p>\u4eca\u65e5\u5c0d\u65bc\u53ef\u4ee5\u5feb\u901f deliver \u6bd4\u7cbe\u78ba\u5730\u6a21\u578b domain \u9084\u91cd\u8981\uff0c\u56e0\u70ba\u6211\u5011\u66f4\u770b\u91cd\u5feb\u901f\u4fee\u6b63\u7684\u80fd\u529b\uff0c\u4e00\u5929\u53ef\u4ee5 deployment \u597d\u5e7e\u6b21\u3002OO \u5f37\u8abf\u7684 object model \u80fd\u529b\u4f3c\u4e4e\u53ef\u4ee5\u518d\u4e09\u8003\u616e\u4e86\uff0cFP \u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u6709\u5f48\u6027\u8b8a\u5316\u7684\u80fd\u529b\u3002<\/p>\n<p>5. Simplicity<\/p>\n<p>\u5f88\u591aOO\u7684\u8907\u96dc\u6027\u548cmiddleware\u90fd\u662f\u4e0d\u5fc5\u8981\u7684\u3002FP \u8fd4\u6a38\u6b78\u771f\u66f4\u70ba\u7c21\u6f54\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662fFP?<\/h2>\n<p>\u4e0d\u662f\u6709 function \u7684\u7a0b\u5f0f\u8a9e\u8a00\u5c31\u53eb\u505a functional programming language \u5537?<\/p>\n<p>\u6700\u65e9\u7684 FP \u662f Lisp\uff0c\u4e5f\u662f\u76ee\u524d\u7b2c\u4e8c\u8001\u7684\u9ad8\u968e\u7a0b\u5f0f\u8a9e\u8a00\u4e86(Fortran\u6700\u8001)\uff0cML family \u5305\u62ec Caml, OCaml, F#\u3002\u6700 purity \u5247\u662f Haskell\uff0c\u8fd1\u671f\u6709 Clojure, Scala \u5728 JVM \u4e0a\u3002<\/p>\n<p>\u57fa\u672c\u539f\u5247\uff1a<\/p>\n<p>1. \u907f\u514d Mutable State<\/p>\n<p>Mutable value \u8b93 multithreaded programming \u8b8a\u56f0\u96e3\u3002\u5982\u679c\u53ef\u4ee5 immutable\uff0c\u90a3\u5c31\u4e0d\u9700\u8981 synchronization \u4e86\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u7a0b\u5f0f\u4e5f\u66f4 correctness \u6b63\u78ba\uff0c\u7279\u5225\u662f\u5728\u4e00\u500b\u5927\u578b\u7cfb\u7d71\u4e2d\u4e00\u500b\u975e locally \u7684 mutations\uff0c\u8981\u627e bugs \u6642\u7279\u5225\u8f9b\u82e6\u3002<\/p>\n<p>Java \u6709\u63d0\u4f9b final\uff0c\u4f46\u662f\u9019\u4fdd\u96aa\u537b\u4e0d\u5920\u3002\u56e0\u70ba final \u7684 object \u9084\u662f\u53ef\u4ee5\u4fee\u6539!! \u4f8b\u5982\u5bb9\u5668\u4e2d\u7684\u5143\u7d20\u3002\u6c92\u6709 mutable value \u4e4b\u5f8c\uff0cFP \u63d0\u4f9b\u4e86\u5176\u4ed6\u6709\u6548\u7387\u7684\u65b9\u5f0f\u4f86\u64cd\u4f5c\u5bb9\u5668\u3002<\/p>\n<p>\u4e0d\u904e\uff0c\u9084\u662f\u6709\u90e8\u5206\u7684 mutability \u662f\u7121\u6cd5\u907f\u514d\u7684\uff0c\u4f8b\u5982IO\u3002\u4f46\u662f FP \u9f13\u52f5\u6211\u5011\u601d\u8003 Mutable \u7684\u5fc5\u8981\uff0c\u5c07 Mutable \u7684\u90e8\u4efd\u5305\u88dd\u8d77\u4f86\uff0c\u7a0b\u5f0f\u5176\u4ed6\u90e8\u5206\u5c31\u662f Immutable \u5b89\u5168\u7684\u3002\u9019\u4e9b\u9700\u8981 Mutable \u7684\u90e8\u4efd\u53ef\u4ee5\u7528 STM \u6216 Actor \u4f86\u89e3\u6c7a Concurrency \u554f\u984c\u3002<\/p>\n<p>2. Function \u662f\u4e00\u7d1a\u8cc7\u6599, Lambdas \u548c Closures, Higher-Order Functions<\/p>\n<p>First-class value \u8868\u793a\u53ef\u4ee5\u7576\u6210\u8b8a\u6578(\u6216\u53c3\u6578)\u76f4\u63a5\u50b3\u905e\uff0c\u65b9\u6cd5\u4e5f\u4e0d\u80fd\u56de\u50b3\u4e00\u500b function\uff0c\u5728 Java \u4e2d\u751a\u81f3\u9023 class \u90fd\u4e0d\u662f first-class\u3002\u5728 Ruby \u4e2d function \u548c class \u90fd\u662f\u3002<\/p>\n<p>(method \u548c function \u7684\u8a9e\u610f\u6709\u4e00\u9ede\u5dee\u5225\uff0c\u524d\u8005\u591a\u534a\u6307\u7269\u4ef6\u4e2d\u7684\u65b9\u6cd5\uff0c\u5f8c\u8005\u5247\u6bd4\u8f03\u5ee3\u7fa9\uff0c\u4e0d\u4e00\u5b9a\u7d81\u5728\u7269\u4ef6\u6216\u985e\u5225\u4e0a\u3002)<\/p>\n<p>Callback method \u7684\u60c5\u666f\u662f\u6700\u5e38\u9700\u8981\u50b3\u905e function \u7684\u5730\u65b9\uff0cJava \u7528 anonymous inner class \u4f86\u89e3\u6c7a\u9019\u500b\u554f\u984c\u3002\u4e0d\u662f\u4e0d\u884c\uff0c\u53ea\u662f\u4e0d\u540c library \u7684\u985e\u5225(\u6216\u4ecb\u9762)\u548c\u8981\u8986\u5beb\u7684 method \u547d\u540d\u90fd\u4e0d\u540c\uff0c\u6bcf\u6b21\u90fd\u8981\u67e5\u3002\u5982\u679c\u7a0b\u5f0f\u8a9e\u8a00\u652f\u63f4\u7d71\u4e00\u7528 function wrapper \u5c31\u7c21\u6f54\u591a\u4e86\u3002\u9019\u7a2e anonymous function \u53c8\u53eb\u4f5c lambda\u3002closure \u6307\u7684\u662f\u53ef\u4ee5\u5728 function \u6307\u6d89\u5230\u5916\u9762\u7684\u8b8a\u6578\u3002\u5728 Java \u6709\u9650\u5ea6\u652f\u63f4\uff0cinner class \u4e2d\u53ea\u53ef\u4ee5\u7528\u5916\u9762\u88ab final \u7684\u8b8a\u6578\u3002<\/p>\n<p>Function \u53ef\u4ee5\u56de\u50b3 Function \u7684\u80fd\u529b\u53eb\u505a Higher-Order Functions\uff0c\u5728 Java \u4e2d\u53ea\u80fd\u7528 function wrapper \u4f86\u505a\u4e86\u3002<\/p>\n<p>3. Side-Effect-Free Function \u6c92\u6709\u526f\u4f5c\u7528<\/p>\n<p>\u4e0d\u50cfOO\uff0cFP\u7684 function \u4e0d\u8ad6\u5728\u4ec0\u9ebc context\uff0c\u57f7\u884c\u7d50\u679c\u90fd\u76f8\u540c\u3002\u53ea\u8981\u53c3\u6578\u5217\u76f8\u540c\uff0c\u4e0d\u8ad6\u4ec0\u9ebc\u60c5\u6cc1\u7d50\u679c\u90fd\u76f8\u540c(\u53eb\u505a referential transparency)\u3002<\/p>\n<p>4. Recursion \u905e\u8ff4<\/p>\n<p>\u56e0\u70ba\u8981\u907f\u514d State (loop counter \u5c31\u662f mutable \u8b8a\u6578)\uff0c\u6240\u4ee5\u7528\u905e\u8ff4\u8655\u7406\u8ff4\u5708\u3002\u4e0d\u904e\uff0c\u6df1\u5ea6\u905e\u8ff4\u6703\u9020\u6210 stack \u904e\u6df1\u7684\u6548\u80fd\u554f\u984c\uff0c\u56e0\u6b64 FP \u8a9e\u8a00\u591a\u6703\u652f\u63f4 tail-call recursion \u7684\u80fd\u529b\u80fd\u5c07\u904b\u7b97\u81ea\u52d5\u8f49\u6210\u8ff4\u5708\u3002\u53ef\u60dc\u7684\u662f Java \u6c92\u6709\u9019\u500b\u80fd\u529b\u3002<\/p>\n<p>5. Lazy Evaluation<\/p>\n<p>\u8981\u8868\u793a\u7121\u7aae\u6578\u5217\uff0c\u4e0d\u53ef\u80fd\u5168\u7b97\u51fa\u4f86\uff0clazy evaluation \u53ef\u4ee5\u5728\u8981\u7684\u6642\u5019\u518d\u8a08\u7b97\u3002lazy evaluation \u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u9700\u8981\u6642\u624d\u57f7\u884c\u6602\u8cb4\u7684\u64cd\u4f5c\u3002\u5b8c\u5168\u7684 lazy evaluation \u9700\u8981 referential transparency \u624d\u8fa6\u7684\u5230\uff0c\u4e5f\u5c31\u662f\u9700\u8981 side-effect-free function \u548c immutable value\u3002(\u53ea\u6709\u6700 Pure Functional \u7684 Haskell \u9810\u8a2d\u6240\u6709 expressions \u90fd\u662f lazy)<\/p>\n<p>6. Declarative \u98a8\u683c\uff0c\u800c\u4e0d\u662f Imperative<\/li>\n<p>OO \u57fa\u672c\u4e0a\u4e5f\u662f Imperative \u98a8\u683c\uff0c\u4e00\u884c\u884c\u544a\u8a34\u96fb\u8166\u7279\u5b9a\u7684\u6b65\u9a5f\u3002<\/p>\n<pre><code># Declarative\r\ndef factorial\r\n  if (n==1) return 1\r\n  else return n * factorial(n-1)\r\nend\r\n\r\n# Imperative \u6709\u8a31\u591a mutation step\r\ndef factorial\r\n  result = 1\r\n  for (i = 2; i&lt;=n; i++) {\r\n    result *= i \r\n  }\r\n  return result\r\nend\r\n<\/code><\/pre>\n<p>Declarative \u98a8\u683c\u4e5f\u8ddf lazy evaluation \u5f88\u5408\u3002\u8ddf mutability \u548c side-effect function \u5247\u4e0d\u76f8\u5bb9\u3002<\/p>\n<p>\u7121\u8ad6\u504f\u597d static \u6216 dynamic typing, FP \u5c0d\u65bc type design \u4e5f\u6709\u4e00\u5957\u770b\u6cd5\uff0c\u9664\u4e86\u4e0b\u4e00\u7ae0\u63d0\u5230\u7684\u6838\u5fc3\u5bb9\u5668 type\uff0c\u9084\u6709\u4e00\u4ef6\u4e8b\u503c\u5f97\u5b78\u7fd2\uff1a<\/p>\n<p>immutable \u8868\u793a\u8b8a\u6578\u521d\u59cb\u4e00\u5b9a\u8981\u6709\u503c\uff0c\u4e5f\u5c31\u8868\u793a\u4e0d\u61c9\u8a72\u5141\u8a31 null\uff0cnull \u4e5f\u5e38\u5e38\u662f bug \u6e90\u982d\uff0c\u4f8b\u5982\u5fd8\u8a18\u53bb check null \u7684\u5b58\u5728\u3002\u5728 Java \u4e2d Null type \u5c31\u662f<a href=\"http:\/\/stackoverflow.com\/questions\/3495711\/why-null-was-called-a-subtype-of-every-reference-type\">\u4efb\u4f55 type \u7684 subtype<\/a>\u3002\u6703\u9700\u8981 null \u7684\u5b58\u5728\uff0c\u986f\u7136\u662f\u56e0\u70ba\u6211\u5011\u9700\u8981\u4e00\u500b\u8b8a\u6578\u4f86\u8868\u793a &ldquo;Optionally&rdquo; \u53ef\u6709\u53ef\u7121\uff0c\u90a3\u9ebc\u4f55\u4e0d\u660e\u986f\u5efa\u7acb\u4e00\u500b type \u8655\u7406\uff0c\u4f8b\u5982\u4e00\u500b\u62bd\u8c61\u4ecb\u9762 Option \u4ee5\u53ca\u5b83\u7684\u5be6\u9ad4\u5316  subtype (final) Some \u548c (final) None \u8868\u793a\u4e00\u500b\u6709\u4e00\u500b\u7121\u3002\u56e0\u6b64\u5728\u9700\u8981 null \u7684\u5834\u5408\uff0c\u4f7f\u7528 Some \u548c None \u4f86\u5305\u88dd\u3002Java \u7684 type-safe \u6703\u4fdd\u8b49\u4f60\u4e0d\u6703\u5fd8\u8a18\u4e00\u5b9a\u8981\u8655\u7406 Option\uff0c\u770b\u662f Some \u6216 None\uff0c\u9019\u7a2e\u4f5c\u6cd5\u4fdd\u8b49\u4e86\u7a0b\u5f0f\u7684\u53ef\u9760\u6027\u3002<\/p>\n<p>\u50cf Option \u4ecb\u9762\u53ea\u5141\u8a31 Some \u548c None \u9019\u5169\u500b final \u4e0d\u80fd\u518d subtype \u7684 type\uff0c\u53eb\u505a Algebraic data type\uff0c\u53ef\u4ee5\u5f9e\u4e00\u7a2e type \u5b89\u5168\u8b8a\u63db\u6210\u53e6\u4e00\u7a2e type (\u4e0b\u4e00\u7ae0\u6703\u6709\u4f8b\u5b50)\u3002\u8ddf\u4e00\u822c\u6211\u5011\u8a2d\u8a08\u4ecb\u9762\u7684 abstract data type \u4e0d\u9650\u5236 subtype\uff0c\u5f37\u8abf polymorphic behavior \u7684\u7528\u6cd5\u6982\u5ff5\u4e0d\u540c\u3002<\/p>\n<h2>\u8cc7\u6599\u7d50\u69cb\u548c\u6f14\u7b97\u6cd5<\/h2>\n<p>FP \u504f\u597d\u4f7f\u7528\u6838\u5fc3\u63d0\u4f9b\u7684\u5bb9\u5668 Lists\u3001Maps\u3001Trees\u3001Sets\uff0c\u4e0d\u50cf OO \u611b\u7528\u7269\u4ef6\u5305\u88f9\u3002\u6839\u64da\u4e0a\u4e00\u7ae0\u7684FP\u539f\u5247\uff0c\u4f86\u5be6\u969b\u770b\u4e00\u4e9b\u8cc7\u6599\u7d50\u69cb\u548c\u6f14\u7b97\u6cd5\u3002FP\u63d0\u4f9b\u4e86\u5e38\u898b\u7684\u8cc7\u6599\u7d50\u69cb\u548c\u5c0d\u61c9\u7684 Combinator \u64cd\u4f5c\u3002<\/p>\n<p>Linked list \u4e5f\u662f\u4e00\u7a2e Algebraic Data Type\uff0c\u53ea\u6709\u5169\u7a2e subtype: empty \u548c non-empty\u3002\u9019\u8ddf Java \u7684 List type \u4e0d\u4e00\u6a23\uff0cMap \u4e5f\u662f abstract data type\u3002\u4f5c\u8005\u7528 Java \u5be6\u4f5c\u4e86 functional-style \u7684 List, Map<\/p>\n<p>Combinator functions: \u8655\u7406\u5bb9\u5668\u7684\u57fa\u672c\u4e09\u62db: 1. filter 2. map 3.fold \u5f88\u591a\u5176\u4ed6\u64cd\u4f5c\u90fd\u662f\u57fa\u65bc\u6b64\u3002\u9019\u4e09\u62db\u53c8\u53eb\u4f5c Combinators\uff0c\u662f\u6700\u53b2\u5bb3\u7684 reusable \u5efa\u69cb\u6f14\u7b97\u6cd5\u53ef\u4ee5\u7d44\u5408\u51fa\u8907\u96dc\u7684\u904b\u7b97\u3002\u9019\u4e09\u62db\u4e5f\u8b93\u4f60\u4e0d\u5fc5\u4e00\u76f4\u7528\u905e\u8ff4\u3002<\/p>\n<p>\u56e0\u70ba\u662f immutable\uff0c\u6240\u4ee5\u8b8a\u6578\u9700\u8981\u6539\u8b8a\u6642\uff0c\u5c31\u8981\u4e0d\u65b7\u7684 Copy \u51fa\u65b0\u503c\u624d\u884c\u3002\u5982\u679c\u78b0\u5230\u5927\u8cc7\u6599\uff0c\u5c0d\u6548\u80fd\u5c31\u6709\u554f\u984c\u4e86\u3002\u597d\u5728 FP \u5167\u90e8\u5be6\u4f5c\u5229\u7528\u4e86 Structure sharing \u7684\u65b9\u5f0f\uff0c\u7528 tree \u7d50\u69cb\u907f\u514d full-copy \u4f86\u6709\u6548\u7387\u7684\u8655\u7406\u3002\u9019\u7a2e\u8cc7\u6599\u7d50\u69cb\u53eb\u505a Persistent Data Structure\u3002<\/p>\n<p>\u5229\u7528 DSL (DSL\u53ef\u4ee5\u7528OO\u4e5f\u53ef\u4ee5\u7528FP\u5be6\u4f5c) \u4f86\u5305\u88dd FP \u64cd\u4f5c\uff0c\u53ea\u4f7f\u7528\u6838\u5fc3\u8cc7\u6599\u7d50\u69cb\u548c Combinators\u3002\u4e0d\u9700\u8981\u6bcf\u6a23\u6771\u897f\u90fd\u7269\u4ef6\u5316\uff0cOO \u64cd\u4f5c\u662f\u932f\u8aa4\u7684\u62bd\u8c61\u5316\u5c64\u7d1a\u3002<\/p>\n<h2>Function Concurrency<\/h2>\n<p>\u5f88\u559c\u6b61\u4f5c\u8005\u7684\u9019\u53e5\u8a71 &ldquo;Multithreaded programming, requiring synchronized access to shared, mutable state, is the assembly language of concurrency&#8221;\uff0c\u6bcf\u6b21\u770b OO \u7a0b\u5f0f\u8a9e\u8a00\u7684 threading \u7ae0\u7bc0\u90fd\u89ba\u5f97 multithreaded \u662f\u795e\u4eba\u624d\u80fd\u5beb\u7684\u6771\u897f\uff0c\u5be6\u5728\u592a\u96e3\u4e86\u3002<\/p>\n<p>\u96d6\u7136 immutable \u7279\u6027\u5df2\u7d93\u8b93\u5f88\u591a synchronization \u4e0d\u9700\u8981\u4e86\u3002\u4f46\u662f mutate state \u9084\u662f\u6709\u4e0d\u53ef\u907f\u514d\u7684\u6642\u5019\uff0c\u9019\u6642\u5019\u53ef\u4ee5\u5229\u7528\u66f4\u9ad8\u62bd\u8c61\u5c64\u7d1a\u7684 Actors \u548c STM \u4f86\u78ba\u4fdd thread-safe\u3002<\/p>\n<p>Actors \u900f\u904e Actor \u4f86\u505a\u8a0a\u606f\u50b3\u905e\uff0c\u6bcf\u500b Actor \u6709\u81ea\u5df1\u7684 queue\u3002\u5be6\u4f5c\u6700\u597d\u7684\u5927\u6982\u662f Erlang \u4e86\u3002<br \/>\n\u6709\u8da3\u7684\u662f\uff0c\u6700\u65e9\u7684 Smalltalk \u60f3\u6cd5\u9084\u6bd4\u8f03\u50cf Actor Model\uff0c\u95dc\u9375\u662f messaging\u3002<\/p>\n<p>1.\u53ea\u6709\u4e00\u500b actor \u8ca0\u8cac\u6539\u8b8a\u72c0\u614b\uff0c\u6240\u4ee5\u5176\u4ed6 code \u60f3\u6539\u8b8a\u6642\uff0c\u90fd\u5fc5\u9808\u901a\u77e5\u552f\u4e00\u7684 actor\uff0c\u5f9e\u800c\u907f\u514d synchronization \u554f\u984c\u3002<br \/>\n2. \u5141\u8a31\u591a\u500b actors \u4fee\u6539\uff0c\u6703\u6709\u4e00\u500b\u7279\u5225\u7684 semaphore message \u4ee3\u8868\u5b89\u5168\u3002<\/p>\n<p>Actors \u98a8\u96aa\u662f\u5982\u679c scope \u592a\u5927\uff0c\u6703\u9020\u6210 bottleneck\u3002<\/p>\n<p>Java \u4e0a\u76ee\u524d\u6709\u5169\u500b\u597d\u7684\u5be6\u4f5c\uff1a<a href=\"http:\/\/akka.io\">Akka<\/a> \u548c <a href=\"http:\/\/functionaljava.org\/\">Functional Java<\/a>\u3002Actors \u6a21\u578b\u6709\u8a31\u591a\u5730\u65b9\u90fd\u53d7 Erlang \u6210\u529f\u7684\u555f\u767c\uff0c\u5305\u62ec Akka \u7528\u4e86 Bridge design pattern \u4f86\u589e\u52a0 robustness \u548c error recovery \u80fd\u529b\u3002<\/p>\n<p>STM \u63d0\u4f9b\u8a18\u61b6\u9ad4\u5c64\u7d1a\u7684 ACI (\u8a18\u61b6\u9ad4\u6240\u4ee5\u505a\u4e0d\u5230 During\uff0c\u6240\u4ee5\u4e0d\u662fACID)\u3002STM \u80cc\u5f8c\u7684\u539f\u7406\u662f value \u672c\u8eab\u9084\u662f immutable \u7684\uff0c\u5982\u679c\u6709\u6539\u8b8a\u503c\uff0c\u5247\u900f\u904e\u6539\u8b8a references\uff0c\u52a0\u4e0a Persistent Data Structures \u6a5f\u5236\u589e\u52a0\u6548\u7387\u3002STM \u76ee\u524d\u505a\u6700\u597d\u7684\u8a9e\u8a00\u5927\u6982\u662f Clojure\u3002Akka \u4e5f\u6709 STM \u7684\u5be6\u4f5c\u3002<\/p>\n<p>\u95dc\u65bc Actor \u548c STM \u7684\u4f7f\u7528\u6642\u6a5f\u6bd4\u55bb\uff0cRubyConf 2011 \u7684\u9019\u5834 <a href=\"http:\/\/confreaks.net\/videos\/702-rubyconf2011-scaling-ruby-with-actors-or-how-i-learned-to-stop-worrying-and-love-threads\">Scaling Ruby with Actors, or How I Learned to Stop Worrying and Love Threads<\/a> \u6f14\u8b1b\u6211\u89ba\u5f97\u9084\u4e0d\u932f\u3002<\/p>\n<h2>\u66f4\u597d\u7684OO<\/h2>\n<p>OO \u7de8\u7a0b\u57fa\u672c\u4e0a\u662f Imperative\uff0c\u800c FP \u662f declarative\u3002Imperative \u770b\u8d77\u4f86\u5f88\u5fd9\u53c8\u5f88\u591a mutation\uff0c\u5bb9\u6613\u51fa\u932f\u3002<br \/>\nmutable \u7269\u4ef6\u4e0d thread-safe \u800c\u4e14\u4e0d\u6613\u638c\u63a7\u4fee\u6539\u3002\u8b93\u7269\u4ef6 immutable\uff0c\u76e1\u91cf declarative\u3002\u96d6\u7136\u6709\u9650\u5236\uff0c\u4f46\u662f\u4fdd\u6301\u6240\u6709 public \u62bd\u8c61\u5c64\u7684 Pure\uff0c\u5373\u4f7f\u5167\u5728\u4e0d Pure\u3002<\/p>\n<p>\u4ee5 LSV \u70ba\u4f8b\uff0c\u5728 OO \u4e2d\u56e0\u70ba\u7e7c\u627f\u7684\u81ea\u7531\u8ddf\u5f48\u6027\uff0c\u5f88\u96e3\u4fdd\u8b49\u7b26\u5408\uff0c\u65bc\u662f\u900f\u904e\u6e2c\u8a66\u6216\u8a2d\u8a08\u6a21\u5f0f\u4f86\u505a\u3002\u4f8b\u5982 Template patterns\uff0c\u4f46\u662f FP \u7684 higher-order functions \u5c31\u53ef\u4ee5\u505a\u5230\u4e86\uff0c\u7d30\u7bc0\u5728 function argument \u518d\u5b9a\u7fa9\u5373\u53ef\u3002<\/p>\n<p>\u6709\u4e9b\u4eba\u89ba\u5f97 FP \u662f\u4e0d\u662f\u8b93 OO \u4e16\u754c\u7684\u8a2d\u8a08\u6a21\u5f0f\u7121\u7528\uff0c\u5176\u5be6\u9019\u662f\u641e\u6df7\u4e86\u6a21\u5f0f\u7684\u7cbe\u795e\u8ddf\u5be6\u4f5c\u3002\u67d0\u4e9b GOF \u7684 pattern \u5176\u5be6\u6839\u672c\u5c31\u662f FP \u5167\u5efa\u7684\u529f\u80fd(Singleton, Composite, Command, Iterator\u7b49)\uff0c\u6709\u4e9b\u5247\u53ef\u4ee5\u88ab\u53d6\u4ee3(Template Method &ndash;> higher-order functions)\u3002<\/p>\n<p>FP \u4e5f\u6709\u81ea\u5df1\u7684\u6a21\u5f0f\uff0c\u4f8b\u5982 Fold \u7528\u6cd5\u548c Pattern matching\u3002Monad \u88ab\u7528\u5728 sequence expressions\u3002Vistor pattern \u7684\u7528\u9014(go inside the object)\u5247\u88ab Pattern matching \u53d6\u4ee3\u3002<\/p>\n<p>Pattern matching \u883b\u50cf switch \u7684\u52a0\u5f37\u7248\uff0c\u662f\u4e00\u7a2e\u597d\u7528\u7684 modularity \u5de5\u5177\uff0c\u53ef\u4ee5\u6839\u64da type \u505a data extraction\uff0c\u4f7f\u7528 Pattern matching \u4f86\u5be6\u4f5c\u65b0\u529f\u80fd\u800c\u4e0d\u6703\u6c61\u67d3\u672c\u4f86\u7684 type<\/p>\n<p>\u4ec0\u9ebc\u662f\u597d type \u8a2d\u8a08? OO modeling \u6c92\u932f\uff0c\u4f46\u662f\u4e0d\u7cbe\u6e96\u7684 OO code \u6642\u537b\u4e0d\u7121\u6cd5\u4fdd\u8b49 LSV \u7684 type \u6b63\u78ba\u6027\u3002\u9019\u5c31\u662f OO programming \u7684\u554f\u984c\uff0cdomain concept \u90fd\u5728\u8b8a\uff0c\u4e0d\u5982\u5316\u4f5c key-value pairs\u3002\u7576\u7136\u4e5f\u6709\u597d\u7684 domain concept \u662f\u4e0d\u6703\u8b8a\uff0c\u4f8b\u5982\u9322\uff0czip codes \u7b49\u7b49<\/p>\n<p>\u4f5c\u8005\u8a8d\u70ba\u4efb\u4f55\u653e\u5728 collection \u88e1\u7684\u90fd\u4e0d\u61c9\u8a72\u6709\u5c08\u5c6c\u7684 type\uff0c\u8b93 filter, map, fold \u4e3b\u5c0e\u3002type wrapper \u4e0d\u503c\u5f97\u82b1\u8cbb\u958b\u767c\u3002<\/p>\n<p>ORM \u8ddf\u5176\u4ed6 OO middleware \u90fd\u662f\u7121\u8b02\u7684\u8907\u96dc\uff0c\u7528 filter, map, fold \u8f49\u63db\u8cc7\u6599\u5f62\u5f0f\u5373\u53ef\u3002Domain object \u96d6\u7136\u5bb9\u6613\u4e86\u89e3\uff0c\u4f46\u662f\u597d\u8655\u537b\u4e0d\u7e3d\u662f\u503c\u5f97\u3002\u8d8a\u5c11 code \u5c31\u8d8a Agile (Play framework \u7684 Scala Anorm API \u662f\u500b\u597d\u4f8b\u5b50)\u3002<\/p>\n<h2>\u9858\u666f<\/h2>\n<p>\u5f9e Groovy, JRuby, Jython \u7b49 Scripting \u8a9e\u8a00\u958b\u59cb\u5b78FP\u662f\u6700\u7c21\u55ae\u7684\u4e00\u6b65\uff0c\u96d6\u7136\u4e0d\u662fFP\u8a9e\u8a00\uff0c\u4f46\u662f\u6709\u8a31\u591aFP\u7684\u529f\u80fd\u3002FP \u4e2d\uff0cScala \u662f\u4f5c\u8005\u7684\u6700\u611b\uff0c\u4f60\u53ef\u4ee5\u540c\u6642\u6709OO\uff0c\u7136\u5f8c\u6162\u6162\u5b78FP\u3002\u7576\u7136\uff0c\u907f\u514d\u4e00\u76f4\u5f85\u5728 OO \u8212\u9069\u5708\u3002Clojure \u5373\u4f7f\u4f60\u4e0d\u611b Lisp \u4f46\u4e5f\u503c\u5f97\u4e00\u5b78\uff0cLisp \u53ef\u662f\u6b77\u4e45\u4e0d\u8870\u7684\u8a9e\u8a00\u3002<\/p>\n<p>\u5982\u679c\u8003\u616eJVM\u4ee5\u5916\uff0cHaskell\u53ef\u4ee5\u4e00\u8a66\uff0c\u9019\u662f\u6240\u6709FP\u8a9e\u8a00\u7684\u6416\u7c43\u3002Windows \u4f7f\u7528\u8005\u5247\u53ef\u4ee5\u8003\u616eF#\u3002The Structure and Interpretation of Computer Programs \u662f\u7d93\u5178\u6559\u79d1\u66f8\uff0c\u4f5c\u8005\u9084\u63a8\u85a6\u8b80 <a href=\"http:\/\/www.ibm.com\/developerworks\/java\/library\/j-ft1\/\">Neal Ford&rsquo;s Functional Thinking<\/a> \u548c <a href=\"http:\/\/www.cs.utexas.edu\/~shmat\/courses\/cs345\/whyfp.pdf\">Why Functional Programming Matters<\/a>\u3002<\/p>\n<p>Java \u7684 Functional \u5de5\u5177\u6709 <a href=\"http:\/\/functionaljava.org\/\">Functional Java<\/a>\u3001<a href=\"http:\/\/code.google.com\/p\/totallylazy\/\">Totally Lazy<\/a> \u548c <a href=\"http:\/\/akka.io\">Akka<\/a>\u3002\u4f5c\u8005\u671f\u671b Akka \u6703\u662f\u63a5\u4e0b\u4f86\u5e7e\u5e74 Java \u754c\u7684\u660e\u65e5\u4e4b\u661f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9019\u662f\u6211\u4e4b\u524d\u5ff5 Functional Programming for Java Developers \u4e00\u66f8\u7684\u6458\u8981 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/6305-functional-programming-for-java-developers-%e8%ae%80%e6%9b%b8%e6%91%98%e8%a6%81\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Functional Programming for Java Developers \u8b80\u66f8\u6458\u8981\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[64,63],"tags":[],"class_list":["post-6305","post","type-post","status-publish","format-standard","hentry","category-functional","category-java","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-1DH","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/6305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/comments?post=6305"}],"version-history":[{"count":67,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/6305\/revisions"}],"predecessor-version":[{"id":7602,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/6305\/revisions\/7602"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=6305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=6305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=6305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}