Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

走马观花— Haskell Web 开发

5,214 views

Published on

走马观花—— Haskell Web 开发

Published in: Technology, Education
  • Be the first to comment

走马观花— Haskell Web 开发

  1. 1. 走马观花—— Haskell Web 开发 黄毅 http://www.yi-programmer.com/ yi.codeplayer@gmail.com
  2. 2. About me● CPP( 静 ) → Python( 动 ) → Haskell( 静 )● 专业 Python● 业余 Haskell
  3. 3. 前情提要 你将会了解到:● 一门 20 多年依然冷门的语言的简单介绍● 微线程模型并发处理的示例程序● 一种对“流式处理”的强大抽象● 一个常数内存占用的 HTTP 代理服务器的实现● 一种类型安全的“宏”,并用它创建 DSL 的例子
  4. 4. So ,Why Haskell ?
  5. 5. Questfor Certain Knowledge of Programming
  6. 6. 虚的部分• 纯,函数式(抽象!抽象!抽象!)• 命令式(显式的副作用管理)• 强大的静态类型系统(避免逻辑矛盾!)
  7. 7. 实在一点, 针对 Web 开发,Haskell 能提供什么?
  8. 8. 一、微线程和高并发
  9. 9. server <- listenOn (PortNumber 3000)forever $ do (client, h, p) <- accept server hSetBuffering client NoBuffering forkIO $ forever $ do request <- hGetLine client hPrintf client "%s from %s:%dn" request h (toInteger p)
  10. 10. 二、 Iteratee 针对流式数据处理的抽象http://www.haskell.org/haskellwiki/Enumerator_and_iteratee
  11. 11. 示例: A http proxy server runs in constant memoryhttp://github.com/yihuang/webproxy-yesod
  12. 12. 状态机
  13. 13. 状态机组合!requestLine :: Parser RequestrequestLine = do method <- P.takeWhile1 isToken <* char8 uri <- P.takeWhile1 (/=32) <* char8 version <- httpVersion <* endOfLine ...
  14. 14. 祭代码 状态机抽象data Stream a = EOF | Chunks [a]data Iteratee a b = Continue (Stream a → Step a b) | Yield b (Stream a) | Error Stringdata Enumerator a b = Iteratee a b → Iteratee a b
  15. 15. 祭代码 Proxy Applicationfetch :: Request IO -> (Status -> Headers -> Iteratee..) -> IO afetch req f = withManager $ m -> run_ $ http req f mgetIndexR = do req <- liftIO $ parseUrl url sendWaiResponse $ ResponseEnumerator $ fetch req
  16. 16. 简单 benchmark● 300M 数据文件, 100 并发,恒定 25M RES
  17. 17. DSLfor web framework
  18. 18. Template Haskell● Run haskell at compile time● Imagine type safe lisp macro
  19. 19. URL RoutemkYesod "Simple" [parseRoutes|/ HomeR GET/static StaticR GET/article ArticleR GET POST/article/#Int ArticleDetailR GET|]
  20. 20. Templating[|hamlet|<div .section > <h3>#{title} <p>#{content}|]
  21. 21. Widgets ( EDSL )dateTimeField name = do addScriptRemote “...” addDateTimeCss “...” toWidget [hamlet|<input ... |]autoCompleteField name = do addScriptRemote “...” addDateTimeCss “...” toWidget [hamlet|<input ... |]myForm = do [hamlet|<form ...|] DateTimeField “date” autoCompleteField “tag”
  22. 22. Widgets result<script datetime.js<script autocomplete.js…<link datetime.css<link autocomplete.css…<input ...
  23. 23. Thanks !

×