{"rsdb":{"rid":"179812","subhead":"","postdate":"0","aid":"130437","fid":"90","uid":"1","topic":"1","content":"
\n

  \u5728\u524d\u9762\u7684\u51e0\u7bc7\u8ba8\u8bba\u91cc\u6211\u4eec\u521d\u6b65\u5bf9FP\u6709\u4e86\u4e9b\u5c11\u4e86\u89e3\uff1aFP\u561b\uff0c\u4e0d\u5c31\u662fF[A]\u5417\uff1f\u4e5f\u662f\uff0cFP\u5c31\u662f\u5728F[]\u58f3\u5b50\uff08context\uff09\u5185\u5bf9\u7a0b\u5e8f\u7684\u72b6\u6001\u8fdb\u884c\u66f4\u6539\uff0c\u4e5f\u5c31\u662f\u5728F\u58f3\u5b50\uff08context\uff09\u5185\u65bd\u7528\u4e00\u4e9b\u51fd\u6570\u3002\u518d\u76f4\u767d\u4e00\u70b9\u5c31\u662f\u5728F\u58f3\u5b50\u5185\u8fdb\u884cOOP\u60ef\u7528\u7684\u884c\u4ee4\u7f16\u7a0b\uff08imperative programming\uff09\u3002\u5f53\u7136\uff0c\u65e2\u7136\u662f\u5728\u58f3\u5b50\uff08context\uff09\u5185\u8fdb\u884c\u7f16\u7a0b\u8fd9\u79cd\u65b0\u7684\u6a21\u5f0f\uff0c\u90a3\u4e48\u603b\u9700\u8981\u4e9b\u65b0\u7684\u51fd\u6570\u65bd\u7528\u65b9\u6cd5\u5427\u3002\u6211\u4eec\u518d\u6b21\u5ba1\u89c6\u4e00\u4e0b\u4ee5\u524d\u4e86\u89e3\u8fc7\u7684FP\u51fd\u6570\u65bd\u7528\u65b9\u6cd5\uff1a<\/span><\/p> \n

\n
1<\/span> 1<\/span> \/\/<\/span> Functor : map[A,B] (F[A])(f: A => B): F[B]<\/span>\r\n2<\/span> 2<\/span> \/\/<\/span> Applicative: ap[A,B] (F[A])(f: F[A => B]): F[B] <\/span>\r\n3<\/span> 3<\/span> \/\/<\/span> Monad : flatMap[A,B](F[A])(f: A => F[B]): F[B]<\/span><\/span><\/pre> \n <\/div> \n 

\u5b83\u4eec\u5206\u522b\u4ee3\u8868\u4e86scalaz\u7684\u4e09\u4e2atypeclass\u3002\u5bf9\u4e8eFP\u7f16\u7a0b\u6765\u8bb2\uff0c\u51fd\u6570\u65bd\u7528\uff08function application\uff09\u5c31\u662f\u6539\u53d8\u7a0b\u5e8f\u72b6\u6001\uff0c\u4e5f\u5c31\u662fmap\u3002\u90a3\u4e48\u4ecemap\u89d2\u5ea6\u5206\u6790\uff0c\u5982\u679c\u76f4\u63a5\u5bf9F[A=>B], A=>F[B]\u8fdb\u884cmap\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\u7c7b\u578b\uff0c\u5982\u76f4\u63a5map A=>F[B]\u7ed3\u679c\u662fF[F[B]]\u3002\u6240\u4ee5\u6211\u4eec\u4f1a\u60f3\u529e\u6cd5\u628a\u7ed3\u679c\u7c7b\u578b\u5bf9\u9f50\u4e86\uff0c\u4f7f\u6700\u7ec8\u7ed3\u679c\u7c7b\u578b\u4e3aF[B]\uff1a<\/span><\/p> \n

\n
1<\/span> def ap[A,B](ma: F[A])(mf: F[A => B]): F[B] = mf.flatMap(f => ma.flatMap(a => point(f(a))) <\/span>2<\/span>  def flatMapByJoin[A,B](ma: M[A])(f: A => M[B]): M[B] = join(map(ma)(a => f(a))) <\/span>3<\/span>  def join[A](mma: M[M[A]]): M[A]<\/span><\/pre> \n <\/div> \n 

\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u6211\u4eec\u770b\u5230\uff1a\u5728flatMap\u6211\u4eec\u901a\u8fc7join\u65bd\u7528\u4e86map\u3002\u800c\u8fd9\u4e2ajoin\u597d\u50cf\u5c31\u662f\u4e3a\u4e86\u628aF[F[B]]\u6253\u5e73\u5230F[B]\u800c\u8bbe\u8ba1\u7684\uff0c\u8fd9\u70b9\u4ecejoin\u51fd\u6570\u6b3e\u5f0f\uff08signature\uff09\u53ef\u4ee5\u770b\u51fa\u3002\u96be\u9053FP\u5c31\u662f\u4e3a\u4e86\u5b9e\u73b0\u7c7b\u578b\u5339\u914d\u5417\uff1f\u7edd\u4e0d\u662f\uff01\u6211\u4eec\u4e0d\u80fd\u628a\u773c\u5149\u5c40\u9650\u5728\u5982\u4f55\u53d6\u5f97\u7c7b\u578b\u5339\u914d\u4e0a\uff0c\u800c\u662f\u5e94\u8be5\u653e\u9614\u5230\u51fd\u6570\u65bd\u7528\u7684\u76ee\u7684\u4e0a\u3002\u6211\u4eec\u4ece\u4e0a\u9762\u51fd\u6570map,ap,flatMap\u7684\u7c7b\u578b\u6b3e\u5f0f\u53ef\u4ee5\u770b\u51fa\uff1amap,ap\u90fd\u662f\u5728F[]\u58f3\uff08context\uff09\u5185\u65bd\u7528\u7684\uff0c\u800cflatMap\u662f\u5728\u58f3\u5916\u5bf9\u8f93\u5165\u7684\u7c7b\u578bA\u503c\u8fdb\u884c\u65bd\u7528\u7684\uff0c\u4f46\u628a\u7ed3\u679c\u653e\u5165\u4e86\u58f3\u5185\u3002\u8fd9\u53ef\u4ee5\u8bf4\u662fflatMap\u4e0emap,ap\u7684\u6839\u672c\u4e0d\u540c\u4e4b\u5904\u3002\u90a3\u4e48flatMap\u4ee3\u8868\u7740\u4ec0\u4e48\u5462\uff1f\u5982\u679c\u4eceflatMap\u7684\u51fd\u6570\u6b3e\u5f0f\uff08function signature\uff09\u5206\u6790\uff1a\u5b83\u662f\u4e00\u4e2a\u9012\u5f52\u7b97\u6cd5: \u7ed9F[A]\u4e00\u4e2aA\u4ea7\u751fF[B]\uff0c\u518d\u7ed9F[B]\u4e00\u4e2aB\u518d\u4ea7\u751fF[C]...\u5982\u6b64\u7c7b\u63a8\u3002\u8fd9\u6837\u770b\u6765flatMap\u662f\u4e00\u4e2a\u6301\u7eed\u7b97\u6cd5\uff08computational continuation\uff09,\u5982\u679c\u628aflatMap\u4e32\u8054\u8d77\u6765\u5c31\u53ef\u4ee5\u5b9e\u73b0\u67d0\u79cd\u7f16\u7a0b\u8bed\u6cd5\uff08syntax\uff09\u3002\u8fd9\u4e2a\u63a8\u8bba\u5728scala\u7684for-comprehension\u4e2d\u5f97\u5230\u8bc1\u5b9e\uff1aflatMap\u53ef\u4ee5\u88ab\u89c6\u4f5c\u4e00\u79cd\u7b80\u5355\u7684FP\u8bed\u6cd5\uff0c\u5b83\u4f7f\u6211\u4eec\u53ef\u4ee5\u5728for-comprehension\u4e2d\u4f7f\u7528\u6211\u4eec\u719f\u6089\u7684\u884c\u4ee4\u7f16\u7a0b\uff0c\u5176\u7ed3\u679c\u662fFP\u6a21\u5f0f\u7684\u884c\u4ee4\u7f16\u7a0b\u3002flatMap\u662fMonad\u7684\u6807\u8bc6\u51fd\u6570\uff0c\u800cMonad\u53c8\u5177\u5907\u6240\u6709\u7684FP\u51fd\u6570\u65bd\u7528\u65b9\u6cd5\u56e0\u4e3a\u5b83\u7ee7\u627f\u4e86Functor\u548cApplicative\uff0c\u6240\u4ee5\u6709\u4e9b\u4eba\u628aFP\u7f16\u7a0b\u79f0\u4e3aMonadic programming\u3002\u4ece\u8fd9\u91cc\u4e5f\u53ef\u4ee5\u770b\u5230flatMap\u5728FP\u7f16\u7a0b\u91cc\u7684\u91cd\u8981\u6027\u3002<\/span><\/p> \n

\u5982\u679c\u4eceflatMap\u4ee3\u8868\u6301\u7eed\u7b97\u6cd5\u8fd9\u4e2a\u89d2\u5ea6\u5206\u6790\uff1aflatMap\u5b9e\u9645\u8fde\u63a5\u4e86\u4e24\u4e2a\u7b97\u6cd5F[A] => F[B]\u3002\u6211\u4eec\u5e94\u8be5\u53ef\u4ee5\u5728\u8fd0\u7b97flatMap\u7684\u8fc7\u7a0b\u4e2d\u5b9e\u73b0\u4e00\u4e9b\u9644\u52a0\u7684\u6548\u679c\u3002\u8fd9\u4e2a\u8981\u6c42\u5e94\u8be5\u53ef\u4ee5\u5728\u5b9e\u73b0flatMap\u51fd\u6570\u65f6\u505a\u5230\u3002\u6211\u4eec\u8fd9\u7bc7\u8ba8\u8bba\u7684\u91cd\u70b9\u5c31\u662f\u5728\u793a\u8303\u5982\u4f55\u5728\u5b9e\u73b0flatMap\u65f6\u589e\u52a0\u4e00\u4e9b\u6548\u679c\u3002\u5f53\u628a\u4e00\u4e32\u7b97\u6cd5\u7528flatMap\u94fe\u63a5\u8d77\u6765\u65f6\u8fd9\u4e9b\u9644\u52a0\u6548\u679c\u662f\u5982\u4f55\u79ef\u7d2f\u7684\u3002<\/span><\/p> \n

\u6211\u60f3\u6ca1\u4ec0\u4e48\u6bd4logger\u66f4\u80fd\u793a\u8303\u4e32\u63a5\u7b97\u6cd5\u524d\u9762\u7b97\u6cd5\u7684\u4e00\u4e9b\u6548\u679c\u662f\u5982\u4f55\u6d41\u8f6c\u5230\u4e0b\u9762\u7684\u7b97\u6cd5\u91cc\u7684\u3002\u6211\u4eec\u6765\u8bbe\u8ba1\u4e00\u4e2a\u4f8b\u5b50\uff1a\u6a21\u62df\u4e00\u4e2a\u8f93\u5165\u88c5\u7f6e\uff0c\u6bcf\u63a5\u6536\u4e00\u6b21\u8f93\u5165\u4ee3\u8868\u4e00\u6b21\u8fd0\u7b97\uff0c\u7528\u4e00\u4e2alogger\u628a\u6bcf\u6b21\u8fd0\u7b97\u7684\u8f93\u5165\u90fd\u8bb0\u5f55\u4e0b\u6765\u3002\u5f53\u7136\uff0c\u8fd9\u4e2a\u4f8b\u5b50\u7528State Monad\u5c31\u5f88\u5bb9\u6613\u5b9e\u73b0\u3002\u4e0d\u8fc7\u6211\u4eec\u7684\u76ee\u7684\u662f\u53bb\u793a\u8303\u5982\u4f55\u901a\u8fc7flatMap\u628a\u6548\u679c\u4f20\u9012\u4e0b\u53bb\u7684\uff0c\u6240\u4ee5\u8fd8\u662f\u5e94\u8be5\u7d27\u8d34\u7740\u5982\u4f55\u5b9e\u73b0flatMap\uff1a<\/span><\/p> \n

\n
 1<\/span> trait KeyLog[K] { <\/span> 2<\/span>  def value: K <\/span> 3<\/span>  def log: String <\/span> 4<\/span>   override<\/span> def toString = "<\/span>[<\/span>"<\/span>+value+"<\/span>,<\/span>"<\/span>+log+"<\/span>]<\/span>"<\/span>\r\n 5<\/span> } <\/span> 6<\/span> object<\/span> KeyLog { <\/span> 7<\/span>     def apply[K](k: K, msg: String): KeyLog[K] = new<\/span> KeyLog[K] { <\/span> 8<\/span>         def value = k <\/span> 9<\/span>         def log = msg <\/span>10<\/span>  } <\/span>11<\/span> } <\/span>12<\/span> \r\n13<\/span> KeyLog(3<\/span>,"<\/span>Entered Number 3<\/span>"<\/span>)                      \/\/<\/span>> res0: Exercises.keylog.KeyLog[Int] = [3,Entered Number 3]<\/span>\r\n14<\/span> KeyLog("<\/span>Hello<\/span>"<\/span>, "<\/span>Entered String 'Hello'<\/span>"<\/span>)         \/\/<\/span>> res1: Exercises.keylog.KeyLog[String] = [Hello,Entered String 'Hello']<\/span><\/span><\/pre> \n <\/div> \n 

\u6211\u4eec\u7528KeyLog[K]\u6765\u4ee3\u8868\u8fd9\u4e2a\u8f93\u5165\u7b97\u6cd5\u3002\u6bcf\u4e2a\u7b97\u6cd5\u90fd\u5305\u542b\u4e00\u4e2aK\u7c7b\u578b\u7684value\u548cString\u7c7b\u578b\u7684log\u3002\u5bf9\u4e8e\u7c7b\u578b\u53c2\u6570K\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u7528\u666e\u901a\u7684flatMap K => KeyLog[I]\u6765\u8f6c\u53d8value\u3002\u800c\u6211\u4eec\u7684\u76ee\u7684\u662f\u5982\u4f55\u901a\u8fc7flatMap\u628a\u524d\u4e00\u4e2aKeyLog\u7684log\u7d2f\u79ef\u5230\u4e0b\u4e2a\u7b97\u6cd5\u7684log\u3002\u633a\u7b80\u5355\uff0c\u662f\u5427\uff1f\u5728KeyLog\u7ed3\u6784\u91cc\u8f6c\u53d8log\u5e76\u628a\u7ed3\u679c\u7559\u5728KeyLog\u91cc\uff0c\u542c\u7740\u50cf\u662fmap\uff0c\u4e0d\u8fc7map\u662f\u9488\u5bf9K\u7684\u3002\u6240\u4ee5\u6211\u4eec\u8981\u5148\u52a0\u4e2amapLog\uff1a<\/span><\/p> \n

\n
1<\/span> trait KeyLog[K] { <\/span>2<\/span>  def value: K <\/span>3<\/span>  def log: String <\/span>4<\/span>   override<\/span> def toString = "<\/span>[<\/span>"<\/span>+value+"<\/span>,<\/span>"<\/span>+log+"<\/span>]<\/span>"<\/span>\r\n5<\/span>   def mapLog(preLog: String): KeyLog[K] = KeyLog(value,preLog +"<\/span>;<\/span>"<\/span>+log) <\/span>6<\/span> }<\/span><\/pre> \n <\/div> \n 

\u6211\u4eec\u8bd5\u7740\u5b9e\u73b0flatMap\uff1a<\/span><\/p> \n

\n
1<\/span> trait KeyLog[K] { <\/span>2<\/span>  def value: K <\/span>3<\/span>  def log: String <\/span>4<\/span>   override<\/span> def toString = "<\/span>[<\/span>"<\/span>+value+"<\/span>,<\/span>"<\/span>+log+"<\/span>]<\/span>"<\/span>\r\n5<\/span>   def mapLog(preLog: String): KeyLog[K] = KeyLog(value,preLog +"<\/span>;<\/span>"<\/span>+log) <\/span>6<\/span>   def flatMap[I](f: K => KeyLog[I]): KeyLog[I] =\r\n7<\/span>  f(value).mapLog(log) <\/span>8<\/span> }<\/span><\/pre> \n <\/div> \n 

\u786e\u5b9e\u7b80\u5355\u53c8\u76f4\u63a5\uff1af(value) \u4ea7\u751f KeyLog[I] \u7136\u540e\u5728\u8fd9\u4e2a\u63a5\u7740\u7684\u7b97\u6cd5\u4e2d\u8c03\u7528 mapLog \u628a\u4e0a\u4e00\u4e2a\u7b97\u6cd5KeyLog[K]\u7684log\u5e76\u5165KeyLog[I]\u7684log\u3002<\/span><\/p> \n

\u6211\u4eec\u8bd5\u7740\u7528","orderid":"0","title":"Scalaz\uff0812\uff09\uff0d Monad\uff1a\u518d\u8ff0\u8ff0flatMap\uff0c\u987a\u4fbf\u4e86\u89e3MonadPlus(\u4e00)","smalltitle":"","mid":"0","fname":"Scala","special_id":"0","bak_id":"0","info":"0","hits":"2459","pages":"4","comments":"0","posttime":"2017-10-10 12:13:33","list":"1507608813","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Scalaz<\/A> Monad<\/A> flatMap<\/A> \u987a\u4fbf<\/A> \u4e86\u89e3<\/A> MonadPlus<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"113.108.110.181","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Scalaz\uff0812\uff09\uff0d Monad\uff1a\u518d\u8ff0\u8ff0flatMap\uff0c\u987a\u4fbf\u4e86\u89e3MonadPlus","lastview":"1714106419","digg_num":"3327","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}