Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Generics - Is this possible?

Reply
Thread Tools

Generics - Is this possible?

 
 
lstephen
Guest
Posts: n/a
 
      04-15-2008
On Apr 16, 1:53 am, (E-Mail Removed) wrote:
>
>
> I've implemented a functional library that addresses this issue. I
> just use the Iterable<T> type unless the operation specifically
> depends on list ordering.


Thanks for the example. I was experimenting with functional like
concepts, but I wanted to be more specific that Iterable, i.e., if you
pass in List<A> you get back List<B>, if you pass in a Set, you get
back a Set etc. Also I was hoping this would allow map to be used on
non collection types (e.g., something like Haskell's Maybe or Either
but translated to Java.)

As I mentioned elsewhere there is no practical reason for me doing
this, just experimenting

Thanks,
Levi
 
Reply With Quote
 
 
 
 
lstephen
Guest
Posts: n/a
 
      04-15-2008
On Apr 15, 10:49 pm, Lew <(E-Mail Removed)> wrote:
> lstephen wrote:
> > Hi,

>
> > I'm looking at the signature for something like a 'map' function.

>
> 'map' is a somewhat unfortunate variable name, since you aren't actually
> associating it with the wildly popular 'Map' type.
>
> > For List it may be something like:

>
> > List<B> map(List<A> a, UnaryFunction<A, B> f)

>
> > But, I want I'd rather it not be List specific, so I was after
> > something like:

>
> > T<B> map(T<A> a UnaryFunction<A, B> f)

>
> > But, the compiler doesn't like this

>
> > Any ideas on how or whether this is possible?

>
> What you want to accomplish is possible, what you're trying to say isn't.
> You're trying to indicate a type parameter that takes a type parameter; that
> doesn't exist.


By doesn't exist, do you mean not possible in Java?

> Type parameters exist to restrict the range of acceptable
> types; what you wrote, 'T<A>', is equivalent to merely 'T'.
>
> What is the restriction that you actually want to place on T? Do you want it
> to be a Collection?


The restriction I want to place on T is that it's a generic class that
takes one type parameter.

>
> public static <A, B>
> Collection <B> xform( Collection <A> a, UnaryFunction <A, B> f )
>
> (untried, untested, uncompiled, one of several solutions that differ subtly in
> semantics)


Yep, I could definately cover the most common case by using Iterable
or Collection, but I was trying to see how general I could make this.
>
> If you have a class called 'UnaryFunction', shouldn't it overload its "unary
> function" method with one that takes a Collection? Having a "friend" method
> like that shown breaks encapsulation.


UnaryFunction wouldn't be passed a collection, it would be passed an
element form a collection - it wouldn't even know the element was part
of a collection.

>
> --
> Lew


 
Reply With Quote
 
 
 
 
lstephen
Guest
Posts: n/a
 
      04-15-2008
On Apr 15, 11:20 pm, "Larry A Barowski"
<ThisisLarrybarAtEngDotAuburnDotLearninginstitutio n> wrote:
> "lstephen" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> > Hi,

>
> > I'm looking at the signature for something like a 'map' function.

>
> > For List it may be something like:

>
> > List<B> map(List<A> a, UnaryFunction<A, B> f)

>
> > But, I want I'd rather it not be List specific, so I was after
> > something like:

>
> > T<B> map(T<A> a UnaryFunction<A, B> f)

>
> > But, the compiler doesn't like this

>
> > Any ideas on how or whether this is possible?

>
> If that were possible, java.util.Collections would not
> need the methods synchronizedList, synchronizedSet,
> and synchronizedSortedSet. It would only need
> synchronizedCollection.


Good point. I think this is a strong indication that the answer is no.

Thanks,
Levi
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-15-2008
Peter Duniho wrote:
> On Tue, 15 Apr 2008 15:19:17 -0700, Patricia Shanahan <(E-Mail Removed)> wrote:
>
>> Incidentally, does anyone understand *why* it has to be an Iterable, not
>> an Iterator? Of course, an Iterator based for loop would only process
>> the elements from the Iterator's current position on.

