Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What Python can learn from Haskell

Gideon de Kok
May 11, 2013
170

What Python can learn from Haskell

Or basically, what makes Haskell cool in the modern era ;)

Gideon de Kok

May 11, 2013
Tweet

Transcript

  1. Daniël @danieldekok Developer Lan ua e Technolo y at Gridline

    Workin on NLP based technolo y on a day to day basis (Java, C++, Haskell) Saturday, May 11, 13
  2. Gideon @ ideondk Tech-lead at SpotDo Focusin on functional and

    actor based pro rammin throu h Scala and native application development Saturday, May 11, 13
  3. The real-time web constant exchan e of data, both in

    lar e and small packa es Saturday, May 11, 13
  4. Callback hell anyone? fs.readdir(source, function(err, files) { if (err) {

    console.log('Error finding files: ' + err) } else { files.forEach(function(filename, fileIndex) { console.log(filename) gm(source + filename).size(function(err, values) { if (err) { console.log('Error identifying file size: ' + err) } else { console.log(filename + ' : ' + values) aspect = (values.width / values.height) widths.forEach(function(width, widthIndex) { height = Math.round(width / aspect) console.log('resizing ' + filename + 'to ' + height + 'x' + height) this.resize(width, height).write(destination + 'w' + width + '_' + filename, function(err) { if (err) console.log('Error writing file: ' + err) }) }.bind(this)) } }) }) } }) Saturday, May 11, 13
  5. Bi data & real-time requirements process lots of data, with

    as few resources as possible Saturday, May 11, 13
  6. Developer requirements composable transformations, to work on lar e streams

    of data as easily as possible ( while keepin yourself DRY ) Saturday, May 11, 13
  7. Mappin map :: (a -> b) -> [a] -> [b]

    map f xs = [f x | x <- xs] Saturday, May 11, 13
  8. A Haskell example obtainKeywords :: [HTMLPage] -> [KeywordSet] obtainKeywords =

    map extractPlainText . map tokenize . map toTokenSet Saturday, May 11, 13
  9. More transformations obtainImportantKeywords :: [HTMLPage] -> KeywordSet obtainImportantKeywords = filter

    importantDocument . map extractPlainText . map tokenize . map toTokenSet . foldl' Set.union Set.empty Saturday, May 11, 13
  10. Not so fancy error handlin ‘either’ obtainImportantKeywords :: [HTMLPage] ->

    KeywordSet obtainImportantKeywords = filter importantDocument . map extractPlainText . map tokenize . map toTokenSet . foldl' Set.union Set.empty Saturday, May 11, 13
  11. Teachin an old fold new tricks obtainImportantKeywords :: Iteratee KeywordSet

    m KeywordSet obtainImportantKeywords = getPages $= EL.filter importantDocument $= EL.map extractPlainText $= EL.map tokenize $= EL.map toTokenSet $$ EL.fold Set.union Set.empty Saturday, May 11, 13
  12. Enumerator obtainImportantKeywords :: Iteratee KeywordSet m KeywordSet obtainImportantKeywords = getPages

    $= EL.filter importantDocument $= EL.map extractPlainText $= EL.map tokenize $= EL.map toTokenSet $$ EL.fold Set.union Set.empty getPages Saturday, May 11, 13
  13. Iteratee obtainImportantKeywords :: Iteratee KeywordSet m KeywordSet obtainImportantKeywords = getPages

    $= EL.filter importantDocument $= EL.map extractPlainText $= EL.map tokenize $= EL.map toTokenSet $$ EL.fold Set.union Set.empty EL.fold Set.union Set.empty Saturday, May 11, 13
  14. Enumeratees obtainImportantKeywords :: Iteratee KeywordSet m KeywordSet obtainImportantKeywords = getPages

    $= EL.filter importantDocument $= EL.map extractPlainText $= EL.map tokenize $= EL.map toTokenSet $$ EL.fold Set.union Set.empty $= EL.filter importantDocument $= EL.map extractPlainText $= EL.map tokenize $= EL.map toTokenSet Saturday, May 11, 13
  15. Operators obtainImportantKeywords :: Iteratee KeywordSet m KeywordSet obtainImportantKeywords = getPages

    $= EL.filter importantDocument $= EL.map extractPlainText $= EL.map tokenize $= EL.map toTokenSet $$ EL.fold Set.union Set.empty $= $= $= $= $$ Saturday, May 11, 13
  16. Combinin Iteratees combinedIteratee = do a <- EL.take 3 b

    <- EL.take 3 return (a,b) Saturday, May 11, 13
  17. Internals in code data Stream i = Chunks [i] |

    EOF deriving (Show, Eq) data Step i m o = Continue (Stream i -> Iteratee i m o) | Yield b (Stream o) | Error Exc.SomeException Saturday, May 11, 13
  18. Constructin an Iteratee consume :: Monad m => Iteratee a

    m [a] consume = liftI $ step id where step acc chunk = case chunk of Chunks [] -> Continue $ returnI . step acc Chunks xs -> Continue $ returnI . (step $ acc . (xs ++)) EOF -> Yield (acc []) EOF Saturday, May 11, 13
  19. Stream processin is ettin hip in other lan ua es

    as well! Saturday, May 11, 13
  20. Java 8 introduces a new stream API (althou h limited

    in comparison) Saturday, May 11, 13
  21. case class LogMessage(ip: String, code: Int, message: String) val (enum,

    channels) = Concurrent.Broadcast[Array[Byte]] val converter = Enumeratee.map[Array[Byte]](ba => JsValue(ba)) val reader = Enumeratee.map[JsValue](evt => LogMessage(evt("id"), evt("code"), evt("message"))) val filterErrors = Enumeratee.filter(e => e.code == 500) def streamErrors = Action { request => enum &> converter &> reader &> filterErrors &> Comet(callback = "parent.message")) } Showin off Iteratees in Scala Saturday, May 11, 13
  22. streams.py import re from stream import filter, map, cut result

    = open('file') \ >> filter(re.compile(regex).search) \ >> map(re.compile(' |:|\.').split) \ >> cut[3] \ >> list Saturday, May 11, 13
  23. First meetup in two weeks contact either of us for

    more info! http://www.meetup.com/Functional-Gronin en/ Saturday, May 11, 13