MemoTrie icon indicating copy to clipboard operation
MemoTrie copied to clipboard

Provide instances of HasTrie for `Text`

Open ocharles opened this issue 7 years ago • 1 comments

ocharles avatar Feb 23 '18 09:02 ocharles

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

Avi-D-coder avatar Aug 04 '19 04:08 Avi-D-coder