-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLookup.hs
More file actions
24 lines (17 loc) · 624 Bytes
/
Lookup.hs
File metadata and controls
24 lines (17 loc) · 624 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module HelVM.HelMA.Common.Collections.Lookup where
import qualified Data.IntMap as IntMap
import qualified Data.Sequence as Seq
index :: (Show c , Lookup e c) => c -> Int -> e
index c i = check (c `indexMaybe` i) where
check (Just e) = e
check Nothing = error $ "Empty stack " <> show c <> " index " <> show i
indexMaybe :: Lookup e c => c -> Int -> Maybe e
indexMaybe = flip lookup
class Lookup e c | c -> e where
lookup:: Int -> c -> Maybe e
instance Lookup e [e] where
lookup = flip (!!?)
instance Lookup e (Seq e) where
lookup = Seq.lookup
instance Lookup e (IntMap e) where
lookup = IntMap.lookup