ãã®è¨äºã§ã¯ãåºæ¬ã©ã¤ãã©ãªã§ãã Prelude ã®é¢æ°ãã ãããç解ãã人ãã次ã«ç¥ãã¹ãã©ã¤ãã©ãªãç´¹ä»ãã¾ããèªç±èªå¨ã«ãªã¹ãã使ãããªããæ£è¦è¡¨ç¾ããªãã¦ãããã°ã©ãã³ã°ãã§ãããã ãªã¨å®æãã人ã対象ã«ãã¦ãã¾ãã
ãã®è¨äºã®ãã¼ãã¯ãè±ãªã¹ãã§ãããªã¹ãã¯ã¨ã¦ãæè»ã§ããããªã¹ãã§è¡¨ç¾ããã¦ããæååã¯ãメモリーをたくさん消費しますãããªã«ããé ãã®ã§ããå®ç¨çãªããã°ã©ã ãæ¸ãããã«ã¯ãå¿ è¦ã«å¿ãã¦é©åãªãã¼ã¿æ§é ã使ãå¿ è¦ãããã¾ãã
containers
containersã¯ãæåéãã³ã³ããåãããã¤ãéããããã±ã¼ã¸ã§ããããã·ã¥ã®ä»£æ¿åããã¥ã¼ã¨ãã¦ä½¿ãã¾ããé£æ³ãªã¹ãã使ã£ã¦ããã¨ããã¯ããã¹ã¦ Data.Map ãªã©ã§ç½®ãæãããã¨ããå§ããã¾ãã
containers ã«å ¥ã£ã¦ããã¢ã¸ã¥ã¼ã«ã¯ãã¹ã¦çºãã¾ããããããã¦ãå®è£ ãèªãã§ã¿ã¾ãããã(ããã°ã©ãã³ã°ã»ã³ã³ãã¹ãã«åªåããããã«ã¯ãData.Sequence ã®ç解ã¯å¿ é ã§ãã:)
Purely functional (æ°¸ç¶ç)ãªå¯å¤ããã·ã¥ã§ãä»ä¸çªæ³¨ç®ããããã®ã¯ hashmapã§ããããã¯ããã®å containers ã«çµ±åãããã¨æãã¾ãã詳ããã¯ãThe performance of Haskell containers package ãèªãã§ä¸ããã
Purely functional data structure ã®å ¥éã¨ãã¦ã¯ã20分でわかるPurely Functional Data Structuresãç§é¸ã§ããã§ããHaskell ã® containers ã«å ¥ã£ã¦ãããã¼ã¿åã¯ãæ£æ ¼ã¨ãã¦å®ç¾©ããã¦ããã®ã§ããã¾ãé 延ãã¾ãããã©ããã
ByteString
ãã¤ãåãããã¯ASCIIæååãæä½ããã«ã¯ãbytestring ã使ãã¾ãã
ã¾ããOverloadedStrings ã¨ããè¨èªæ¡å¼µãè¦ãã¾ããããããã使ãã°ãããã«ã¯ãªã¼ãã§å²ãã æååããByteString ãªãã©ã«ã¨ãã¦æ±ã£ã¦ãããããã«ãªãã¾ãã
ByteString ã¯ããªã¹ãã¨éã£ã¦ãã¿ã¼ã³ãããã§ãã¾ãããããã¬ã¼ããé§ä½¿ãã¦é¢æ°ãå®ç¾©ãããã¨ã«ãªãã¾ãããªã¹ãã§ã¯ length ã O(N) ãªã®ã§ä½¿ãã¹ãã§ã¯ããã¾ããããByteString ã§ã¯ O(1) ãªã®ã§ãã©ãã©ãå©ç¨ãã¾ããããå°ãçºæ³ãå¤ãã¦ããã°ã©ãã³ã°ããå¿ è¦ãããã¾ãããããã«æ £ããã§ãããã
ByteString ã«ã¯ä»¥ä¸ã®åã¤ã®ã¢ã¸ã¥ã¼ã«ããã£ã¦ãã©ãã使ãã°ããã®ãæ©ãããããã¾ããã
- Data.ByteString (æ£æ ¼ Word8)
- Data.ByteString.Char8 (æ£æ ¼ Char)
- Data.ByteString.Lazy (é 延 Word8)
- Data.ByteString.Lazy.Char8 (é 延 Char)
ãã¨ãã°ãData.ByteString 㨠Data.ByteString.Char8 ã§å®ç¾©ããã¦ãã ByteString ã¯åãç©ã§ããåã«ã¤ã³ã¿ã¼ãã§ã¤ã¹ããData.Word ã§å®ç¾©ããã¦ãã Word8 ãåãããChar ãåããã®éãã§ããå¤æã®ãªã¼ãã¼ããããæ°ã«ãªãããæå(Char)ã便å©ã«ä½¿ããããªã Data.ByteString.Char8 ã使ãã¾ããããå°ãä¸ä¾¿ã ããã©ãæ°å¤ãæå®ãã¦å¤æã®ãªã¼ãã¼ãããããªãããããªã Data.ByteString ã使ãã¾ãããã
ãªããOverloadedStrings ã®ããã«ã¯ãData.ByteString.Char8 ã® instance 宣è¨ãèªã¿è¾¼ãå¿ è¦ãããã¾ããData.ByteString ã使ãããããByteString ãªãã©ã«ã使ãããå ´åã¯ã以ä¸ã®ããã«æå®ããå¿ è¦ãããã¾ãã
{-# LANGUAGE OverloadedStrings #-} import Data.ByteString (ByteString) import qualified Data.ByteString as BS import Data.ByteString.Char8 () -- instance ã ãèªã¿è¾¼ã
ã¨ãã訳ã§ãéããæ°ã«ãã¹ããªã®ã¯ãæ£æ ¼ ByteString ã¨é 延 ByteString ã§ãã使ãæ¹ã®çµè«ãè¿°ã¹ãã¨ãå ¥åã«ã¯æ£æ ¼ ByteStringãåºåã¯é 延 ByteString ãç¨ãã¾ãããããã風ã«ä½¿ãã¦ãªãã¨ãªãã¨ãããªãã®ã³ã¼ãã¯ã¾ã ã¾ã ã¢ããã¥ã¢ãªã®ã§ãã
Text
Unicode æååãæ±ãã«ã¯ textã使ãã¾ããå©ç¨æ㯠ByteString ã¨åãã§ãããã ãlength ã O(N) ãªã®ã§ãå©ç¨ã¯æ§ãã¾ãããã
Parsec
parsec ã¯ããã¼ãµã¼ã©ã¤ãã©ãªã®å®çªã§ããããããç解ãã¦ããã°ãä»ã®ãã¼ãµã¼ã©ã¤ãã©ãªãç解ããã®ã¯ãã¨ã¦ãç°¡åã§ãã
Parsec ã¯é ãåé¢ãã©ãã§ã©ãããã¨ã©ã¼ãèµ·ãã£ããåããã¾ãã®ã§ãè¨å®ãã¡ã¤ã«ã®è§£æãªã©ãã¦ã¼ã¶ã«è¿ãã¨ãããå®è£ ããã®ã«é©ãã¦ãã¾ãã
æ®å¿µãªãã¨ã«ãParsec ã¯ç¾å¨æ··ä¹±ç¶æ³ã«ããã¾ãã
parsec ã«ã¯ãã¼ã¸ã§ã³3ç¸å½ã® Text.Parsec ã¨ãã¼ã¸ã§ã³2ç¸å½ã® Text.ParserCombinators.Parsec ãæä¾ããã¦ãã¾ãããã¼ã¸ã§ã³3ã§ã¯ãString ã«å ãã¦ãByteString ããµãã¼ããããã«ãé¢ããããText ã¯ãµãã¼ãããã¦ãã¾ããã
ã¾ããæ··ä¹±ãé¿ããããã«ããã±ã¼ã¸ã parsec2 㨠parsec3 ã«åé¢ãããã®ã«ãHaskell Platform 㯠parsec ãæ¡ç¨ãã¦ãã¾ãã¾ããã
æ··ä¹±ã¯ãã°ããç¶ãã§ãããã
attoparsec
attoparsecã¯é«éãªãã¼ãµã¼ã©ã¤ãã©ãªã§ããã¨ã©ã¼éç¥ã®æ©è½ã¯è²§å¼±ã§ããã§ãã®ã§ãã¦ã¼ã¶ããé ãã¨ããã§ãé«éå¦çãããã®ã«é©ãã¦ãã¾ããattoparsec ã対象ã¨ããã®ã¯ ByteString ã§ããText ã対象ã¨ãããattoparsec-text ã¨ããã©ã¤ãã©ãªãããã¾ãã
enumerator
Enumerator/Iteratee 㯠Haskell ã³ãã¥ããã£ã§ä»æãç±ããã©ãã¤ã ã§ãããã®ãã©ãã¤ã ã使ãã°ãä¸æ岩ã«ãªããã¡ãª IO ãçç£è ã¨æ¶è²»è ã«åããããããåæãããã¨ã§ããã°ã©ã ãæ§æã§ãã¾ãã
ãã®ãã©ãã¤ã ã®å ¥éã«ã¯ã使ってみよう Enumeratorã¨Enumeratorは終了条件の検査からの解放だã¨ããè¨äºããå§ããã¾ãã
Enumerator/Iterateeãå®è£ ããã©ã¤ãã©ãªã¨ãã¦ã¯ã以ä¸ã®3ã¤ãããã¾ãã
- iteratee
- Enumerator/Iterateeã®ãã©ãã¤ã ãèãã Oleg ããã®ã©ã¤ãã©ãª
- enumerator
- ç¾å¨ä¸çªäººæ°ã®ããã©ã¤ãã©ãª
- iterIO
- ææ°ã®æ´ç·´ãããã©ã¤ãã©ãªã赤丸æ¥ä¸æä¸
attoparsec ã¯ãenumerator ã¨ä¸ç·ã«ä½¿ãã®ããå§ãã§ãããã®ããã«ãattoparsec-enumeratorã¨attoparsec-text-enumeratorã¨ããããã±ã¼ã¸ãããã¾ãã
blaze-builder
æååã®é£çµã¯å³çµåã«ãã㨠O(N) ã§ãããå·¦çµåã«ããã¨O(N^2)ã«ãªã£ã¦ãã¾ãã¾ããããã§ãæååã®é£çµãå³çµåã«ãããã¨ãä¿è¨¼ãããã¯ããã¯ã¨ãã¦ãå·®åãªã¹ããå©ç¨ããã¦ãã¾ããã
blaze-builderã¯ãå·®åãªã¹ãããã®ä»ã®ã¢ã¤ãã£ã¢ãèåãããé«éãªæååçæã©ã¤ãã©ãªã§ããæååã®åºåã«ã¯ããã®ã©ã¤ãã©ãªã使ãã¾ãããã