From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 6DA9BBB81 for ; Wed, 22 Dec 2004 14:09:32 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id iBMD9WCO025174 for ; Wed, 22 Dec 2004 14:09:32 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id OAA28312 for ; Wed, 22 Dec 2004 14:09:32 +0100 (MET) Received: from annexia.force9.co.uk (annexia.force9.co.uk [212.56.101.183]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id iBMD9VMZ025170 for ; Wed, 22 Dec 2004 14:09:31 +0100 Received: from rich by annexia.force9.co.uk with local (Exim 3.36 #1 (Debian)) id 1Ch6FB-0004co-00 for ; Wed, 22 Dec 2004 13:09:33 +0000 Date: Wed, 22 Dec 2004 13:09:33 +0000 To: caml-list@inria.fr Subject: Functional pattern for converting database results to structures Message-ID: <20041222130933.GA16609@annexia.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="9jxsPFA5p3P2qPhR" Content-Disposition: inline User-Agent: Mutt/1.5.6+20040722i From: Richard Jones X-Miltered: at concorde with ID 41C9720C.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 41C9720B.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; ocaml:01 trotts:01 ocaml:01 beginners:01 val:01 foo:01 baz:01 foo:01 baz:01 nesting:01 sandbox:01 campaign:98 advert:98 campaign:98 advert:98 X-Attachments: type="application/pgp-signature" name="signature.asc" X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.0 X-Spam-Level: --9jxsPFA5p3P2qPhR Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable [This isn't really a question about OCaml, or ML, but functional programming in general.] I often come across the same problem. I have to read a table of data (often the result of a SQL query, or a CSV file), and create a hierarchical structure from it. For example, my current problem is a table of data which looks like this: Campaign Adgroup Keyword Advert Campaign_status Adgroup_status ----------------------------------------------------------------------- Summer04 Domain KW1 AD1 Finished Finished Summer04 Domain KW2 AD1 Finished Finished Summer04 Domain KW3 AD1 Finished Finished Summer04 Domain KW4 AD1 Finished Finished Summer04 Domain KW5 AD2 Finished Finished Summer04 Domain KW6 AD2 Finished Finished Summer04 Offer1 KW7 AD3 Finished Finished Summer04 Offer2 KW8 AD3 Finished Finished Summer04 Offer3 KW9 AD3 Finished Finished Summer04 Offer4 KW10 AD3 Finished Finished Winter04 Domain KW1 AD1 Active Active Winter04 Domain KW2 AD1 Active Active Winter04 Domain KW3 AD1 Active Active Winter04 Domain KW4 AD1 Active Active Winter04 Domain KW5 AD2 Active Active Winter04 Domain KW6 AD2 Active Active Winter04 Offer5 KW7 AD3 Active Active Winter04 Offer5 KW8 AD3 Active Active Winter04 Offer5 KW9 AD3 Active Active Winter04 Offer6 KW10 AD4 Active Active and I'd like to come out with a structure like this: type campaign =3D { campaign_name : string; campaign_status : string; adgroups : adgroup list } and adgroup =3D { adgroup_name : string; adgroup_status : string; keywords : keyword list; adverts : advert list } and keyword =3D { keyword : string } and advert =3D { advert : string } I've previously been using a "group_by" function kindly written by Isaac Trotts (see thread here: http://groups.yahoo.com/group/ocaml_beginners/message/1556 ). val group_by : ('a * 'b) list -> ('a * 'b list) list group_by [1, "foo"; 2, "bar"; 2, "baz"; 2, "biz"; 3, "boo"; 4, "fizz"];; - : (int * string list) list =3D [(1, ["foo"]); (2, ["bar"; "baz"; "biz"]); (3, ["boo"]); (4, ["fizz"])] But it's still complicated to use, particularly when we have several levels of nesting, and, as in this case, the levels aren't even cleanly nested (adgroups have both keywords and adverts). Help! What's a good pattern for solving this problem? Rich. --=20 Richard Jones. http://www.annexia.org/ http://www.j-london.com/ >>> http://www.team-notepad.com/ - collaboration tools for teams <<< Merjis Ltd. http://www.merjis.com/ - improving website return on investment Advanced wiki and collab tool - http://sandbox.merjis.com/ --9jxsPFA5p3P2qPhR Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) iD8DBQFByXIN4le1M6q9pzoRAq9SAKCb0ktMSQnWU0CxstkDUmyTpoog1wCgyt/7 ShVkACM/IzmI3P8grJM4ppw= =KEDO -----END PGP SIGNATURE----- --9jxsPFA5p3P2qPhR--