How to compile a resource into a binary in Haskell?

后端 未结 4 1851
小蘑菇
小蘑菇 2020-12-29 04:12

Say I have a dictionary.txt file, I need to read it into a map and use it in my program, how can I make this dictionary.txt file contained in the compiled exe f

4条回答
  •  旧巷少年郎
    2020-12-29 05:05

    You can store serialized data types as bytestring literals. Here's an example:

    • http://code.haskell.org/~dons/code/compiled-constants/
    • http://haskell.org/haskellwiki/Compiling_in_constants

    However, the file-embed automates this process, making it easier to do non-trivial embeddings.

    All approaches essentially boil down to representing the static data as a bytestring literal,

    {-# LANGUAGE OverloadedStrings #-}
    
    import Data.Binary
    import qualified Data.Map as M
    import qualified Data.ByteString.Char8 as S
    import Data.ByteString.Lazy
    import Codec.Compression.GZip
    
    --
    -- this is a gzip compressed literal bytestring, storing a binary-encoded Data.Map
    --
    mytable =
        "\US\139\b\NUL\NUL\NUL\NUL\NUL\NUL\ETXEN\
        \\219\SO\194 \f\197\224\188\196\CAN\227\US\
        \\224\171~\NAKc\GS4ce\161`\178\191\215(\176\
        \\190\180\167\231\210\n\241\171\203\191\ti\
        \\157\217\149\249< \ENQ\214\&9>\202\162\179a\
        \\132X\233\ESC=\231\215\164\SYN\157\DC2D\226*\
        \\146\174o\t\167\DLE\209\"i_\240\193\129\199

提交回复
热议问题