conal / MemoTrie

Trie-based memo functions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Provide instances of HasTrie for `Text`

ocharles opened this issue · comments

I believe this is the correct instance, but memorizing on Text or String leads to massive memory consumption.

@conal Is this expected because a node is being created for every Char? Or am I just doing something wrong in this file.

-- taken from MemoTrie.hs
enum' :: (HasTrie a) => (a -> a') -> (a :->: b) -> [(a', b)]
enum' f = (fmap . first) f . enumerate

instance HasTrie T.Text where
  newtype (T.Text :->: b) = TextTrie (String :->: b)

  trie f = TextTrie (trie (f . T.pack))

  untrie (TextTrie t) = untrie t . T.unpack

  enumerate (TextTrie t) = enum' T.pack t