Creating a composite iterator in F #

I use a game similar to checkers, and I need a sequence that lists all the legal actions for this configuration.

I have the following function directly translated from C #:

seq {
    for y1 = 0 to BOARDSIZE-1 do
        for x1 = 0 to BOARDSIZE-1 do
             for dy = -2 to 2 do
                 for dx = -2 to 2 do
                     let x2 = x1 + dx;
                     let y2 = y1 + dy;
                     let currentMove = new MoveStruct(x1, y1, x2, y2);
                     if (currentMove.SomeCondition = true) then
                             yield currentMove;
   }

This works, but it’s inconvenient, and not exactly “F # way”, not to mention that I hide the suspicion that what I am doing here is not optimal in performance.

I would like to “smooth this out” into something that uses the combination of “iterate over all cells”, “iterate over all valid moves from this cell”.

And here are the functions that I hope to combine:

let AllCells =
    seq {
        for y=0 to BOARDSIZE-1 do
            for x=0 to BOARDSIZE-1 do
                yield (x,y);
    };

and

let LegalMovesAround(x1,y1) = 
    seq {
      if board.[x1, y1] = WHITE then
        for dy = -2 to 2 do
          for dx = -2 to 2 do
                let x2 = x1 + dx;
                let y2 = y1 + dy;
                let currentMove = new MoveStruct(x1, y1, x2, y2);
                if (currentMove.DetermineMoveType <> MoveType.ILLEGAL 
                    && board.[x2, y2] = NONE) then
                        yield currentMove;
     }

, . , , , , - , seq , , , MoveStruct.

- , AllCells LegalMovesAround (x, y)?

, Aleks

+5
3

, , , - :

 let validMoves = 
    AllCells 
    |> Seq.collect LegalMovesAround
    |> Seq.distinct

, .

EDIT: Tomas

+1

! :

let allLegalMoves = seq {
  for cell in AllCells do
    yield! LegalMovesAround cell
}
+3

yield!?

-

seq {
    for x,y in Allcells do
        yield! LMA(x,y)
}
+3

All Articles