Module Sliceoper


module Sliceoper: sig  end
Flexible subparts of arrays, lists, and strings

The Sliceoper and Slice modules help you grab parts of arrays, lists, and strings. For those familiar with Python, this mechanism is patterned after Python's indexing features. The mechanism is also generalizable to any other type that provides support for sub and length functions of the same manner as the Array, String, and List (plus Listutil from this library) modules.

It is recommended the modules that will be using slices should load the Sliceoper module with:

open Sliceoper;;

to make the operators conveniently available.


Slice Basics

A slice via this module contains two parts: a start position and an end position. Position 0 corresponds to the start of the list (or string, whatever). The end position is the index of the last item plus one. Therefore, a slice from 0 to 2 would return the first two elements: elements 0 and 1.

The end position can also be negative. In that case, it means to leave off the given number of items from the end of the list. For instance, if you had the string "abcdefg" and took the slice from 0 to -1, you would obtain "abcdef". If you took the slice from 1 to -3, you'd get "bcd".

You can also omit the end position to obtain the entire item starting from the given start position. There are special operators for this case; see below.

These features, taken together, make it easy to obtain certain internal parts of arrays, lists, or strings without requiring multiple calls to sub and manual length calculations.




Slice Composition Operator


val (|>) : (int -> 'a) -> int -> 'a
|> is the slice composition operator, and is designed to work solely in conjunction with one of the slice application operators below. That is, you can not just say let x = 1 |> 3;; and expect a valid result (see the Slice module if you wish to do this).

An example usage would be:

"abcdefg" |$ 1 |> 3;;
  string = "bc" 



Slice Application Operators

The slice application operators are used to apply a slice to a string, array, or list. They are designed to be used together with the slice composition operator documented above.

val (|$) : string -> int -> int -> string
String application operator. Example usage:

string |$ 1 |> -1;;

This strips the first and last characters off the string.

val (|@) : 'a list -> int -> int -> 'a list
List application operator. Example usage:

list |@ 1 |> -1;;

This strips the first and last elements from the list.

val (|&) : 'a array -> int -> int -> 'a array
Array application operator. Example usage:

array |& 1 |> -1;;

This strips the first and last elements from the array.



Remainder-Of-Items Application Operators

These operators are designed to take no ending position, instead taking only a starting position. Therefore, the |> operator should not be used separately.

val (|$>) : string -> int -> string
String operator. Example usage:

string |$> 1;;

This strips the first character from the string.

val (|@>) : 'a list -> int -> 'a list
List operator. Example usage:

list |@> 1;;

This strips the first character from the list.

val (|&>) : 'a array -> int -> 'a array
Array operator. Example usage:

array |&> 1;;

This strips the first character from the array.