这是可以改进的,还是可以变得更简洁?
type Government = {
Id : Id;
Name : string;
Abbreviation : string;
ParentId : string option;
}
let govList =
[
{Id = "1"; Name = "United States"; Abbreviation = "USA"; ParentId = None}
{Id = "2"; Name = "California"; Abbreviation = "CA"; ParentId = Some("1")}
{Id = "3"; Name = "Texas"; Abbreviation = "TX"; ParentId = Some("1")}
{Id = "4"; Name = "Houston"; Abbreviation = "HOU"; ParentId = Some("3")}
{Id = "5"; Name = "Dallas"; Abbreviation = "DAL"; ParentId = Some("3")}
{Id = "6"; Name = "San Antonio"; Abbreviation = "SANAN"; ParentId = Some("3")}
{Id = "7"; Name = "El Paso"; Abbreviation = "ELP"; ParentId = Some("3")}
{Id = "8"; Name = "Canada"; Abbreviation = "CAN"; ParentId = None}
{Id = "9"; Name = "France"; Abbreviation = "FRN"; ParentId = None}
]
type GovernmentStructure<'gov> =
| Root of Government : 'gov * SubGov : GovernmentStructure<'gov> list
| Node of Government : 'gov * SubGov : GovernmentStructure<'gov> list
| Leaf of Government : 'gov
let insertGovernment (posGov: Government) (newGov : Government)
(currentStructure : GovernmentStructure<Government>) =
let updateStructure (govPos : Government) (newGov : Government)
(currentStructure : GovernmentStructure<Government>) =
// iterate over the nodes to find position and insert. parentId is useless, will fix later.
let rec findPosAndInsertInto parentId (startStructure : GovernmentStructure<Government>) =
match startStructure with
| Node (g, sg) when g = govPos ->
Node(g, [
yield Node(newGov, [])
yield! sg
])
| Node (g, sg) ->
Node (g, [
for x in sg do
yield findPosAndInsertInto g.Id x
])
| Leaf(g) when g = govPos ->
Node (g, [Leaf(newGov)])
// better logic needed, should not hit this
| node -> node
match currentStructure with
// Insert if position is at root
| Root(gov', subGov) when gov' = govPos ->
Root(gov', [
yield Node(newGov, [])
yield! subGov
])
// Insert if position if at node or leaf
| node ->
match node with
| Root (gov', subGov) ->
Root(gov', [
for x in subGov do
yield findPosAndInsertInto gov'.Id x
])
// better logic needed, shouldn't hit this level
| node -> node
updateStructure posGov newGov currentStructure发布于 2016-11-23 19:38:46
通过使用缩写本身作为标识符,可以简化记录类型;ISO 3166至少为国家和直属人员提供了标准代码(尽管有冲突-西澳大利亚州和华盛顿州都是“WA”-因此您可能希望由其父母对它们进行限定,例如,USA:WA诉AUS:WA)。
https://codereview.stackexchange.com/questions/146459
复制相似问题