Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Type error
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-10-15 (04:12)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] Type error
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


	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++: