caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
To: caml-list@inria.fr
Subject: Functional pattern for converting database results to structures
Date: Wed, 22 Dec 2004 13:09:33 +0000	[thread overview]
Message-ID: <20041222130933.GA16609@annexia.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 3146 bytes --]

[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 = { campaign_name : string; campaign_status : string;
                  adgroups : adgroup list }
and adgroup = { adgroup_name : string; adgroup_status : string;
                keywords : keyword list;
                adverts : advert list }
and keyword = { keyword : string }
and advert = { 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 =
[(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.

-- 
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/

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

                 reply	other threads:[~2004-12-22 13:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20041222130933.GA16609@annexia.org \
    --to=rich@annexia.org \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).