English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
optimization of sequence of List.map and inlining
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-06-10 (22:10)
From: Peng Zang <peng.zang@g...>
Subject: Re: [Caml-list] optimization of sequence of List.map and inlining
Hash: SHA1

ExtLib has an Enum module.  It has lazy maps among other things.  This means I 
can perform multiple maps and it will not create intermediate data 
structures.  Example (++ is function composition and $ is function 

  Enum.fold (+) 0
  ++ Enum.map (( * ) 2)
  ++ Enum.map ((+) 1)
  $ List.enum [1;2;3;4;5]

Will add one to everything in the list, double it and then sum the list, which 
yields 40.


On Tuesday 10 June 2008 03:01:12 pm Charles Hymans wrote:
> Let's say, I have the following code:
>   let f l = List.map succ l
>   ....
>   let l = f l in
>   let l = List.map succ l in
>     do_something_with l
> Is there a way to tell the compiler to optimize it so that it runs as fast
> as this code:
>   let l = List.map (fun x -> succ (succ x)) l in
>     l
> In the first case, there are two passes where succ is applied to each
> elements of the list.
> In the second case, there is only one pass that applies succ twice to each
> element of the list.
> Thank you,
Version: GnuPG v2.0.7 (GNU/Linux)