File: Generic.hs

package info (click to toggle)
haskell-memotrie 0.6.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 84 kB
  • sloc: haskell: 320; makefile: 2
file content (25 lines) | stat: -rw-r--r-- 748 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{-# LANGUAGE DeriveGeneric, TypeOperators, TypeFamilies #-}
import Data.MemoTrie
import GHC.Generics (Generic) 

data Color = RGB Int Int Int
           | NamedColor String 
  deriving (Generic) 

instance HasTrie Color where
  newtype (Color :->: b) = ColorTrie { unColorTrie :: Reg Color :->: b } 
  trie      = trieGeneric ColorTrie 
  untrie    = untrieGeneric unColorTrie
  enumerate = enumerateGeneric unColorTrie

runColor (RGB r g b) = r + g + b
runColor (NamedColor s) = length [1..10e7] 

runColorMemoized = memo runColor 

main =
  do putStrLn "first call (should take a few seconds): " 
     print$ runColorMemoized (NamedColor "")
     putStrLn "cached call (should be instantaneous): " 
     print$ runColorMemoized (NamedColor "")