* [Caml-list] AtomMap: build a list of all the elements in a map
@ 2011-02-21 12:40 Tie Cheng
2011-02-21 12:44 ` David Allsopp
2011-02-21 13:05 ` Raphael Proust
0 siblings, 2 replies; 3+ messages in thread
From: Tie Cheng @ 2011-02-21 12:40 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 651 bytes --]
Hello all,
I have posted a question on Caml-beginners, but have not got response so
far... guess maybe you could here...
I have defined "type AT = A AtomMap.t". Do you know how to build a function
"f: AT -> (atom * A) list" which returns a list of all the elements in a
map?
I am trying to combine AtomMap.fold, AtomMap.Iter (in this manual
http://cristal.inria.fr/~fpottier/alphaCaml/alphaCaml.pdf) to do so...
But it seems that unlike List.folder_left, AtomMap.fold does not accept list
as argument: val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b, there
it is not convenient to build a list progressively here...
Thanks and regards
Tie
[-- Attachment #2: Type: text/html, Size: 975 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* RE: [Caml-list] AtomMap: build a list of all the elements in a map
2011-02-21 12:40 [Caml-list] AtomMap: build a list of all the elements in a map Tie Cheng
@ 2011-02-21 12:44 ` David Allsopp
2011-02-21 13:05 ` Raphael Proust
1 sibling, 0 replies; 3+ messages in thread
From: David Allsopp @ 2011-02-21 12:44 UTC (permalink / raw)
To: 'Tie Cheng', 'caml-list@yquem.inria.fr'
Tie Cheng wrote:
> I have posted a question on Caml-beginners, but have not got
> response so far... guess maybe you could here... I have defined
> "type AT = A AtomMap.t". Do you know how to build a function
> "f: AT -> (atom * A) list" which returns a list of all the
> elements in a map?
> I am trying to combine AtomMap.fold, AtomMap.Iter
> (in this manual http://cristal.inria.fr/~fpottier/alphaCaml/alphaCaml.pdf)
> to do so... But it seems that unlike List.folder_left, AtomMap.fold
> does not accept list as argument: val fold : (key -> 'a -> 'b -> 'b)
> -> 'a t -> 'b -> 'b, there it is not convenient to build a list
> progressively here...
I don't see how that function signature differs from what you expect so sorry if that's because I don't know anything about alphaCaml!
What's wrong with:
fold (fun key value acc -> (key, value)::acc)) atomMap []
The signature for [fold] is simply like the signature for List.fold_right rather than List.fold_left.
David
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] AtomMap: build a list of all the elements in a map
2011-02-21 12:40 [Caml-list] AtomMap: build a list of all the elements in a map Tie Cheng
2011-02-21 12:44 ` David Allsopp
@ 2011-02-21 13:05 ` Raphael Proust
1 sibling, 0 replies; 3+ messages in thread
From: Raphael Proust @ 2011-02-21 13:05 UTC (permalink / raw)
To: Tie Cheng; +Cc: caml-list
On Mon, Feb 21, 2011 at 1:40 PM, Tie Cheng <chengtie@gmail.com> wrote:
> Hello all,
>
> I have posted a question on Caml-beginners, but have not got response so
> far... guess maybe you could here...
>
> I have defined "type AT = A AtomMap.t". Do you know how to build a function
> "f: AT -> (atom * A) list" which returns a list of all the elements in a
> map?
It seems that OCaml 3.12 introduced the function [bindings] which has
the type and semantic you're looking for.
>
> I am trying to combine AtomMap.fold, AtomMap.Iter (in this
> manual http://cristal.inria.fr/~fpottier/alphaCaml/alphaCaml.pdf) to do
> so... But it seems that unlike List.folder_left, AtomMap.fold does not
> accept list as argument: val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b ->
> 'b, there it is not convenient to build a list progressively here...
You should be able to combine fold and iter without the list conversion step:
let fold_iter iterator folder map init =
AtomMap.fold (fun k e accu -> iterator k e; folder k e accu) map init
(I haven't actually tried the code, but something similar should do the trick)
--
_______
Raphael
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2011-02-21 13:05 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-21 12:40 [Caml-list] AtomMap: build a list of all the elements in a map Tie Cheng
2011-02-21 12:44 ` David Allsopp
2011-02-21 13:05 ` Raphael Proust
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox