"Slice, I say. Pauca, pauca. Slice! That's my humour."
-- Shakespeare, The Merry Wives of Windsor
Act I, Scene I, Line 125
I've been working my way through Dave Fancher's excellent Book of F# and found an interesting example of indexing and slicing arrays in F#. Since programming is a full contact sport, here is my take on indexing and slicing in F#.
We have a constructor for a type called Word which takes a string and separates it into words.
type Words(sentence string)
We implement an index with the Item member.
This allows us to access the words of the string given.
We also implement an array slice with the GetSlice member.
( : , : )
This allows us to slice the array (substring it) of words in the string given.
We can go one better than built in slice by allow for range in the slice to be bigger than the number of words.
In order to allow for this we have to check to see if the end is greater than the number of words.
e words.Length words.s words.se
e words.Length words words.e
I hope I have shown that F# has some very nice support for objects.