This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
Re: Support Iterable in for-each
- From: Per Bothner <per at bothner dot com>
- To: kawa at sourceware dot org
- Date: Thu, 26 Feb 2015 18:25:01 -0800
- Subject: Re: Support Iterable in for-each
- Authentication-results: sourceware.org; auth=none
- References: <CAOTvmokU5J0Gy58iCa_UgU7FhvYLT6Ba7Z8C6W1yA7DBA44G_w at mail dot gmail dot com> <52D048D4 dot 2080803 at bothner dot com>
[This is a follow-up to a discussion from January 2014.]
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.
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 lists.)
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.
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).
Regardless, if the mapped-over procedure is a lambda it will
get inlined.
The splice operator @ now also handles strings:
(string #\X @"abc" #\Y) ==> "XabcY"
(However, this has not been optimized as well as I'd like yet.)
--
--Per Bothner
per@bothner.com http://per.bothner.com/