on 92b6021 help sign in
typerep-map/README.md
58 lines 2.1 kB history raw
1# typerep-map
3![electricity](https://user-images.githubusercontent.com/8126674/44323413-788dd700-a484-11e8-842e-f224cfaa4206.png)
4[![Hackage](https://img.shields.io/hackage/v/typerep-map.svg)](https://hackage.haskell.org/package/typerep-map)
5[![Build status](https://secure.travis-ci.org/kowainik/typerep-map.svg)](https://travis-ci.org/kowainik/typerep-map)
6[![MIT license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/vrom911/typerep-map/blob/master/LICENSE)
8`typerep-map` introduces `TMap` and `TypeRepMap` — data structures like [`Map`](http://hackage.haskell.org/package/containers-0.6.0.1/docs/Data-Map-Lazy.html#t:Map), but where types serve as keys, and values have the types specified in the corresponding key spots.
10For the more details on the implementation see [this blog post](https://kowainik.github.io/posts/2018-07-11-typerep-map-step-by-step.html).
12## Usage example
14```haskell
15ghci> import Data.TMap
17ghci> tm = insert True $ one (42 :: Int)
19ghci> size tm
22ghci> res = lookup tm
24ghci> res :: Maybe Int
25Just 42
27ghci> res :: Maybe Bool
28Just True
30ghci> res :: Maybe String
31Nothing
33ghci> lookup (insert "hello" tm) :: Maybe String
34Just "hello"
36ghci> member @Int tm
37True
39ghci> tm' = delete @Int tm
41ghci> member @Int tm'
42False
43```
45## Benchmarks
47Tables below contain comparision with `DMap TypeRep` of ten `lookup` operations
48on structure with size `10^4`:
50| | ghc-8.2.2 | ghc-8.4.3 |
51|----------------|-----------|-----------|
52| `DMap TypeRep` | 517.5 ns | 779.9 ns |
53| `typerep-map` | 205.3 ns | 187.2 ns |
55 ghc-8.2.2 | ghc-8.4.3
56:---------:|:-----------:
57![DMap 8.2.2](https://user-images.githubusercontent.com/4276606/42495129-c700f21e-8454-11e8-98b4-ba080259c712.png) | ![DMap 8.4.3](https://user-images.githubusercontent.com/4276606/42495168-ebb1d13c-8454-11e8-9d17-f6da29d2169a.png)
58![TMap 8.2.2](https://user-images.githubusercontent.com/4276606/42494935-3a352d96-8454-11e8-985e-ebc77cc51ca0.png) | ![TMap 8.4.3](https://user-images.githubusercontent.com/4276606/42495147-d884bdf4-8454-11e8-887f-9815fd2b8d68.png)