Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
#229: The Pair class
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 26 - 38 of 38 - Expand all  -  Translate all to Translated (View all originals) < Older 
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Josh Suereth  
View profile  
 More options Feb 10 2009, 2:40 am
From: Josh Suereth <joshua.suer...@gmail.com>
Date: Tue, 10 Feb 2009 02:40:31 -0500
Local: Tues, Feb 10 2009 2:40 am
Subject: Re: [The Java Posse] Re: #229: The Pair class

I think perhaps as Scala is academic, speed takes more of a focus for some
decisions.  Although what you're describing with nested pairs works
perfectly well, the performance of grabbing item N off the Tuple is N-1
lookups on Pair.   Call it what you will, but Scala tries to strike a
balance between elegance and speed.   As you add these higher-level
abstractions it can be tough to keep them fast enough for "general-purpose"
(or better yet, specific purpose) computing.

Note that Scala takes the approach for sizes you outline in it's "Seq"
abstract Trait.   It is recommended against calling the length method for
performance reasons.  If you want fast length calculations, use a List.

It seems if you'd like to support an arbitrary number of Tuples, you
literally should return Arrays of Objects or some form of Collection, then
have javac auto-cast results for you in bytecode.   The difference here
would be some form of bytecode flag that says: "This return value isn't a
list, it's actually a Tuple of these other types".  Either that, or have the
JVM support tuples directly

-Josh

On Mon, Feb 9, 2009 at 10:50 PM, Reinier Zwitserloot <reini...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Viktor Klang  
View profile  
 More options Feb 10 2009, 5:36 am
From: Viktor Klang <viktor.kl...@gmail.com>
Date: Tue, 10 Feb 2009 11:36:19 +0100
Local: Tues, Feb 10 2009 5:36 am
Subject: Re: [The Java Posse] Re: #229: The Pair class

On Tue, Feb 10, 2009 at 12:34 AM, Michael Neale <michael.ne...@gmail.com>wrote:

> So how would you do it in scala ;) ?

*laugh*

Well, the Scalaposse isn't really broadcasting yet ;)

--
Viktor Klang
Senior Systems Analyst

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Reinier Zwitserloot  
View profile  
 More options Feb 10 2009, 6:04 am
From: Reinier Zwitserloot <reini...@gmail.com>
Date: Tue, 10 Feb 2009 03:04:39 -0800 (PST)
Local: Tues, Feb 10 2009 6:04 am
Subject: Re: #229: The Pair class
That was a joke.

On Feb 10, 5:00 am, Christian Catchpole <christ...@catchpole.net>
wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Reinier Zwitserloot  
View profile  
 More options Feb 10 2009, 7:02 am
