Skip to content

Instantly share code, notes, and snippets.

@einarwh
Created December 6, 2024 08:02
Show Gist options
  • Save einarwh/d347caff2221be6ca0409ae3a6ebe163 to your computer and use it in GitHub Desktop.
Save einarwh/d347caff2221be6ca0409ae3a6ebe163 to your computer and use it in GitHub Desktop.
Array2D module.
module Array2D =
let tryGet (a : 'a[,]) (x, y) =
let first = y >= 0 && y < a.GetLength(0)
let second = x >= 0 && x < a.GetLength(1)
if first && second then Some (Array2D.get a y x) else None
let positions (a : 'a[,]) =
let rowCount = a.GetLength(0)
let colCount = a.GetLength(1)
[for x in [0..colCount-1] do for y in [0..rowCount-1] -> (x, y)]
let fromList (lst : 'a list list) =
let width = lst |> List.head |> List.length
let height = lst |> List.length
Array2D.init height width (fun y x -> lst |> List.item y |> List.item x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment