From: "Yaron M. Minsky" <yminsky@CS.Cornell.EDU>
To: "Caml List " <caml-list@inria.fr>
Subject: [Caml-list] python-style array access
Date: Wed, 4 Dec 2002 13:27:05 -0500 (EST) [thread overview]
Message-ID: <32290.141.155.88.179.1039026425.squirrel@minsky-primus.homeip.net> (raw)
One of the things I miss from Python is the small syntactical touches that
simplify common operations. one of my favorites is the way python does
array indexing. You can use negative numbers to count from the end of the
array, and you can easily take subranges of arrays or strings. It's
actually quite simple to get the same functionality in ocaml, and here's
some simple code for doing it. It adds a bit of overhead (an extra
function call), but where that small overhead isn't an issue, I think this
is a nice solution. If you include the following code in the beginning of
your module, you'll be able to do the following kind of tricks:
# "foobar".[-1];;
- : char = 'r'
# "foobar".[-2];;
- : char = 'a'
# String.rsub "foobar" 0 (-1);;
- : string = "fooba"
# String.rsub "foobar" 1 0;;
- : string = "oobar"
# String.rsub "foobar" 1 3;;
- : string = "oo"
# String.rsub "foobar" 1 (-1);;
- : string = "ooba"
The same kind of things can be done with arrays. rsub stans for "range
sub", by the way. Note that a 0 in the second position of rsub means the
very last as opposed to the very first index position.
------ Snip Here -------
open StdLabels
open MoreLabels
module Array =
struct
include Array
let normalize ar i = if i < 0 then length ar + i else i
let get ar i = get ar (normalize ar i)
let rsub ar start stop =
let stop = if stop = 0 then length ar else stop in
let pos = normalize ar start in
let len = (normalize ar stop) - pos in
sub ar ~pos ~len
end
module String =
struct
include String
let normalize str i = if i < 0 then length str + i else i
let get str i = get str (normalize str i)
let rsub str start stop =
let stop = if stop = 0 then length str else stop in
let pos = normalize str start in
let len = (normalize str stop) - pos in
sub str ~pos ~len
end
--
|--------/ Yaron M. Minsky \--------|
|--------\ http://www.cs.cornell.edu/home/yminsky/ /--------|
Open PGP --- KeyID B1FFD916 (new key as of Dec 4th)
Fingerprint: 5BF6 83E1 0CE3 1043 95D8 F8D5 9F12 B3A9 B1FF D916
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
reply other threads:[~2002-12-04 18:27 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=32290.141.155.88.179.1039026425.squirrel@minsky-primus.homeip.net \
--to=yminsky@cs.cornell.edu \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox