我希望我能做到:
let myFun param1 param2 =
.....
if condition 1
....
if condition 2
yield ....
let sequence = seq { for x in xs do
myFun 1 x
myFun 2 x
}
但现在我只能做
let myFun param =
.....
.....
if condition 1
....
if condition 2
Some....
else None // has to complete else
else None
let sequence = seq { for x in xs do
match myFun 1 x with
| Some x -> yield x
| None -> ()
match myFun 2 x with
| Some x -> yield x
| None -> ()
}
let sequence2 = // order will be different, and we can not have index passed into function unless we do a futher Seq.map
let a = xs |> Seq.choose (myFun 1)
let b = xs |> Seq.choose (myFun 2)
Seq.append a b
我的真实代码:
代码看起来不太好None
,我试过Seq.choose
了,因为它不支持Seq.choosei
。
let exam isForward brickID =
let brick = state.[brickID]
let (nextTR, nextOccupied, nextUnoccupied) = if isForward then brick.Body.nudgeForward () else brick.Body.nudgeBack ()
if (nextOccupied.isInGrid gridSize) && (map.ContainsKey nextOccupied |> not) then
let nextBrick = { brick with Body = nextTR }
let nextState = getNextState state brickID nextBrick
if not (explored.Contains nextState) then
let nextMap = map |> Map.remove nextUnoccupied
|> Map.add nextOccupied nextBrick
Some ((nextState, nextMap), (nextBrick, if isForward then brick.Body.Direction else brick.Body.Direction.Opposite))
else None
else None
seq { for brickID in 0 .. state.Length - 1 do
match exam true brickID with
| Some x -> yield x
| None -> ()
match exam false brickID with
| Some x -> yield x
| None -> ()
}