From: Richard J. <ri...@an...> - 2008-04-22 10:45:09
|
I'd like to propose the following function for inclusion in ExtList: (** [find_map pred list] finds the first element of [list] for which [pred element] returns [Some r]. It returns [r] immediately once found. @raise Not_found if no element matches the predicate. See also {!filter_map}. *) val find_map : ('a -> 'b option) -> 'a list -> 'b let rec find_map f = function | [] -> raise Not_found | x :: xs -> match f x with | Some y -> y | None -> find_map f xs The HOF is analogous to filter_map, but only returns the first element found, rather than all matching elements. I find this HOF useful when iterating over XML [1]. For example if you have XML like this: <guest> <os_type>hvm</os_type> <arch name='ppc'/> </guest> and you want to find the <os_type> content, then assuming 'guest' is a variable containing the child nodes of <guest>: let os_type = try find_map ( function | Element ("os_type", _, [PCData "hvm"]) -> Some VT_HVM | Element ("os_type", _, [PCData "xen"]) -> Some VT_Xen | _ -> None ) guest with Not_found -> VT_Unknown in If nobody objects I would like to add this to extlib. Rich. [1] I know it'd be better to do this with CDuce, but that would require there to be some sort of schema to the XML which doesn't change with the phase of the moon. -- Richard Jones Red Hat |
From: Janne H. <jjh...@gm...> - 2008-04-22 19:18:15
|
Looks good, I would find this useful. Janne On Tue, Apr 22, 2008 at 3:45 AM, Richard Jones <ri...@an...> wrote: > I'd like to propose the following function for inclusion in ExtList: > > (** > [find_map pred list] finds the first element of [list] for which > [pred element] returns [Some r]. It returns [r] immediately > once found. > > @raise Not_found if no element matches the predicate. > > See also {!filter_map}. > *) > > val find_map : ('a -> 'b option) -> 'a list -> 'b > > let rec find_map f = function > | [] -> raise Not_found > | x :: xs -> > match f x with > | Some y -> y > | None -> find_map f xs > > The HOF is analogous to filter_map, but only returns the first element > found, rather than all matching elements. > > I find this HOF useful when iterating over XML [1]. For example if > you have XML like this: > > <guest> > <os_type>hvm</os_type> > <arch name='ppc'/> > </guest> > > and you want to find the <os_type> content, then assuming 'guest' is a > variable containing the child nodes of <guest>: > > let os_type = > try > find_map ( > function > | Element ("os_type", _, [PCData "hvm"]) -> Some VT_HVM > | Element ("os_type", _, [PCData "xen"]) -> Some VT_Xen > | _ -> None > ) guest > with > Not_found -> VT_Unknown in > > If nobody objects I would like to add this to extlib. > > Rich. > > [1] I know it'd be better to do this with CDuce, but that would > require there to be some sort of schema to the XML which doesn't > change with the phase of the moon. > > -- > Richard Jones > Red Hat > > ------------------------------------------------------------------------- > This SF.net email is sponsored by the 2008 JavaOne(SM) Conference > Don't miss this year's exciting event. There's still time to save $100. > Use priority code J8TL2D2. > > http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone > _______________________________________________ > ocaml-lib-devel mailing list > oca...@li... > https://lists.sourceforge.net/lists/listinfo/ocaml-lib-devel > |
From: Richard J. <ri...@an...> - 2008-04-23 12:05:21
|
On Tue, Apr 22, 2008 at 12:18:15PM -0700, Janne Hellsten wrote: > Looks good, I would find this useful. OK, I've committed find_map: http://code.google.com/p/ocaml-extlib/source/detail?r=380 Rich. -- Richard Jones Red Hat |
From: Janne H. <jjh...@gm...> - 2008-04-23 17:50:30
|
Hi Rich, Would you also be so kind as to add a simple unit test for your new function? Although your addition is fairly straightforward, it's good to test it in order to protect it in the future if someone wishes to change it. Janne On Wed, Apr 23, 2008 at 5:05 AM, Richard Jones <ri...@an...> wrote: > On Tue, Apr 22, 2008 at 12:18:15PM -0700, Janne Hellsten wrote: > > Looks good, I would find this useful. > > OK, I've committed find_map: > http://code.google.com/p/ocaml-extlib/source/detail?r=380 > > Rich. > > -- > Richard Jones > Red Hat > |
From: Richard J. <ri...@an...> - 2008-04-23 18:09:52
|
On Wed, Apr 23, 2008 at 10:50:17AM -0700, Janne Hellsten wrote: > Hi Rich, > > Would you also be so kind as to add a simple unit test for your new > function? Although your addition is fairly straightforward, it's good > to test it in order to protect it in the future if someone wishes to > change it. Done (rev 381). Rich. -- Richard Jones Red Hat |
From: Janne H. <jjh...@gm...> - 2008-04-23 18:19:39
|
That was quick, thanks! Janne On Wed, Apr 23, 2008 at 11:09 AM, Richard Jones <ri...@an...> wrote: > On Wed, Apr 23, 2008 at 10:50:17AM -0700, Janne Hellsten wrote: > > Hi Rich, > > > > Would you also be so kind as to add a simple unit test for your new > > function? Although your addition is fairly straightforward, it's good > > to test it in order to protect it in the future if someone wishes to > > change it. > > Done (rev 381). > > > > Rich. > > -- > Richard Jones > Red Hat > |