Diving into Batteries #2 : Heavyweight strings

Today, we’ll give a whirl at the Rope module of OCaml Batteries Included.

What are ropes ?

Ropes are better strings (but heavyweight) :

    • The length of a rope can be determined in constant time
      appending or prepending a small rope to an arbitrarily large one in amortized constant time
      concat, substring, insert, remove operations in amortized logarithmic time
      access to and modification of ropes in logarithmic time
  • Moreover, functional purity is preserved, we do never modify the original rope when appending, inserting, removing, etc.
    There’ll just be as much as possible data shared between the resulting string and the original one.

    However, world is not pink-made. I quote the documentation :

    However, Rope is an amortized data structure, and its use in a persistent setting can easily degrade its amortized time bounds. It is thus mainly intended to be used ephemerally. In some cases, it is possible to use Rope persistently with the same amortized bounds by explicitly rebalancing ropes to be reused using balance. Special care must be taken to avoid calling balance too frequently; in the limit, calling balance after each modification would defeat the purpose of amortization.

    First, you have to know that a syntax extension is included in Batteries, such that you can create literal ropes instead of strings just by prefixing the litteral string with ‘r’. Let’s now see how we can create a rope containing the “OCaml Batteries Included” string.

    1
    
    let my_first_rope = r"OCaml Batteries Included" ;;

    The type of ‘my_first_rope’ is Batteries.Rope.t, that’s to say a rope.

    Of course, there are many conversions possible from and to ropes. from/to_string, from/to_ustring, of_char, of_uchar, of_latin1, and also many interactions possible with the Enum module. You can also lowercase and uppercase a rope, make a rope of ‘n’ times a given character ‘c’.

    As you may expect, there are many string operations included in this module. You can explode a string into a character list, you can split a rope using a given separator, get the character at a given position, remove characters, insert characters, concatenate ropes,

    OCaml programming, and more generally functional programming, is empowered by higher order functions. The Rope module uses it, providing us higher order functions like iter[...], range_iter[...], fold, map, filter, filter_map, etc. This way, we can express shortly in our code very complex operations with these higher order functions, which let us work on strings with power, speed and elegance.

    There are many other functions that fulfill (personally) all the needs I have from a string manipulation module.

    To conclude, there are many “bulk” variants of functions that let us work more efficiently for large chunks of string data.

    I think it is useless to give code using Batteries’ ropes, you should rather go to the corresponding documentation page and try it out !


    Leave a Comment