>
> Only the language designers could tell you the actual reason why. But I
> would agree with anyone who felt it wise to not allow user code access
> to the actual iterator being used for the loop. The potential for
> adding bugs seems to me to outweigh any potential convenience. It keeps
> the semantics of the for( syntax nice, simple, and easy-to-predict.


I certainly agree that not allowing user code access to the actual
iterator would be desirable, if it were possible. It isn't. The
following, horrible, program prints "1", then "3", then gets a
java.util.NoSuchElementException.

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class WildFor {
public static void main(String[] args) {
List<Integer> myList = Arrays.asList(1, 2, 3, 4, 5);
RememberingIterable<Integer> myIterable =
new RememberingIterable<Integer>(myList);
for (Integer i : myIterable) {
myIterable.getLastIterator().next();
System.out.println(i);
}
}

static class RememberingIterable<T> implements
Iterable<T> {
private Iterable<T> baseIterable;

private Iterator<T> lastIterator;

public RememberingIterable(Iterable<T> iterable) {
baseIterable = iterable;
}

public Iterator<T> iterator() {
lastIterator = baseIterable.iterator();
return lastIterator;
}

public Iterator<T> getLastIterator() {
return lastIterator;
}
}

}

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-16-2008
Peter Duniho wrote:
> On Tue, 15 Apr 2008 16:41:55 -0700, Patricia Shanahan <(E-Mail Removed)> wrote:
>
>> I certainly agree that not allowing user code access to the actual
>> iterator would be desirable, if it were possible. It isn't.

>
> But it would be if Java supported using an Iterator in the for() statement.
>
>> The
>> following, horrible, program prints "1", then "3", then gets a
>> java.util.NoSuchElementException.

>
> Right. But if you can provide an actual iterator in the for()
> statement, then the code has access to the iterator implicitly. It
> wouldn't be nearly so much work to reproduce the example of bad usage
> you provided if Java allowed an Iterator instead of an Iterable
> implementation in the for() statement. Your example would instead look
> like this:

....

The point is that not allowing Iterator does not prevent access to it,
just makes it a bit harder. Meanwhile, it also makes legitimate uses harder.

Patricia
 
Reply With Quote
 
thufir
Guest
Posts: n/a
 
      04-16-2008
On Tue, 15 Apr 2008 00:34:07 -0700, lstephen wrote:


> For List it may be something like:
>
> List<B> map(List<A> a, UnaryFunction<A, B> f)



would you flesh this example out?



thanks,

Thufir
 
Reply With Quote
 
Hendrik Maryns
Guest
Posts: n/a
 
      04-16-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) schreef:
> On Apr 15, 2:34 am, lstephen <(E-Mail Removed)> wrote:
>> <snip request for map>

>
> I've implemented a functional library that addresses this issue. I
> just use the Iterable<T> type unless the operation specifically
> depends on list ordering. Here's a skeleton of my class declarations
> adapted to your notation. Note, my implementation uses lazy
> evaluation so you can do things like sum up an infinite list.


Very nice, but…

> private static <R, T> Iterator<R> map_iterator( final UnaryFunction<R,


Please, please don’t. It hurts me so much to see such beautiful code
and then not following naming conventions.

Sorry, had to get this off my heart.

H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFIBcy1e+7xMGD3itQRAn38AJwNeBqOsEV551I1HIA+ij +8PronVACfbA4T
ciBWXRMu15rCQCNiZxiolQ4=
=ql66
-----END PGP SIGNATURE-----

 
Reply With Quote
 
lstephen
Guest
Posts: n/a
 
      04-16-2008
On Apr 16, 6:30 pm, thufir <(E-Mail Removed)> wrote:
> On Tue, 15 Apr 2008 00:34:07 -0700, lstephen wrote:
> > For List it may be something like:

>
> > List<B> map(List<A> a, UnaryFunction<A, B> f)

>
> would you flesh this example out?


It would probably be something like (without compiling or testing it):

public interface UnaryFunction<A, B> {
B apply(A a);
}

public List<B> map(List<A> as, UnaryFunction<A, B> f) {
List<B> bs = new ArrayList<B>();
for (A a : as) {
bs.add(f.apply(a));
}
return bs;
}


>
> thanks,
>
> Thufir


Levi
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      04-17-2008
Peter Duniho <(E-Mail Removed)> wrote:
> On Wed, 16 Apr 2008 17:44:53 -0700, Lew <(E-Mail Removed)> wrote:
>> There is a standard naming convention in Java, published by Sun in 1999.
>> Ignore it at your peril.

> Peril? Something bad will happen to me? What? What bad thing will
> happen?
> And I mean, other than Java zealots harrassing me about my naming.


Zealots are not just here in c.l.j.p, but you may also meet some
in real world - e.g. those who review your code and rate it poorly.
The zealots *here* rather help you be prepared for the others

 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      04-17-2008
Peter Duniho wrote:
> On Wed, 16 Apr 2008 17:44:53 -0700, Lew <(E-Mail Removed)> wrote:
>> Forget it, Pete. Java is a community, not just a language, and the
>> community has standards.

>
> Java _is_ "just a language". There may also be a "Java community", but
> the language itself doesn't enforce any particular naming convention and
> there's no requirement to comply with "community standards" in order to
> be a successful Java programmer.
>


This is true but misses the point that if you wish to enlist the help of
the community in fixing some problem you are having with your Java
source code, you'll get a better response from the community if your
source code follows the community conventions that make it easier for
community members to follow.

Using underscore connected words instead of camelCase isn't much of a
hindrance, it's when people use wild indentation or initial upper-case
for variables that my reading speed tends to slow down dramatically. I
tend to lose motivation a bit when I think the author is wilfully making
it harder for helpers to help.

bI awrl menes beeYidIosYnCRAT.IC wern ryEtAng koid fore eeyore Owen
prusell Owen Lee.

When communicating with others, I think it helps to follow the conventions.

Just my EUR 0.00000002 worth.

--
RGB
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM
generics in TB valentin tihomirov VHDL 4 12-18-2003 07:04 PM
Integers only as generics? Acciduzzu VHDL 4 09-23-2003 12:45 AM
Re: Multi-dimentional arrays in components using generics Willem Oosthuizen VHDL 1 07-09-2003 12:13 PM



Advertisments