From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 40880 invoked by alias); 27 Feb 2015 17:35:58 -0000 Mailing-List: contact kawa-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: kawa-owner@sourceware.org Received: (qmail 40864 invoked by uid 89); 27 Feb 2015 17:35:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL,BAYES_00,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mail.theptrgroup.com Received: from mail.theptrgroup.com (HELO mail.theptrgroup.com) (71.178.251.9) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 27 Feb 2015 17:35:56 +0000 Received: from [10.11.21.26] (unknown [10.11.21.26]) by mail.theptrgroup.com (Postfix) with ESMTPS id 0C1C1E0964 for ; Fri, 27 Feb 2015 12:35:53 -0500 (EST) Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 6.6 \(1510\)) Subject: Re: Support Iterable in for-each From: Jamison Hope In-Reply-To: <54EFD57D.9060808@bothner.com> Date: Fri, 27 Feb 2015 17:35:00 -0000 Content-Transfer-Encoding: quoted-printable Message-Id: References: <52D048D4.2080803@bothner.com> <54EFD57D.9060808@bothner.com> To: kawa@sourceware.org X-IsSubscribed: yes X-SW-Source: 2015-q1/txt/msg00052.txt.bz2 On Feb 26, 2015, at 9:25 PM, Per Bothner wrote: > [This is a follow-up to a discussion from January 2014.] >=20 > I've checked in code to generalize map, for-each, and vector-map > so the sequence arguments can be "generalized sequences": > * any java Iterable, which includes Scheme lists, vectors, uniform > vectors, and any java.util.List; > * any primitive array; > * and any CharSequence, including java.lang.String and Scheme strings. Very cool. I see that map always returns a list, regardless of the type(s) of the sequence argument(s). So if I've got an ArrayList of numbers #|kawa:1|# (define arraylist (java.util.ArrayList 1 2 3 4 5)) and I want to produce another ArrayList containing the squares of those numbers, I can do.. #|kawa:2|# (java.util.ArrayList @(map square arraylist)) [1, 4, 9, 16, 25] or #|kawa:3|# (apply java.util.ArrayList (map square arraylist)) [1, 4, 9, 16, 25] That was too painless. :-) Which is more idiomatic, (apply type list) or (type @list) ? > vector-for-each is generalized to any java.util.List, using the > get and size methods. (I.e it assumes the List is RandomAccess.) > Thus it performs poorly on Scheme lists. (Perhaps we should compile > in a cast to RandomAccess to guard against quadratic behavior on Scheme l= ists.) >=20 > Note that a CharSequence is considered a sequence of Unicode scalar values > (a surrogate pair is converted to a single character). OTOH a native > Java char array is considered a sequence of 16-bit char values. I think > it makes most sense this way. >=20 > If the sequence type is known as compile-time then the Kawa compiler > well generate custom code for that sequence type (see the scanner-for > procedure and the ScanHelper class in compile_map.scm). Otherwise > it will use an Iterator, selecting the Iterator kind at run-time (see the > static getIterator method in gnu/lists/Sequences.java). >=20 > Regardless, if the mapped-over procedure is a lambda it will > get inlined. >=20 > The splice operator @ now also handles strings: > (string #\X @"abc" #\Y) =3D=3D> "XabcY" > (However, this has not been optimized as well as I'd like yet.) BTW, I had to define a remove() method for CharacterIterator in gnu/lists/Sequences.java to build with Java 1.7, which doesn't have these newfangled "default" methods. I also had to add to the java-classes target in build.xml. -- Jamison Hope The PTR Group www.theptrgroup.com