I have a list like this: (Pseudo notation)
(X,...) -> (X,...) -> (X,...) -> ...
| | |
V V V
(Y,...) (Y,...) (Y,...)
| | |
V V V
(Z,...) (Z,...) (Z,...)
Type is (Enum a, Bounded a) => [[(a,x)]]
. But I need something like this:
(X, ... -> ... -> ... -> ...
|
V
(Y, ... -> ... -> ... -> ...
|
V
(Z, ... -> ... -> ... -> ...
Type is like (Enum a, Bounded a) => [(a,[x])]
x has an arbitrary number of elements. It can be assumed, that each Member of x is a key in each sublist of the first list.
How is this transformation possible as a lazy haskell algorithm (List doesn't needs to be evaluated completely to return (partitially) result)?
Test data
See above, something like this:
--Input
[[(Foo,1),(Bar,1),(Baz,1)],[(Foo,2),(Bar,2),(Baz,2)],...]
--Output
[(Foo,[1,2,3,...]),(Bar,[1,2,3,...),(Baz,[1,2,3,...])]
What I want to do with the data
I want to use it in a function like this:
myFunc :: [(MyEnum,[Int])]
myFunc x@((_,(_:[])):_) = x
myFunc x = foldTheListRecursively
The function has to work on large amounts of data (~10'000 entries per enum), the list should be garbage collectable by the runtime system (The list is adhoc build by another part of the program)
My (uggly) implementation
This is the way, I implemented it, but obviously it doesn't fits the requirements, as the list is traversed multiple times:
restructList :: [[(a,x)]] -> [(a,[x])]
resturctList list = (\x -> (x,listFor x)) <$> keys where
keys = fst <$> head list
listFor x = snd <$> any ((==x).fst) <$> list
I'm not at home so can't test it, so there may be a mistake.