Skip to content

Instantly share code, notes, and snippets.

@rizo
Created June 15, 2015 18:26
Show Gist options
  • Select an option

  • Save rizo/5b6e8dcf6fcaab83863c to your computer and use it in GitHub Desktop.

Select an option

Save rizo/5b6e8dcf6fcaab83863c to your computer and use it in GitHub Desktop.
type point = { x : int; y : int };;
type 'a cell = { coord : point; level : int; cargo : 'a } ;;
List.Assoc.find;;
let assoc = [(2, "a"); (3, "b"); (5, "d")];;
List.Assoc.find assoc 3;;
\ntype point = { x : int; y : int }\nlet point ~x ~y = { x ; y }\n;;
point ~x:2 ~y: 12;;
let point_of_pair (x, y) = point ~x ~y;;
let points = List.map [(1,2); (3, 1); (5, 1); (10; 23)] ~f:point_of_pair;;
let points = List.map [(1,2); (3, 1); (5, 1); (10, 23)] ~f:point_of_pair;;
type 'a cell = { coord : point; level : int; cargo : 'a }\n let cell p l x = { coord = p; level = l; cargo = x };;
_ let cells = List.map points ~f:(fun p -> cell p 1 Random.int)
Random.int;;
Random.int 5;;
let cells = List.map points ~f:(fun p -> cell p 1 @@ Random.int 10);;
List.Assoc.find cells {coord = {x = 1; y = 2}; level = 1; cargo = 2};;
type cell = {x : int; y : int; level : int };;
points;;
let grid = List.map points ~f:(fun p -> { x = p.x; y = p.y; l = 1 });;
let grid = List.map points ~f:(fun p -> { x = p.x; y = p.y; level = 1 });;
let grid' = List.map grid ~f:(fun c -> (c, Random.int 10));;
let grid' = ({x = 21; y = 32; level = 0}, 40)::grid';;
List.Assoc.add;;
List.Assoc.add grid' {x = 1; y = 2; level = 1} ((List.Assoc.find grid' {x = 1; y = 2; level = 1}) + 1);;
let increment_grid_cell g cell =\nlet x = match List.Assoc.find g cell with\n| Some old -> old + 1\n| None -> 1 in\nList.Assoc.add g cell x;;
grdi';;
grid';;
increment_grid_cell grid' {x = 1; y = 2; level = 1};;
grid';;
increment_grid_cell grid' {x = 21; y = 32; level = 0};;
let grid' = increment_grid_cell grid' {x = 21; y = 32; level = 0};;
let grid = List.fold positions ~init:[] ~f: begin fun p ->\nlet cell = closest_grid_cell p in\nincrement_grid_cell grid cell;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment