{"id":27005,"date":"2013-05-02T02:55:26","date_gmt":"2013-05-01T18:55:26","guid":{"rendered":"http:\/\/www.yankay.com\/?p=27005"},"modified":"2021-03-28T20:06:20","modified_gmt":"2021-03-28T12:06:20","slug":"scala-tour-choiceness","status":"publish","type":"post","link":"http:\/\/yankay.com\/scala-tour-choiceness\/","title":{"rendered":"Scala Tour – \u7cbe\u9009"},"content":{"rendered":"
5\u67081\u65e5\u662f\u52b3\u52a8\u7684\u65e5\u5b50\uff0c\u7b14\u8005\u505a\u4e86\u4e00\u4e2a\u5b66\u4e60Scala\u7cbe\u5f69\u7279\u6027\u7684\u7f51\u7ad9Scala-Tour<\/a>\u3002\u5728\u5b66\u4e60Scala\u662f\u65f6\u5019\uff0c\u9047\u5230\u5f88\u591a\u4ee4\u4eba\u6fc0\u52a8\u7684\u7279\u6027\uff0c\u4e3b\u8981\u51fd\u6570\u5f0f\u7f16\u7a0b\u548c\u5e76\u53d1\u3002\u76f8\u6bd4\u4e0bJava\u5df2\u7ecf\u8001\u6001\u9f99\u949f\uff0c\u6b65\u5c65\u8e87\u8dda\u3002\u6216\u8bb8Scala\u4e0d\u4f1a\u6210\u4e3a\u66ff\u4ee3Java\u8bed\u8a00\uff0c\u4f46\u7684\u786e\u7ed9\u540e\u6765\u8005\u8bbe\u7acb\u4e86\u6807\u6746\u3002\u6240\u4ee5\u505a\u4e86\u8fd9\u4e2a\u7f51\u7ad9\uff0c\u987a\u7740\u4e00\u4e2a\u4e00\u4e2a\u4f8b\u5b50\uff0c\u7531\u6d45\u5165\u6df1\uff0c\u7531\u8868\u53ca\u91cc\u3002\u9010\u6e10\u5b66\u4f1aScala,\u5c3d\u7ba1\u4e0d\u4f1a\u56e0\u6b64\u6210\u4e3a\u4e00\u4e2a\u719f\u7ec3Scala\u7684\u5f00\u53d1\u8005\uff0c\u4f46\u662f\u5bf9\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u4e5f\u4f1a\u76f8\u5f53\u4e86\u7136\u3002\u8fd9\u7bc7\u6587\u7ae0\u7cbe\u9009\u4e86Scala-Tour\u4e0a\u4e86\u4e00\u4e9b\u7ae0\u8282\uff0c\u60f3\u5feb\u901f\u4e86\u89e3\u7684\u670b\u53cb\u53ef\u4ee5\u770b\u770b\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5f53\u7136\u60f3\u8be6\u7ec6\u770b\u5c31\u4e0a\u4e0aScala-Tour<\/a>\u5427\u3002 \u5728Java\u91cc\u9762\uff0c\u4f7f\u7528\u5b8c\u8d44\u6e90(\u6587\u4ef6\u53e5\u67c4\uff0c\u6570\u636e\u5e93\u8fde\u63a5)\u7b49\u4e4b\u540e\uff0c\u5fc5\u987b\u624b\u52a8Close\u3002\u5426\u5219\u53d1\u751f\u6cc4\u6f0f\u540e\uff0c\u7a0b\u5e8f\u53ea\u6709\u88ab\u8feb\u91cd\u542f\u3002Scala\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u5f0f\u5b9e\u73b0\u81ea\u52a8close\u3002<\/p>\n \u8fd9\u4e2a\u4f8b\u5b50\u662f\u4ece\/proc\/self\/stat\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684pid\u3002withScanner\u5c01\u88c5\u4e86try-finally\u5757\uff0c\u6240\u4ee5\u8c03\u7528\u8005\u4e0d\u7528\u518dclose\u3002<\/p>\n \u6211\u4eec\u719f\u6089\u7684\u53c2\u6570\u4f20\u9012\u65b9\u5f0f\u662f\u6309\u503c\u4f20\u9012\u3002\u6309\u540d\u79f0\u4f20\u9012\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u76f4\u63a5\u4f20\u9012\u53c2\u6570\u540d\u5b57\uff0c\u7b49\u5230\u5b9e\u9645\u8c03\u7528\u7684\u65f6\u5019\uff0c\u518d\u53bb\u53d6\u503c\u3002\u5728Java\u4ee3\u7801\u4e2d\uff0c\u5f80\u5f80\u5145\u65a5\u7740if(log.isDebug()){log.debug(…)}\u8fd9\u6837\u8bed\u53e5\u3002\u4e4b\u524d\u7684if\u8c03\u7528\u662f\u5f88\u6709\u5fc5\u8981\u7684\uff0c\u56e0\u4e3a\u5728\u4e4b\u540e\u7684debug\u8bed\u53e5\u4e2d\u5f80\u5f80\u6709\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u64cd\u4f5c\u3002\u5728\u4e0d\u9700\u8981\u6253Log\u7684\u65f6\u5019\uff0c\u5b57\u7b26\u4e32\u62fc\u63a5\u4e5f\u6709\u53ef\u80fd\u53d1\u751f\u5f02\u5e38\u629b\u51fa\u3002\u800cScala\u53ef\u4ee5\u901a\u8fc7\u6309\u540d\u79f0\u4f20\u9012\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u8fd9\u6837\u5c31\u4e0d\u518d\u9700\u8981if(log.isDebug())\u8fd9\u6837\u7684\u8bed\u53e5\u4e86\u3002<\/p>\n \u201c\u8d70\u8d77\u6765\u50cf\u9e2d\u5b50\uff0c\u53eb\u8d77\u6765\u50cf\u9e2d\u5b50\uff0c\u5c31\u662f\u9e2d\u5b50\u3002\u201d\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u4f7f\u7528{ def close(): Unit }\u4f5c\u4e3a\u53c2\u6570\u7c7b\u578b\u3002\u56e0\u6b64\u4efb\u4f55\u542b\u6709close()\u7684\u51fd\u6570\u7684\u7c7b\u90fd\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570\u3002\u8fd9\u6837\u7684\u505a\u6cd5\u6bd4\u4f7f\u7528\u63a5\u53e3\u8981\u597d\u5f88\u591a\uff0c\u56e0\u4e3a\u53ef\u4ee5\u4e0d\u5f15\u5165\u4efb\u4f55\u4f9d\u8d56\u3002\u8fd9\u4e2awithClose\u65b9\u6cd5\u5355\u72ec\u7f16\u8bd1\uff0c\u968f\u5904\u4f7f\u7528\u3002<\/p>\n Traits\u5c31\u50cf\u662f\u6709\u51fd\u6570\u4f53\u7684Interface\uff0c\u4f7f\u7528with\u5173\u952e\u5b57\u6765\u6df7\u5165\u3002\u5355\u4e2aTraits\u5c31\u50cf\u662f\u4e00\u5757\u4e50\u9ad8\u79ef\u6728\uff0c\u4e00\u4e2a\u63d2\u4ef6\u3002\u5c31\u50cf\u4e0b\u9762\u7684JsonAble\uff0c\u5f53\u4f7f\u7528\u4e00\u4e2a\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u968f\u65f6\u968f\u5730\u628a\u5b83\u63d2\u5728\u4ed6\u4e0a\u9762\u3002\u8fd9\u4e2a\u5bf9\u63a5\u5c31\u5177\u5907\u4e86toJson\u7684\u80fd\u529b\u3002\u4e0d\u7528\u521b\u5efa\u4e00\u4e2a\u7c7b\uff0c\u6216\u8005\u5199\u7ec4\u5408\u7684\u4ee3\u7801\uff0c\u975e\u5e38\u5e72\u8106\u3002\u8fd9\u6837\u4e5f\u53ef\u4ee5\u4f7f\u4ee3\u7801\u6709\u5f88\u9ad8\u7684\u6b63\u4ea4\u6027\u3002\u4e0d\u518d\u4f1a\u4e3a\u4e86\u4e00\u4e2a\u5f88\u5c0f\u7684\u9700\u6c42\uff0c\u53bb\u4fee\u6539\u4e00\u4e2a\u88ab\u5e7f\u6cdb\u4f7f\u7528\u7684\u7c7b\u3002<\/p>\n \u901a\u8fc7\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u7a0b\u5e8f\u6781\u4e3a\u7b80\u6d01\u3002 \u51fd\u6570\u5f0f\u9664\u4e86\u80fd\u7b80\u5316\u4ee3\u7801\u5916\uff0c\u66f4\u91cd\u8981\u7684\u662f\u4ed6\u5173\u6ce8\u7684\u662fInput\u548cOutput\uff0c\u51fd\u6570\u672c\u8eab\u6ca1\u6709\u526f\u4f5c\u7528\u3002 \u5c31\u662fUnix pipeline\u4e00\u6837\uff0c\u7b80\u5355\u7684\u547d\u4ee4\u53ef\u4ee5\u7ec4\u5408\u5728\u4e00\u8d77\u3002 List\u7684filter\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u8fc7\u6ee4\u51fd\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684List \u5982\u679c\u4f60\u559c\u6b22Unix pipeline\u7684\u65b9\u5f0f\uff0c\u4f60\u4e00\u5b9a\u4e5f\u4f1a\u559c\u6b22\u51fd\u6570\u5f0f\u7f16\u7a0b\u3002 \u8fd9\u4e2a\u4f8b\u5b50\u662f\u7528\u51fd\u6570\u5f0f\u7684\u4ee3\u7801\u6a21\u62df\u201ccat file | grep ‘warn’ | grep ‘2013’ | wc\u201d\u7684\u884c\u4e3a\u3002\u76f8\u6bd4\u4e8eRuby\u7b49\u52a8\u6001\u8bed\u8a00,\u8fd9\u5a01\u529b\u6765\u81ea\u4e8e\u79d1\u5b66\u800c\u4e0d\u662f\u9b54\u6cd5<\/p>\n \u6bcf\u4e2aJava\u7a0b\u5e8f\u5458\u90fd\u88abNullException\u6298\u78e8\u8fc7\u3002\u56e0\u4e3aJava\u4e2d\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u53ef\u80fd\u4e3aNull,\u6240\u4ee5\u8981\u4e48\u5230\u5904\u68c0\u67e5null\u7684\u95ee\u9898\uff0c\u8981\u4e48\u5230\u5904try\/cache\u3002 \u8fd9\u4e2a\u4f8b\u5b50\u662f\u8bbf\u95ee\u82e5\u5e72URL\u3002\u4f46\u786e\u53ef\u4ee5\u5e76\u884c\u8bbf\u95ee\uff0c\u6bd4\u975e\u5e76\u884c\u7684\u505a\u6cd5\u53ef\u4ee5\u5feb\u4e00\u500d\u3002\u8981\u60f3\u8ba9\u8bbf\u95ee\u5e76\u884c\uff0c\u53ea\u8981\u8c03\u7528List.par\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n \u662f\u4e0d\u662f\u975e\u5e38\u7684\u7b80\u5355\uff1f\u5e76\u884c\u96c6\u5408\u652f\u6301\u5927\u90e8\u5206\u96c6\u5408\u7684\u529f\u80fd\u3002\u4e0d\u589e\u52a0\u7a0b\u5e8f\u590d\u6742\u6027\uff0c\u5374\u80fd\u5927\u5e45\u63d0\u9ad8\u5e76\u53d1\u7684\u80fd\u529b\u3002<\/p>\n Actor\u662f\u5e76\u53d1\u6a21\u578b\uff0c\u4e5f\u4f7f\u7528\u4e8e\u5206\u5e03\u5f0f\u3002\u8fd9\u4e2a\u4f8b\u5b50\u521b\u5efa\u4e00\u4e2a\u65f6\u95f4\u670d\u52a1\u5668\uff0c\u901a\u8fc7alive\u6765\u76d1\u542cTCP\u7aef\u53e3\uff0cregister\u6765\u6ce8\u518c\u81ea\u5df1\u3002\u8c03\u7528\u65f6\u901a\u8fc7select\u521b\u5efaclient\u3002\u5176\u4f59\u4f7f\u7528\u65b9\u5f0f\u548c\u666e\u901aActor\u4e00\u6837\u3002 \u62bd\u53d6\u5668\u53ef\u4ee5\u8fdb\u884c\u89e3\u6784\u3002\u8fd9\u4e2a\u4f8b\u5b50\u662f\u6784\u5efa\u4e00\u4e2aEmail\u62bd\u53d6\u5668\uff0c\u53ea\u8981\u5b9e\u73b0unapply\u51fd\u6570\u5c31\u53ef\u4ee5\u4e86\u3002 DSL\u662fScala\u6700\u5f3a\u5927\u6b66\u5668\uff0c\u53ef\u4ee5\u4f7f\u4e00\u4e9b\u63cf\u8ff0\u6027\u4ee3\u7801\u53d8\u5f97\u6781\u4e3a\u7b80\u5355\u3002\u8fd9\u4e2a\u4f8b\u5b50\u662f\u4f7f\u7528DSL\u751f\u6210JSON\u3002Scala\u5f88\u591a\u770b\u4f3c\u662f\u8bed\u8a00\u7ea7\u7684\u7279\u6027\u4e5f\u662f\u7528DSL\u505a\u5230\u7684\u3002 SBT\u662fScala\u7684\u6700\u4f73\u7f16\u8bd1\u5de5\u5177\uff0c\u5728\u4ed6\u7684\u5e2e\u52a9\u4e0b\uff0c\u4f60\u751a\u81f3\u4e0d\u9700\u8981\u5b89\u88c5\u9664JRE\u5916\u7684\u4efb\u4f55\u4e1c\u897f\uff0c\u6765\u5f00\u53d1Scala\u3002 \u8fd9\u51e0\u4e2a\u4f8b\u5b50\u7cbe\u9009\u81eaScala-Tour<\/a>\uff0c\u8fd9\u4e2a\u7f51\u7ad9\u4e2d\u8fd8\u6709\u4ecb\u7ecd\u4e86\u5f88\u591a\u5176\u4ed6\u597d\u7684\u7279\u6027\uff0c\u6bd4\u5982\u6a21\u5f0f\u5339\u914d\u548c\u9690\u5f0f\u8f6c\u6362\uff0c\u5c31\u4e0d\u9010\u4e00\u4ecb\u7ecd\u4e86\u3002\u8fd9\u4e2a\u9879\u76eeHost\u5728GitHub<\/a>\u4e0a\uff0c\u5982\u679c\u4f60\u4e5f\u6709\u7cbe\u5f69\u7684\u7528\u6cd5\u7684\u8bdd\uff0c\u5927\u5bb6\u4ea4\u6d41\u4ea4\u6d41\u5427\u3002<\/p>\n","protected":false},"excerpt":{"rendered":" 5\u67081\u65e5\u662f\u52b3\u52a8\u7684\u65e5\u5b50\uff0c\u7b14\u8005\u505a\u4e86\u4e00\u4e2a\u5b66\u4e60Scala\u7cbe\u5f69\u7279\u6027\u7684\u7f51\u7ad9Scala-Tour<\/a>\u3002<\/p>\n
\n<\/p>\n\u4e0d\u518d\u9700\u8981Close<\/h3>\n
import scala.reflect.io.File\nimport java.util.Scanner\n\ndef withScanner(f: File, op: Scanner => Unit) = {\n val scanner = new Scanner(f.bufferedReader)\n try {\n op(scanner)\n } finally {\n scanner.close()\n }\n}\n\nwithScanner(File(\"\/proc\/self\/stat\"),\n scanner => println(\"pid is \" + scanner.next()))<\/pre>\n
\u6309\u540d\u79f0\u4f20\u9012\u53c2\u6570<\/h3>\n
val logEnable = false\n\ndef log(msg: => String) =\n if (logEnable) println(msg)\n\nval MSG = \"programing is running\"\n\nlog(MSG + 1 \/ 0)<\/pre>\n
\u9e2d\u5b50\u7c7b\u578b<\/h3>\n
def withClose(closeAble: { def close(): Unit }, op: { def close(): Unit } => Unit) {\n try {\n op(closeAble)\n } finally {\n closeAble.close()\n }\n}\n\nclass Connection {\n def close() = println(\"close Connection\")\n}\n\nval conn: Connection = new Connection()\nwithClose(conn, conn =>\n println(\"do something with Connection\"))<\/pre>\n
Trait<\/h3>\n
trait ForEachAble[A] {\n def iterator: java.util.Iterator[A]\n def foreach(f: A => Unit) = {\n val iter = iterator\n while (iter.hasNext)\n f(iter.next)\n }\n}\n\ntrait JsonAble {\n def toJson() =\n scala.util.parsing.json.JSONFormat.defaultFormatter(this)\n}\n\nval list = new java.util.ArrayList[Int]() with ForEachAble[Int] \nlist.add(1); list.add(2)\n\nprintln(\"For each: \"); list.foreach(x => println(x))\n\/\/println(\"Json: \" + list.toJson())<\/pre>\n
\u51fd\u6570\u5f0f\u771f\u6b63\u7684\u5a01\u529b<\/h3>\n
val file = List(\"warn 2013 msg\", \"warn 2012 msg\", \"error 2013 msg\", \"warn 2013 msg\")\n\nprintln(\"cat file | grep 'warn' | grep '2013' | wc : \" \n + file.filter(_.contains(\"warn\")).filter(_.contains(\"2013\")).size)<\/pre>\n
\u518d\u89c1 NullException<\/h3>\n
\nScala\u63d0\u4f9b\u4e86Option\u673a\u5236\u6765\u89e3\u51b3\uff0c\u4ee3\u7801\u4e2d\u4e0d\u65ad\u68c0\u67e5null\u7684\u95ee\u9898\u3002\u8fd9\u4e2a\u4f8b\u5b50\u5305\u88c5\u4e86getProperty\u65b9\u6cd5\uff0c\u4f7f\u5176\u8fd4\u56de\u4e00\u4e2aOption\u3002 \u8fd9\u6837\u5c31\u53ef\u4ee5\u4e0d\u518d\u6f2b\u65e0\u76ee\u7684\u5730null\u68c0\u67e5\u3002\u53ea\u8981Option\u7c7b\u578b\u7684\u503c\u5373\u53ef\u3002\u4f7f\u7528pattern match\u6765\u68c0\u67e5\u662f\u5e38\u89c1\u505a\u6cd5\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528getOrElse\u6765\u63d0\u4f9b\u5f53\u4e3aNone\u65f6\u7684\u9ed8\u8ba4\u503c\u3002\u7ed9\u529b\u7684\u662fOption\u8fd8\u53ef\u4ee5\u770b\u4f5c\u662f\u6700\u5927\u957f\u5ea6\u4e3a1\u7684List\uff0cList\u7684\u5f3a\u5927\u529f\u80fd\u90fd\u53ef\u4ee5\u4f7f\u7528\u3002
\n\u4e0d\u662f\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u53ef\u4ee5\u4e3aNull\u4e86\uff0c\u53ea\u6709Option\u53ef\u4ee5\u4e3aNone\u3002\u8fd9\u6837\u7684\u505a\u6cd5\u663e\u793a\u533a\u5206\u4e86\u53ef\u80fd\u4e3aNull\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u548cNullException\u8bf4\u518d\u89c1\u4e86\u3002<\/p>\ndef getProperty(name: String): Option[String] = {\n val value = System.getProperty(name)\n if (value != null) Some(value) else None\n}\n\nval osName = getProperty(\"os.name\")\n\nosName match {\n case Some(value) => println(value)\n case _ => println(\"none\")\n}\n\nprintln(osName.getOrElse(\"none\"))\n\nosName.foreach(print _)<\/pre>\n
\u5e76\u884c\u96c6\u5408<\/h3>\n
val urls = List(\"http:\/\/scala-lang.org\",\n \"https:\/\/github.com\/yankay\/scala-tour\")\n\ndef fromURL(url: String) = scala.io.Source.fromURL(url)\n .getLines().mkString(\"\\n\")\n\nval t = System.currentTimeMillis()\nurls.par.map(fromURL(_))\nprintln(\"time: \" + (System.currentTimeMillis - t) + \"ms\")<\/pre>\n
\u8fdc\u7a0bActor<\/h3>\n
\n\u5c06\u5355\u673a\u5e76\u53d1\u548c\u5206\u5e03\u5f0f\u62bd\u8c61\u6210\u4e00\u79cd\u6a21\u578b\u3002\u7b80\u5316\u4e86\u7a0b\u5e8f\u590d\u6742\u6027\u3002\u867d\u7136\u591a\u6838\u7f16\u7a0b\u5e76\u4e0d\u5e7f\u6cdb\uff0c\u4f46\u8c03\u7528\u5916\u90e8\u63a5\u53e3\u7684\u60c5\u51b5\u8d8a\u6765\u8d8a\u591a\u3002Actor\u6a21\u578b\u975e\u5e38\u9002\u7528\u4e8e\u8fd9\u6837\u7684\u5f02\u6b65\u73af\u5883\u3002<\/p>\nimport scala.actors.remote.RemoteActor._\nimport scala.actors.Actor._\nimport scala.actors.remote.Node\n\nval port = 31241\n\nval echoServer = actor {\n alive(port)\n register('echoServer, self)\n loop {\n react {\n case msg => {\n reply(\"replay \" + msg)\n }\n }\n }\n}\n\nval timeServerClient = select(Node(\"127.0.0.1\", port), 'echoServer)\n\ntimeServerClient !? \"hi\" match {\n case replay: String => println(replay)\n}<\/pre>\n
\u62bd\u53d6\u5668<\/h3>\n
\nScala\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4f1a\u81ea\u5e26\u62bd\u53d6\u5668\uff0c\u53ef\u4ee5\u62bd\u53d6\u51fa\u4e00\u4e2aList\u3002List\u91cc\u7684\u5143\u7d20\u662f\u5339\u914d()\u91cc\u7684\u8868\u8fbe\u5f0f\u3002
\n\u62bd\u53d6\u5668\u5f88\u6709\u7528\uff0c\u77ed\u77ed\u7684\u4f8b\u5b50\u91cc\u5c31\u6709\u4e24\u5904\u4f7f\u7528\u62bd\u53d6\u5668\uff1a<\/p>\n\n
import scala.util.matching.Regex\n\nobject Email {\n def unapply(str: String) = new Regex(\"\"\"(.*)@(.*)\"\"\")\n .unapplySeq(str).get match {\n case user :: domain :: Nil => Some(user, domain)\n case _ => None\n }\n}\n\n\"user@domain.com\" match {\n case Email(user, domain) => println(user + \"@\" + domain)\n}<\/pre>\n
DSL<\/h3>\n
\n\u81ea\u5df1\u7f16\u5199DSL\u6709\u70b9\u590d\u6742\uff0c\u4f46\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u65b9\u4fbf\u3002\u8fd9\u6837\u53ef\u4ee5\u4f7fScala\u53ef\u4ee5\u5d4c\u5165XML\uff0c\u5d4c\u5165Json\uff0c\u5d4c\u5165SQL\u3002\u800c\u5176\u4ed6\u8bed\u8a00\u4e2d\u8fd9\u4e9b\u90fd\u53ea\u662f\u5b57\u7b26\u4e32\u800c\u5df2\u3002<\/p>\nimport org.json4s._\nimport org.json4s.JsonDSL._\nimport org.json4s.jackson.JsonMethods._\nimport java.util.Date\n\ncase class Twitter(id: Long, text: String, publishedAt: Option[java.util.Date])\n\nvar twitters = Twitter(1, \"hello scala\", Some(new Date())) ::\n Twitter(2, \"I like scala tour\", None) :: Nil\n\nvar json = (\"twitters\"\n -> twitters.map(\n t => (\"id\" -> t.id)\n ~ (\"text\" -> t.text)\n ~ (\"published_at\" -> t.publishedAt.toString())))\n\nprintln(pretty(render(json)))<\/pre>\n
Simple Build Tool<\/h3>\n
\n\u4f8b\u5982\u4f60\u60f3\u5728\u81ea\u5df1\u7684\u673a\u5668\u4e0a\u6267\u884cScala-Tour<\/a>,\u53ef\u4ee5\u6267\u884c\u4e0b\u9762\u7684\u547d\u4ee4<\/p>\n#Linux\/Mac(compile & run):\ngit clone https:\/\/github.com\/yankay\/scala-tour-zh.git\ncd scala-tour-zh\n.\/sbt\/sbt stage\n.\/target\/start\n\n#Windows(can only compile):\ngit clone https:\/\/github.com\/yankay\/scala-tour-zh.git\ncd scala-tour-zh\nsbt\\sbt stage<\/pre>\n
\u7ed3\u8bed<\/h3>\n