From: Brian H. <bri...@ql...> - 2003-05-22 22:52:34
|
On Thu, 22 May 2003, Alan Post wrote: > The perl map function allows the number of outgoing items to be > other than 1, for instance: > > my @l2 = map { if ( filter( $_ ) { $_ } else {()}} @l1; > > or: > > my %h = map { $_, f( $_ )} @l; > > I imagine it would be a bit tricky to implement an Enum function like > that ("merge_map"?), but it would generalize the partial map function > above. > > > A more general question: how many map/iterate functions will be a > part of the data structures, as opposed to having users stick to the > Enum versions? For instance, > > List.map f l > > vs > > List.of_enum (Enum.map f (List.enum l)) > > Will each datastructure have its own map function, to make things > convenient for users? > I considered implementing this as an enum. But here's the problem: count requires you to know in advance how many elements you have. The only way I can see doing this would be to create a list of pre-converted elements that didn't return None. A stream might be a better choice. let stream_partial_map f src = let rec next curr_ref x = match !curr_ref with [] -> None | h :: t -> curr_ref := t; match f h with None -> next curr_ref x | Some t -> Some t in Stream.from (next (ref src)) ;; But I hate having two different almost-identical-but-subtly-different ways of doing things. I think that instead of Enum module, we should consider an ExtStream module, with Enum functionality. Brian |