From: Reinier Zwitserloot <reini...@gmail.com>
Date: Tue, 10 Feb 2009 04:02:28 -0800 (PST)
Local: Tues, Feb 10 2009 7:02 am
Subject: Re: #229: The Pair class
Even if you use a list instead of a tuple, lists don't support
heterogenous typing; you can have a tuple of type <String, Integer>,
but you can't have a list that is defined to contain alternating
String/Integer. I agree that it would be great if somehow the actual
underlying object is the same thing between a tuple and a list, and
the only difference is that a tuple has a set size and a listed type
for each element, whereas a list has a single type for all elements
and can be any size. I guess you could theoretically employ a nested
pairs structure just for the sake of the compiler, and actually do a
relatively quick list lookup + cast at runtime, using copious amounts
of unsafes. Without more sugar you can't make this work in the JVM
(You'd define a tuple as a Tuple<H, T>, so how do you extract the H of
the Tuple that is the T? Or the H of the Tuple that is the T of the
tuple that is the T of this tuple? The generics type system has the
info, there's just no syntax (in java 1.6) to liberate the right type.
Scala has higher kinded types but I'm not sure it can solve this
problem either, by the way.

I get that scala's current solution was the easiest given the
constraints, but littering the top-level namespace with 22 instances
of FoobarX, and having an arbitrary limit at all, can't be the right
answer. If java is to get arbitrary-length tuples I'd prefer a less
hacky solution.

On Feb 10, 8:40 am, Josh Suereth <joshua.suer...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Fischer  
View profile  
 More options Feb 10 2009, 9:02 am
From: Robert Fischer <robert.fisc...@smokejumperit.com>
Date: Tue, 10 Feb 2009 09:02:07 -0500
Local: Tues, Feb 10 2009 9:02 am
Subject: Re: #229: The Pair class
As your third paragraph states, if you're grabbing the Nth element out of an N-tuple that way,
you're doing something wrong.  Probably confusing "tuple" with "list".

In some other languages, a method that takes a tuple transparently unpacks the tuple into local
variables, so accessing the Nth element out of the tuple is just a local variable access.  This is
done on the assumption that you're asking for a tuple because you know what you're doing, and at
least the majority of elements are meaningful.  Similarly, the most common way to unpack returned
tuples is through a case statement or multiple assignment, either of which is basically the same
stunt as unpacking into local variables.

The reason Scala has 22 "Tuple" types is because they need some way to hook type information onto
(1,"foo") in a way that's coherent to the Java-based generics model they've got.

~~ Robert.

--
~~ Robert Fischer.
Grails Trainining      http://GroovyMag.com/training
Smokejumper Consulting http://SmokejumperIT.com
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Fischer  
View profile  
 More options Feb 10 2009, 9:03 am
From: Robert Fischer <robert.fisc...@smokejumperit.com>
Date: Tue, 10 Feb 2009 09:03:02 -0500
Local: Tues, Feb 10 2009 9:03 am
Subject: Re: [The Java Posse] Re: #229: The Pair class
Why would it be good for a Tuple to be a List?

~~ Robert.

--
~~ Robert Fischer.
Grails Trainining      http://GroovyMag.com/training
Smokejumper Consulting http://SmokejumperIT.com
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Iry  
View profile  
 More options Feb 10 2009, 9:45 am
From: James Iry <james...@gmail.com>
Date: Tue, 10 Feb 2009 06:45:36 -0800 (PST)
Local: Tues, Feb 10 2009 9:45 am
Subject: Re: #229: The Pair class
Scala doesn't work that way because Tuples have O(1) access to all
elements where what you propose would have O(n) access.  Having 22
Tuple classes is definitely a code smell.  Same with having 22
Function classes.  But those are code smells forced by the JVM.  On
"machines" with a less strict stack discipline than the JVM, e.g. the
x86, there are other ways to encode tuples that are much more
flexible.

An alternative on the JVM is to use runtime byte code generation and
classloader-fu to generate TupleN classes as needed.  But Scala has
avoided that kind of magic as it tends to not play well with other
things that want to do classloader-fu.

What you've proposed is called a Heterogeneous Linked List, or HList
for short.  It's exactly the kind of list you get from Lisps.  Most
statically typed languages can't do them, or at least not usefully.
HLists are very tricky to type correctly and still make pleasant to
use. Scala can do it, but only just barely using a currently
experimental compiler flag.  When that feature stabilizes you can
probably expect HLists to be in the standard library.

Finally, related to your last point re: BGGA, Scala does have a type
for undefined called "Nothing."  BGGA's undefined type was renamed
from "Unreachable" to "Nothing" in order to follow Scala's example:
http://gafter.blogspot.com/2008/08/java-closures-prototype-feature.html.
Both names make sense in context.

The BGGA Void type means something else.  Void is the type of a
closure that returns normally but doesn't return any useful data.
Scala has an equivalent (again, before BGGA did), but Scala calls it
Unit to differentiate from the non-first class nature of Java's void
and to help further distinguish it from Nothing.

On Feb 9, 7:50 pm, Reinier Zwitserloot <reini...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Reinier Zwitserloot  
View profile  
 More options Feb 10 2009, 11:40 am
From: Reinier Zwitserloot <reini...@gmail.com>
Date: Tue, 10 Feb 2009 08:40:16 -0800 (PST)
Local: Tues, Feb 10 2009 11:40 am
Subject: Re: #229: The Pair class
There's 'synthetic', which is a flag on class members that indicates
that they aren't intentional (by the code author), just a side-effect
of the compiler making it all work right. A side-effect is that most
IDEs suppress them in auto-complete models, which is overkill, but
another flag that means: This is a system type thing, so, auto-
complete, sure, but definitely don't generate javadocs, or offer it
when extending classes, etcetera. I don't like structural typing in a
java closure proposal, but if it does happen, The FunctionIII stuff
can also use such a flag. Adding flags to members is backwards
compatible. Just an idea.

NB: For a closure proposal I was cooking up I'm going to need a bunch
of synthetic publics, which fortunately works as intended; javac will
refuse to compile access to synthetics in .java source, and eclipse
does not list them in auto-completes etc, but the JVM completely
ignores the synthetic flag. (allows any call to a synthetic same as a
call to a non-synthetic). Nice feature.

On Feb 10, 3:45 pm, James Iry <james...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Becker  
View profile  
 More options Feb 11 2009, 6:16 am
From: Peter Becker <peter.becker...@gmail.com>
Date: Wed, 11 Feb 2009 21:16:36 +1000
Local: Wed, Feb 11 2009 6:16 am
Subject: Re: [The Java Posse] Re: #229: The Pair class
Re 22: consider the empty product/tuple and suddenly you are in the
realm of conspiracy theories. With the functions you actually see the
true number -- they explicitly start with 0.

How far is it from the EPFL to Lake Totenkopf? Does either really exist?
Who's reality is this anyway?

  Peter

PS: no, we are probably not doomed, it's just the type of comments
people deserve for arbitrary cut-offs :-)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bill Wohler  
View profile  
 More options Feb 11 2009, 5:01 pm
