以下折叠功能会将重复的玩家加载到基本记录中:
$scope.validValues = [{
"code": "LKG",
"decode": "LKG"
}, {
"code": "UKG",
"decode": "UKG"
}, {
"code": "1",
"decode": "class 1"
}, {
"code": "2",
"decode": "class 2"
}, {
"code": "3",
"decode": "class 3"
}, {
"code": "4",
"decode": "class 4"
}, {
"code": "5",
"decode": "class 5"
}, {
"code": "6",
"decode": "class 6"
}, {
"code": "7",
"decode": "class 7"
}, {
"code": "8",
"decode": "class 8"
}, {
"code": "9",
"decode": "class 9"
}, {
"code": "10",
"decode": "class 10"
}, {
"code": "11",
"decode": "class 11"}
];
测试如下:
(initializedBase, plays) ||> List.fold (fun bases play ->
Some play.Player |> move bases)
实际结果是:
[<Test>]
let ``2 players each hit single``() =
// Setup
let players = [{ Player= Scott; Hit= Single }; { Player= Brian; Hit= Single }]
// Test
let bases = players |> assignBases
bases |> should equal { First=Some Brian; Second=Some Scott; Third=None }
我期待:
{ First=Some Scott; Second=Some Scott; Third=None }
整个代码在这里:
{ First=Some Brian; Second=Some Scott; Third=None }
答案 0 :(得分:1)
问题似乎与Seq.fold
无关。如果我没弄错的话,错误发生在move
函数中匹配表达式的第二行:
| { First=player; Second=None; Third=None } -> { bases with First=player; Second=bases.First }
该模式使用player
来捕获First
的{{1}}字段的值。虽然看起来应该是这样,但不匹配来自匹配表达式之外的bases
的值!如果您需要,则需要另外使用player
子句。
实际上,从F#确定第二个案例是正确的案例(when
和Second
为Third
而None
有{First
时1}} value),直到控件离开整个匹配表达式,名称Some
表示新绑定,并且被称为“遮蔽”旧的player
值。 / p>
因此,当您在该匹配案例的结果部分中将player
分配给player
时,这又将是First
的原始值 - 与您为{设置的相同{1}}。
那么你想要的第二个匹配案例是
bases.First
现在,Second
仍然引用与匹配表达式之外的值相同的值,即您要在| { First=oldFirst; Second=None; Third=None } -> { bases with First=player; Second=oldFirst }
输入字段的值,player
获取您的播放器“从“First
获取”。