From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id F27B0BC6F for ; Sun, 15 Oct 2006 06:12:29 +0200 (CEST) Received: from ipmail01.adl2.internode.on.net (ipmail01.adl2.internode.on.net [203.16.214.140]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id k9F4CRug004745 for ; Sun, 15 Oct 2006 06:12:29 +0200 Received: from ppp229-164.lns3.syd7.internode.on.net (HELO rosella) ([59.167.229.164]) by ipmail01.adl2.internode.on.net with ESMTP; 15 Oct 2006 13:18:03 +0930 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AY8CAGdLMUU X-IronPort-AV: i="4.09,311,1157293800"; d="scan'208"; a="29174437:sNHT39331215" Subject: Re: [Caml-list] Type error From: skaller To: Denis Bueno Cc: OCaml Mailing List In-Reply-To: <6dbd4d000610142029m92d7005v65e95f031e7eae9b@mail.gmail.com> References: <6dbd4d000610142029m92d7005v65e95f031e7eae9b@mail.gmail.com> Content-Type: text/plain Date: Sun, 15 Oct 2006 13:48:00 +1000 Message-Id: <1160884080.28140.80.camel@rosella.wigram> Mime-Version: 1.0 X-Mailer: Evolution 2.6.1 Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 4531B52B.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; compilable:01 vastly:01 2006:98 extractor:98 sourceforge:01 iterators:01 wrote:01 caml-list:01 define:01 data:02 data:02 lazy:02 structures:02 structures:02 objects:02 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 Felix, successor to C++: http://felix.sf.net