Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Adam P. Jenkins" <ajenkins@cs.umass.edu>
To: caml-list@inria.fr
Subject: mapi, iteri, fold_lefti, fold_righti
Date: Wed, 16 Jul 1997 15:27:40 -0400	[thread overview]
Message-ID: <199707161927.PAA00882@dwarin.oit.umass.edu> (raw)

Hi,
	I find a lot of times that Array.map and Array.iter aren't
adequate when I want to iterate through an array, since I need to use
the corresponding indexes as well.  While it's trivial to do what I
need with a for loop, I like having a function.  I've therefore
written my own mapi and iteri, like SML's mapi and appi.  For those
not familiar with SML, iteri is like Array.iter except f is called
with the index of the element as well as the element.

Also, I think Array should have fold functions too, so I wrote
fold_left, fold_right, fold_lefti, and fold_righti for arrays.  

In my opinion these belong in the Array module for O'Caml, but maybe
others don't find they use these as much as me.  Here they are.

Cheers,
Adam

-- 
Adam P. Jenkins 
mailto:ajenkins@cs.umass.edu

module MyArray :
    sig
 (* mapi and iteri are like map and iter except f is called with the element's
    index and the element as arguments. *)
      val iteri : (int -> 'a -> 'b) -> 'a array -> unit
      val mapi : (int -> 'a -> 'b) -> 'a array -> 'b array

 (* fold_left and fold_right for arrays, just like the list versions *)
      val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b array -> 'a
      val fold_right : ('a -> 'b -> 'b) -> 'a array -> 'b -> 'b
	  
 (* the same as fold_left and fold_right, except f gets the element's index as
    argument too. *)
      val fold_lefti : (int -> 'a -> 'b -> 'a) -> 'a -> 'b array -> 'a
      val fold_righti : (int -> 'a -> 'b -> 'b) -> 'a array -> 'b -> 'b    
    end =
  struct
    let iteri f a =
      for i = 0 to (Array.length a) - 1 do
     	f i a.(i)
      done

    let mapi f a =
      let len = Array.length a in
      if len = 0 then [||]
      else begin
     	let newa = Array.create len a.(0) in
     	for i = 0 to len - 1 do
	  newa.(i) <- f i a.(i)
     	done;
     	newa
      end

    let fold_left f init a =
      let len = Array.length a in
      if len = 0 then init
      else begin
     	let accum = ref init in
     	for i = 0 to len - 1 do
	  accum := f !accum a.(i)
     	done;
     	!accum
      end
	  
    let fold_right f a init =
      let len = Array.length a in
      if len = 0 then init
      else begin
     	let accum = ref init in
     	for i = len - 1 downto 0 do
	  accum := f a.(i) !accum
     	done;
     	!accum
      end

    let fold_lefti f init a =
      let len = Array.length a in
      if len = 0 then init
      else begin
     	let accum = ref init in
     	for i = 0 to len - 1 do
	  accum := f i !accum a.(i)
     	done;
     	!accum
      end

    let fold_righti f a init =
      let len = Array.length a in
      if len = 0 then init
      else begin
     	let accum = ref init in
     	for i = len - 1 downto 0 do
	  accum := f i a.(i) !accum
     	done;
     	!accum
      end
  end
	  





                 reply	other threads:[~1997-07-17  8:45 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=199707161927.PAA00882@dwarin.oit.umass.edu \
    --to=ajenkins@cs.umass.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