在这种情况下如何处理序列构造,因为我们没有断点也没有强大的Seq.choose版本?

col*_*ang 0 f#

我希望我能做到:

 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
                    }
Run Code Online (Sandbox Code Playgroud)

但现在我只能这样做

 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
Run Code Online (Sandbox Code Playgroud)

我的真实代码:

代码看起来不是那么好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   -> ()
        }
Run Code Online (Sandbox Code Playgroud)

Tom*_*cek 5

您的exam函数可以返回一个空的(而不是None)或者只包含一个元素(而不是Some)的yield!序列,并使用它将给定序列的所有元素添加到您当前生成的元素中.

在您的伪代码示例中,这将看起来像:

let myFun param1 param2 param3 = seq {
  if param1 then
    if param2 then
      yield param3 }

 let sequence = seq { 
   for x in xs do
     yield! myFun true true x 
     yield! myFun true true (x * 10) }
Run Code Online (Sandbox Code Playgroud)

假设xs = [1;2]上面的示例生成[1; 10; 2; 20](如果您设置了一些true参数false,它将跳过一些数字)