Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Matej Košík" <mail@matej-kosik.net>
To: OCaml Mailing List <caml-list@inria.fr>
Subject: [Caml-list] stdlib → Queue → iter : implementation question / (possibly) bikeshedding
Date: Fri, 22 Dec 2017 12:05:31 +0100	[thread overview]
Message-ID: <64f1e880-fb8c-4432-3597-3485ec06c046@matej-kosik.net> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 1403 bytes --]

Hi,

I just had a look at Queue.iter function as it is implemented by Ocaml standard library.

The original version

  (* https://github.com/ocaml/ocaml/blob/c08532f561548f6164278ba0d156da841aefe44a/stdlib/queue.ml#L100-L108 *)

  let iter =
    let rec iter f cell =
      match cell with
      | Nil -> ()
      | Cons { content; next } ->
        f content;
        iter f next
    in
  fun f q -> iter f q.first

I am wondering about two things.

--------------------------------------------------------------------------------

(1)

Why is the original implementation preferred over

  let iter f =
    let rec iter cell =
      match cell with
      | Nil -> ()
      | Cons { content; next } ->
        f content;
        iter next
    in
    fun q -> iter q.first

where "f" would be bound once and then reused as many times as necessary
(instead of passed over and over again within the inner "iter" loop).

--------------------------------------------------------------------------------

(2)

Why is the original implementation preferred over the following version

  let iter f q =
    let rec iter cell =
      match cell with
      | Nil -> ()
      | Cons { content; next } ->
        f content;
        iter next
    in
    iter q.first

?

Similar decisions where made elsewhere in stdlib so I am wondering what am I missing?


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

             reply	other threads:[~2017-12-22 11:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-22 11:05 Matej Košík [this message]
2017-12-22 11:55 ` Nicolás Ojeda Bär

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=64f1e880-fb8c-4432-3597-3485ec06c046@matej-kosik.net \
    --to=mail@matej-kosik.net \
    --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