I’m running into a design issue and I need your help.
I have a database containing my tables (Dict
):
type alias Database =
{ table1 : Dict Int Int
, table2 : Dict Int String
}
I want to remove
an id in each table. To do this I can call Dict.remove
on each table:
remove : Int -> Database -> Database
remove id database =
{ table1 = Dict.remove id database.table1
, table2 = Dict.remove id database.table2
}
The issue is that when I add a new table I have to add it to the remove
function.
Now I want to know the size of my database. I can call Dict.size
for each table like I did for remove
:
size : Database -> Int
size database =
Dict.size database.table1
+ Dict.size database.table2
So now, when I add a new table, I need to add it to the remove
and size
function.
And so on… In fact, what I want, like for any other container, is to have a foldl
function which could look like:
foldl : (Dict Int a -> result -> result) -> result -> Database -> result
foldl func result database =
result
|> func database.table1
|> func database.table2
With foldl
, when I add a new table I have to add it to the foldl
function but, only in foldl
function. But this doesn’t compile for an understandable reason: Type mismatch . Required: Dict Int a Found: Dict Int Int
.
In fact, all my tables (Dict
) have a common thing: Their id (Int
). If they have a “common thing” I want to do “common actions” on them. But, I can’t figure how to.
The best would be to iterate over my tables in foldl
to avoid adding a line each time I add a table. But to do this I think we need reflection.
I love elm because it helps me to generalize my local design issues to the simplest form. A big thanks to everyone in the Elm community!
I’m really curious to hear your answers !