My original code is:
let (upPosition, downPosition, leftPosition, rightPosition) = match myMove.MyDirection with | Direction.Up -> (GoUp y, y, GoLeft x, GoRight x) | Direction.Down-> (y, GoDown y, GoLeft x, GoRight x) | Direction.Left -> (GoUp y, GoDown y, GoLeft x, x) | Direction.Right -> (GoUp y, GoDown y, x, GoRight x) | _ -> (y, y, x, x)
Basically, there is an object on the 2d board.
myMove.MyDirection dictates its next move , and
x,y are its target position.
Once the object gets to
x,y, it would cause some impacts on its front, left, right neighbours from its local rotational space.
GoUp, ... , GoRight determines how far those neighbours could be.
So, ultimately, the code returns a "+" (cross) shape centred at
x,y, so the minimum variable to descibe this shape is
(x,y,upPosition, downPosition, leftPosition, rightPosition)
The best solution i can think of to the prevous code is the following. However I feel there is more can be done to make it better. Is there any
map function for
tuple and is it costly?
let Reverse = function | Direction.Up -> Direction.Down | Direction.Down -> Direction.Up | Direction.Left -> Direction.Right | Direction.Right -> Direction.Left | _ -> raise (ArgumentException("Not Vaild Move!")) let Go myDirection filter pos = match myDirection, filter with | a,b when a=b -> pos | Direction.Up , _ -> GoUp pos | Direction.Down , _ -> GoDown pos | Direction.Left , _ -> GoLeft pos | Direction.Right , _ -> GoRight pos | _ -> pos let (upPosition, downPosition, leftPosition, rightPosition) = let rev = Reverse myMove.MyDirection (Go Direction.Up rev y, Go Direction.Down rev y, Go Direction.Left rev x, Go Direction.Right rev x)