Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* [Caml-list] stdlib → Queue → iter : implementation question / (possibly) bikeshedding
@ 2017-12-22 11:05 Matej Košík
  2017-12-22 11:55 ` Nicolás Ojeda Bär
  0 siblings, 1 reply; 2+ messages in thread
From: Matej Košík @ 2017-12-22 11:05 UTC (permalink / raw)
  To: OCaml Mailing List


[-- 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 --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-12-22 11:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-22 11:05 [Caml-list] stdlib → Queue → iter : implementation question / (possibly) bikeshedding Matej Košík
2017-12-22 11:55 ` Nicolás Ojeda Bär

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox