Guest User

Untitled

a guest
Jan 15th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.06 KB | None | 0 0
  1. Code:
  2.  
  3. let passable gstate (row, col) =
  4.    let t = tile_of_int (gstate.tmap.(row).(col).content) in
  5.       not ((t = `Water) || (t = `Food))
  6. ;;
  7.  
  8. Some might prefer:
  9.  
  10. let passable gstate (row, col) =
  11.    let landmap = gstate.tmap in
  12.    let lm_row = landmap.(row) in
  13.    let tile = lm_row.(col) in
  14.    let v = tile_of_int tile.content in
  15.       not ((v = `Water) || (v = `Food))
  16. ;;
  17.  
  18. In this case I don't see any benefit in expanding the value-accessing line.
  19.  
  20. Consider this, though:
  21.  
  22. let visible_and_passble gstate (row, col) =
  23.    let tile = gstate.tmap.(row).(col) in
  24.    let terrain = tile_of_int tile.content in
  25.    let visible = (tile.seen > 0) in
  26.       visible && (not ((terrain = `Water) || (terrain = `Food)))
  27. ;;
  28.  
  29. Here we refer to 'tile' twice after its initial declaration, so the above could be considered better than:
  30.  
  31. let visible_and_passble gstate (row, col) =
  32.    let terrain = tile_of_int gstate.tmap.(row).(col).content in
  33.    let visible = (gstate.tmap.(row).(col).seen > 0) in
  34.       visible && (not ((terrain = `Water) || (terrain = `Food)))
  35. ;;
Add Comment
Please, Sign In to add comment