From: Bill Wohler <bill.woh...@gmail.com>
Date: Wed, 11 Feb 2009 14:01:14 -0800 (PST)
Local: Wed, Feb 11 2009 5:01 pm
Subject: Re: #229: The Pair class
On Feb 9, 9:26 am, d...@happygiraffe.net (Dominic Mitchell) wrote:

> Hear, hear!  Although, I'd call it "of" rather than create, as that's
> what the google collections library does.  The type inference leads to a
> really nice API.

Thanks, Dominic. I used your message as an incentive to replace our
Pair constructor with an "of" factory method. And yes, it does a great
job of cleaning things up! I hit one snag. Is there a way around the
following compilation problem?

public class PairTest {
    /*
     * The Pair.of() call below yields the following compilation
error: Type
     * mismatch: cannot convert from Pair<Class<capture#1-of ? extends
     * Exception>,Class<capture#2-of ? extends Exception>> to
Pair<Class<?
     * extends Exception>,Class<? extends Exception>>
     */
    public static void main(String[] args) {
        Class<? extends Exception> foo;
        Class<? extends Exception> bar;

        // Original line with constructor.
        Pair<Class<? extends Exception>, Class<? extends Exception>>
compiles = new Pair<Class<? extends Exception>, Class<? extends
Exception>>(
            foo, bar);

        // Failed attempt at using of.
        Pair<Class<? extends Exception>, Class<? extends Exception>>
doesntCompile = Pair.of(
            foo, bar);
    }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Reinier Zwitserloot  
View profile  
 More options Feb 12 2009, 5:57 am
From: Reinier Zwitserloot <reini...@gmail.com>
Date: Thu, 12 Feb 2009 02:57:42 -0800 (PST)
Local: Thurs, Feb 12 2009 5:57 am
Subject: Re: #229: The Pair class
Yes, write it like so:

Pair.<? extends Exception, ? extends Exception>of(foo, bar);

Not pretty, but it works.

though, I'm somehat confounded as to why java wouldn't compile it as
is. Generics still surprise me sometimes.

On Feb 11, 11:01 pm, Bill Wohler <bill.woh...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bill Wohler  
View profile  
 More options Feb 12 2009, 12:10 pm
From: Bill Wohler <bill.woh...@gmail.com>
Date: Thu, 12 Feb 2009 09:10:41 -0800 (PST)
Local: Thurs, Feb 12 2009 12:10 pm
Subject: Re: #229: The Pair class
On Feb 12, 2:57 am, Reinier Zwitserloot <reini...@gmail.com> wrote:

> Yes, write it like so:

> Pair.<? extends Exception, ? extends Exception>of(foo, bar);

> Not pretty, but it works.

Thanks, Reinier. Weird, but true!

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
joel.neely  
View profile  
 More options Feb 17 2009, 9:03 am
From: "joel.neely" <joel.ne...@gmail.com>
Date: Tue, 17 Feb 2009 06:03:34 -0800 (PST)
Local: Tues, Feb 17 2009 9:03 am
Subject: Re: #229: The Pair class
<ROTFL>Thanks for posting that!

(no closing tag... I'm still chuckling silently!)

-jn-

On Feb 9, 10:00 pm, Christian Catchpole <christ...@catchpole.net>
wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages < Older 
« Back to Discussions « Newer topic     Older topic »