Up a Free

Eric’s latest post has some more code to chew upon. Here is where we find the first slight deviations in syntax and used modules.

Generic types

the generic IntMap is defined as such…

type IntMap<'t> = Map<int,'t>

It looks like F# deviated from the unusual type argument placement in OCaml.

The bucket

for the original_bytes value I actually chose a byte array

In read_bytes it seemed more idiomatic to me to use Map.tryFind which returns an option value on which you can match whether something was found or not:

match Map.tryFind addr bytes.edits with
| (Some value) -> value
| None -> bytes.originalBytes.[addr]

The rest

All other bits stay more or less the same.

As a side note, the byte array that probably uses the .NET type for Array in the background already has an int index. I assume that such a structure is not available in OCaml. In some functional languages finding the element at index x is an O(n) operation in a typical list-like structure, that may also be the case in OCaml.

The code for this post is at this tag.