caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: Denis Bueno <dbueno@gmail.com>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Type error
Date: Sun, 15 Oct 2006 13:48:00 +1000	[thread overview]
Message-ID: <1160884080.28140.80.camel@rosella.wigram> (raw)
In-Reply-To: <6dbd4d000610142029m92d7005v65e95f031e7eae9b@mail.gmail.com>

On Sat, 2006-10-14 at 23:29 -0400, Denis Bueno wrote:
> I'm writing a simple (stupid) de-functorised Set library. Its skeleton is:

> 
> One of the things I'd like to do is write an of_array to create an
> array from a set. However, it is often the case that I have an array
> of objects from which I'd like to pull out one field & make a set of
> those fields. So, my of_array looks like this:


> I understand why this shouldn't be compilable, but, is it possible to
> do something similarly elegant (without creating a separate function)?

Well yes, you can do something vastly more comprehensive,
and at the same time save yourself months of implementation
work .. by simply using Extlib.

Extlib decouples data structures in a similar way to C++.
C++ uses iterators, Extlib uses streams.

The trick is factorisation: instead of

	to_set: Array --> Set
	to_array: Set --> Array

etc etc etc etc .. combinatorial explosion which invades all
data structures ..

you use:

	to_stream: Set -> stream
	from_stream: stream -> Set

	to_stream: Array  -> stream
	from_Stream: stream -> Array

and now you can define:

	let array_to_set a = Set.from_stream (Array.to_stream a)

So to get only one field of an array .. or anything else,
all you need is to write the extractor:

	proj: 'a -> 'b

and use

	stream_map proj x

like

	Set.from_stream (stream_map proj (Array.to_stream a))

Note that Extlib tries to be lazy so it will not actually build
two streams here (the actual function calls used by Extlib
may have different names).


-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


  parent reply	other threads:[~2006-10-15  4:12 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-10-15  3:29 Denis Bueno
2006-10-15  3:40 ` [Caml-list] " Jonathan Roewen
2006-10-15  3:48 ` skaller [this message]
2006-10-15  3:54 ` Jonathan Roewen
2006-10-15 11:26 ` Etienne Miret
2006-10-15 23:37 ` Jacques Garrigue
  -- strict thread matches above, loose matches on Subject: below --
2008-04-01  2:27 type error Jacques Le Normand
2008-04-01  2:42 ` [Caml-list] " Jacques Garrigue
2008-03-23 22:01 Jacques Le Normand
2008-03-23 22:19 ` [Caml-list] " Martin Jambon
2005-04-02  1:29 Type error Jon Harrop
2005-04-02  7:50 ` [Caml-list] " Jacques Garrigue

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=1160884080.28140.80.camel@rosella.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=caml-list@inria.fr \
    --cc=dbueno@gmail.com \
    /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).