From: Per Bothner <per@bothner.com>
To: kawa@sourceware.org
Subject: Re: Support Iterable in for-each
Date: Fri, 27 Feb 2015 20:43:00 -0000 [thread overview]
Message-ID: <54F0D6CD.20207@bothner.com> (raw)
In-Reply-To: <F3C17C73-BF81-472F-AA85-2BC189B34C6D@theptrgroup.com>
On 02/27/2015 09:35 AM, Jamison Hope wrote:
> On Feb 26, 2015, at 9:25 PM, Per Bothner <per@bothner.com> wrote:
>> I've checked in code to generalize map, for-each, and vector-map
>> so the sequence arguments can be "generalized sequences":
> Very cool. I see that map always returns a list, regardless of the
> type(s) of the sequence argument(s).
Yes - the alternative seems too complicated. Scala has a complicated
framework for picking a result type based on the argument type, but it
seems very complicated and fragile. What if you map sqrt over an int[]?
Should the result be a double[]? What when there are multiple sequence
arguments? Note the rules have to work sanely and consistently both
with compile-time typing and run-time-typing. It's a lot easier when
the compiler just knows the result type. And as you show there is an
easy work-around with constructors and splices.
> 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]
> Which is more idiomatic, (apply type list) or (type @list) ?
I'd say the latter. Currently apply isn't well optimized, Though splicing isn't
as well optimized as I'd like, it's better than apply. For example calling
a varargs method with a splice is complied pretty well.
My plan is to have the compiler rewrite: (apply f x y lst)
to: (f x y @lst)
When that is done they'll have the same performance,
Further out, the plan is to allow this syntax:
(java.util.ArrayList (square (each arraylist)) ...)
This uses the same '...' as in syntax patterns, and 'each' is a magic
function inside the context of a '...'. It would also work in patterns:
(! [x ...] arraylist) ;; x is 'each' element of arraylist
(java.util.ArrayList (* x x) ...)
I expect this feature before the next release.
> 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.
Oops - fixed.
> I also had to add
> <include name="kawa/Source*.java"/>
> to the java-classes target in build.xml.
It doesn't seem like it's strictly needed, but I fixed this too.
--
--Per Bothner
per@bothner.com http://per.bothner.com/
prev parent reply other threads:[~2015-02-27 20:43 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-10 18:45 Matthieu Vachon
2014-01-10 19:24 ` Per Bothner
2014-01-10 21:04 ` Jamison Hope
2014-01-10 21:20 ` Per Bothner
2014-01-10 21:27 ` Jamison Hope
2014-01-10 21:27 ` Per Bothner
2014-01-10 21:29 ` Jamison Hope
2015-02-27 2:25 ` Per Bothner
2015-02-27 17:35 ` Jamison Hope
2015-02-27 18:22 ` Matthieu Vachon
2015-02-27 20:43 ` Per Bothner [this message]
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=54F0D6CD.20207@bothner.com \
--to=per@bothner.com \
--cc=kawa@sourceware.org \
/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 read-only IMAP folder(s) and NNTP newsgroup(s).