# Doubly indexed map

**URL:** <https://discourse.haskell.org/t/doubly-indexed-map/8764>\
**Category:** Learn\
**Created:** [February 9, 2024, 2:28pm UTC](https://discourse.haskell.org/t/doubly-indexed-map/8764 "2024-02-09T14:28:20Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcosh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/marcosh/32/436_2.png) [@marcosh](https://discourse.haskell.org/u/marcosh)\
**Post date:** [February 9, 2024, 2:28pm UTC](https://discourse.haskell.org/t/doubly-indexed-map/8764/1 "2024-02-09T14:28:21Z")

</div>

Suppose I have some tabular data

| | John | James |
| --- | --- | --- |
| January | 3 | 5 |
| February | 2 | 8 |

where the rows have type `Month` and the columns have type `Name`.

I could group the data first by `Month` and then by `Name` and use `type MonthlyData = Map Month (Map Name Int)`.  
Or I could group them first by `Name` and then by `Month` and some something like `type MonthlyData' = Map Name (Map Month Int)`.  
Or I could group them by `Name` and `Month` and use `type MonthlyData'' = Map (Month, Name) Int`.

I would like to be able to access both rows, providing a `Month`, and columns, providing a `Name`.

Is there in the ecosystem a data structure optimised for that?  
If not, would it make sense to have something like

```haskell
data MonthlyData''' = MonthlyData'''
  { groupByName :: Map Name (Map Month Int)
  , groupByMonth :: Map Month (Map Name Int)
  }

 -- uses groupByName
lookupByName :: Name -> MonthlyData''' -> Maybe (Map Month Int)

 -- uses groupByMonth
lookupByMonth :: Month -> MonthlyData''' -> Maybe (Map Name Int)

insert :: Name -> Month -> MonthlyData''' -> MonthlyData'''

update :: Name -> Month -> Int -> MonthlyData''' -> MonthlyData'''

```

where the constructor `MonthlyData'''` is not exposed and `insert` and `update` modify both `groupByMonth` and `groupByName`?

---

<div class="post-metadata">

**Author:** ![adamgundry](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@adamgundry](https://discourse.haskell.org/u/adamgundry)\
**Post date:** [February 9, 2024, 2:50pm UTC](https://discourse.haskell.org/t/doubly-indexed-map/8764/2 "2024-02-09T14:50:16Z")

</div>

The `ixset-typed` package does essentially this. It supports multiple indices and internally it constructs a `Map` from each key type to the values:

> **[ixset-typed](https://hackage.haskell.org/package/ixset-typed)**
>
> Efficient relational queries on Haskell sets.

---

<div class="post-metadata">

**Author:** ![marcosh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/marcosh/32/436_2.png) [@marcosh](https://discourse.haskell.org/u/marcosh)\
**Post date:** [February 9, 2024, 3:56pm UTC](https://discourse.haskell.org/t/doubly-indexed-map/8764/3 "2024-02-09T15:56:17Z")

</div>

that looks really interesting, but in my case the `Ord a` instance required by [Data.IxSet.Typed](https://hackage.haskell.org/package/ixset-typed-0.5.1.0/docs/Data-IxSet-Typed.html#t:Indexable) is quite problematic, since the data I have in the cells are not actually `Int` and they are not easily ordered

---

<div class="post-metadata">

**Author:** ![adamgundry](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@adamgundry](https://discourse.haskell.org/u/adamgundry)\
**Post date:** [February 9, 2024, 9:20pm UTC](https://discourse.haskell.org/t/doubly-indexed-map/8764/4 "2024-02-09T21:20:05Z")

</div>

Thinking about it, it may indeed be easiest to roll your own data structure out of simpler components in `containers`, `unordered-containers`, `array`, etc., especially if you have only two dimensions. The best representation choices (e.g. `Map` vs `IntMap` vs `HashMap` vs `Array` vs `Matrix`) rather depend on the key/value types, data density, access pattern and security considerations.

---

<div class="post-metadata">

**Author:** ![AntC2](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/antc2/32/1872_2.png) [@AntC2](https://discourse.haskell.org/u/AntC2)\
**Post date:** [February 10, 2024, 11:37am UTC](https://discourse.haskell.org/t/doubly-indexed-map/8764/5 "2024-02-10T11:37:10Z")

</div>

I’m very puzzled by the way you’ve structured the q:

- If your data has more months, that’s merely more rows in the table; but
- might your data have more than two names? What would that mean/how would you represent it in the table? (Given there’s usually a limited width.)
- What does the John = 3 value ‘mean’? Could there be a James = 3 value?

I’d expect:

- A Month column, as you have.
- A Name column, with possible values John, James, …
- A payload column with an Int.
- A lookup would need to index by both Month and Name, to return a cell.

Are you really asking for a way to index by Name (say) and return a pair or vector of cells (Ints)?

---

<div class="post-metadata">

**Author:** ![silky](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/silky/32/3555_2.png) [@silky](https://discourse.haskell.org/u/silky)\
**Post date:** [February 11, 2024, 9:00am UTC](https://discourse.haskell.org/t/doubly-indexed-map/8764/6 "2024-02-11T09:00:51Z")

</div>

This is at least mildly related to an earlier quesiton I asked @marcosh - [Nice data-structure for grouping?](http://discourse.haskell.org/t/nice-data-structure-for-grouping/8085)

In that thread, I ended up following basically @tcard 's advice ( [Nice data-structure for grouping? - #4 by tcard](http://discourse.haskell.org/t/nice-data-structure-for-grouping/8085/4) ) but perhaps @mixphix 's library is more to your interest - [Nice data-structure for grouping? - #7 by mixphix](http://discourse.haskell.org/t/nice-data-structure-for-grouping/8085/7)

I also think it would be worth a glance at @LaurentRDC 's fancy new javelin library: [Data.Series](https://hackage.haskell.org/package/javelin-0.1.0.0/docs/Data-Series.html#g:18) ; but it might not work as you need a dataframe-like thing, instead of a series. But maybe, if we ask nicely, @LaurentRDC will add such a feature 😃

---

<div class="post-metadata">

**Author:** ![LaurentRDC](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/laurentrdc/32/4950_2.png) [@LaurentRDC](https://discourse.haskell.org/u/LaurentRDC)\
**Post date:** [February 11, 2024, 1:37pm UTC](https://discourse.haskell.org/t/doubly-indexed-map/8764/7 "2024-02-11T13:37:51Z")

</div>

Dataframes are definitely in the cards. The only problem is that I don’t have the time or need to work on this right now unfortunately.

In the original post:

> I would like to be able to access both rows, providing a `Month` , and columns, providing a `Name` .

You can do so with a `Series`. Assuming that you do not know all the names in advance, in which case you need to store your column values in a `Map`:

```haskell
> import Data.Series ( at )
> import qualified Data.Series as Series
> import Data.Map.Strict ( lookup )
> import qualified Data.Map.Strict as Map
>
> :{ let xs = Series.fromList 
            [ ("January", Map.fromList [ ("John", 3::Int)
                                       , ("James", 5) 
                                       ]
            , ("February", Map.fromList [ ("John", 2)
                                        , ("James", 8)
                                        ]
            ]
:}
> xs
       index | values
       ----- | ------
   'January' | fromList [("John", 3), ("James", 5)]
  'February' | fromList [("John", 2), ("James", 8)]
>
> lookup "James" $ xs `at` "January"
Just 3

```

Just in terms of lookup, this is [O(log n)](https://hackage.haskell.org/package/javelin-0.1.0.0/docs/Data-Series.html#v:at) + [O(log m)](https://hackage.haskell.org/package/containers-0.7/docs/Data-Map-Internal.html#v:lookup), where n is the length of the index, and m is the number of “columns”, so it’s pretty efficient.

However, inserting and updating a `Series` is very slow because they are based on arrays. Every update results in an array copy.

If you want to process the data in the columns efficiently, then you might need a true dataframe structure, which is column-oriented (while `Series` is row-oriented).

Hope this helps

---

<div class="post-metadata">

**Author:** ![marcosh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/marcosh/32/436_2.png) [@marcosh](https://discourse.haskell.org/u/marcosh)\
**Post date:** [February 12, 2024, 11:43am UTC](https://discourse.haskell.org/t/doubly-indexed-map/8764/8 "2024-02-12T11:43:01Z")

</div>

[deep-map](https://github.com/mixphix/deep-map) looks exactly like what I had in mind. Unfortunately there’s no documentation on Hackage

---

<div class="post-metadata">

**Author:** ![BurningWitness](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/burningwitness/32/3665_2.png) [@BurningWitness](https://discourse.haskell.org/u/BurningWitness)\
**Post date:** [February 12, 2024, 12:14pm UTC](https://discourse.haskell.org/t/doubly-indexed-map/8764/9 "2024-02-12T12:14:57Z")

</div>

> [@marcosh](#):
>
> Is there in the ecosystem a data structure optimised for that?

If there existed a dictionary with optimal lookup in both directions, databases would use it everywhere. Since one does not appear to exist, every library you’re directed to is merely composing other data structures.

The correct answer, in my mind, is to use optimal data structures directly. Your example is best solved as two dictionaries that are updated in tandem, exactly the way you outlined (although I’d argue for fewer backticks in names).

* * *

A broader design point to make here is that while your state _can_ be so tight as to exclude any invalid positions, in a lot of places in the real world this is not practical. What you should strive for is instead that at every point in your application the state remains coherent. You may think dictionaries are “unsafe” specifically because they are full of invalid states, however your problem cannot be solved in any other way, so you might as well cut down on pointless dependencies.

Also [here](https://old.reddit.com/r/haskell/comments/1abq0en/nested_field_update/kjp67hm/) is a link to a Reddit post that links to a video that makes a parallel between databases and dictionaries, you may find it useful.

---

<div class="post-metadata">

**Author:** ![mixphix](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/mixphix/32/5629_2.png) [@mixphix](https://discourse.haskell.org/u/mixphix)\
**Post date:** [February 12, 2024, 2:37pm UTC](https://discourse.haskell.org/t/doubly-indexed-map/8764/10 "2024-02-12T14:37:05Z")

</div>

That’s weird, there are definitely Haddocks. Sorry about that. I’m not exactly sure how to get Hackage to run the generator thing.

---

<div class="post-metadata">

**Author:** ![silky](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/silky/32/3555_2.png) [@silky](https://discourse.haskell.org/u/silky)\
**Post date:** [February 12, 2024, 3:09pm UTC](https://discourse.haskell.org/t/doubly-indexed-map/8764/11 "2024-02-12T15:09:43Z")

</div>

I think the problem is that it just isn’t building - [Hackage: Build #1 for deep-map-0.2.0](https://hackage.haskell.org/package/deep-map-0.2.0/reports/1)

---

<div class="post-metadata">

**Author:** ![mixphix](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/mixphix/32/5629_2.png) [@mixphix](https://discourse.haskell.org/u/mixphix)\
**Post date:** [February 12, 2024, 4:26pm UTC](https://discourse.haskell.org/t/doubly-indexed-map/8764/12 "2024-02-12T16:26:06Z")

</div>

Thanks for pointing that out! It seems it built with a version of GHC that didn’t support `\cases` syntax.
