给定的是应用程序和容器的配置,其中一个应用程序能够拥有多个容器,一个容器能够拥有多个应用程序。我希望能够以两种方式输出它们
- 每个应用列出容器
- 每个容器列出应用程序
数据格式很简单,但我似乎无法找到一种方法来获得这两种表示而不重复关系。
从具有应用程序的容器开始时的示例数据
let app1 = { name = "app1" }
let app2 = { name = "app2" }
let containers = [
{ name = "container1", apps = [ app1 ] },
{ name = "container2", apps = [ app1, app2 ] }
]
{- I can easily transform this data to the following -}
[
{ app = "app1", container = "container1" },
{ app = "app1", container = "container2" },
{ app = "app2", container = "container2" }
]
{- But I cannot seem to get it into the requested format -}
[
"app1" = [ "container1", "container2" ]
"app2" = [ "container2" ]
]
我认为使用标识符是Text
行不通的,因为没有办法使用相等的标识符来合并关联列表或类似的东西。
使用记录我可以合并这样的东西{a1 = {c1 = True}} /\ {a1 = {c2 = True}} /\ {a2 = {c2 = True}}
。这将是{a1 = {c1 = True, c2 = True}, a2 = {c2 = True}}
。但我一开始就无法达到这种状态,因为我无法“逆转”记录。
我不在乎我需要如何构建配置,只要我不需要重复两次关系。