From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] optimization of sequence of List.map and inlining
Date: Tue, 17 Jun 2008 15:36:31 +0100 [thread overview]
Message-ID: <200806171536.31220.jon@ffconsultancy.com> (raw)
In-Reply-To: <676aba050806101201x526f03b1lf1fdbed665ee2e3f@mail.gmail.com>
On Tuesday 10 June 2008 20:01:12 Charles Hymans wrote:
> Thank you for your answer.
>
>> In MLs, you deforest by hand. You might like to use a function
>> composition
>
> Unfortunately, in the case from which my example is extracted, I
> believe I can't do that:
> The several List.maps, even though they are executed in sequence, are
> not in the same module. In order to deforest by hand, I have to break
> the module interface, do some function inlining and then do the
> function composition.
> Something I don't want to do, because it would make the code difficult
> to read and to maintain.
Perhaps you could use something like the following abstract lazy sequence
implementation:
module Seq : sig
type ('a, 'b) t
val of_list : 'a list -> ('a, 'a) t
val map : ('a -> 'b) -> ('c, 'a) t -> ('c, 'b) t
val to_list : ('a, 'b) t -> 'b list
end = struct
type ('a, 'b) t = ('a -> 'b) * 'a list
let of_list list = (fun x -> x), list
let map f (g, list) = (fun x -> f(g x)), list
let to_list (f, list) = List.map f list
end
Where your existing implementation exposes an 'a list you now expose an
('b, 'a) Seq.t instead.
# let a = Seq.of_list [1];;
val a : (int, int) Seq.t = <abstr>
# let b = Seq.map float a;;
val b : (int, float) Seq.t = <abstr>
# let c = Seq.map string_of_float b;;
val c : (int, string) Seq.t = <abstr>
# Seq.to_list c;;
- : string list = ["1."]
> That's why I wondered if there was a way to tell the compiler to do it
> for me. Or maybe some incantation with ocamlp4?
I would not recommend pursuing that line of thought under any circumstances.
Even if this problem is a critical showstopper for you, I would always
recommend uglier code over camlp4 hacks.
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e
prev parent reply other threads:[~2008-06-17 14:40 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-06-10 19:01 Charles Hymans
2008-06-10 19:21 ` [Caml-list] " Jon Harrop
2008-06-10 20:55 ` Richard Jones
2008-06-10 22:10 ` Peng Zang
2008-06-10 23:07 ` Brian Hurt
2008-06-17 14:36 ` Jon Harrop [this message]
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=200806171536.31220.jon@ffconsultancy.com \
--to=jon@ffconsultancy.com \
--cc=caml-list@yquem.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