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
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?

Thanks,
Levi



 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      04-15-2008
Lew <(E-Mail Removed)> wrote:
> lstephen wrote:
>> 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)


If you won't specify anything about T itself, you
wouldn't be able to do anything at all with it.
Inside your "map" method, T would be like Object,
and that has no method to retrieve any "A" from it

> public static <A, B>
> Collection <B> xform( Collection <A> a, UnaryFunction <A, B> f )
> (untried, untested, uncompiled, one of several solutions [...])


Another problem is, that xform has the "burden" of picking some
implementation of a Collection, an instance of which it then
returns, filled with all the "B"-objects.

> 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.


That would just shift the problems from xform to UnaryFunction's
overloaded(or just additional) method. If "UnaryFunction" was
an interface, it would mean that any implementation would also
need to care for an implementation of the Collection-handling
method.
Imho "map" or "xform" are rather the type of methods that
would belong into a "Collections"-like toolkit class.

 
Reply With Quote
 
 
 
 
Jan Thomä
Guest
Posts: n/a
 
      04-15-2008
On Tue, 15 Apr 2008 10:20:11 -0400 Larry A Barowski wrote:
> If that were possible, java.util.Collections would not
> need the methods synchronizedList, synchronizedSet,
> and synchronizedSortedSet. It would only need
> synchronizedCollection.


On a side note, something like this is possible in C#:


// function that creates a T
public T create<T>() {
return new T();
}

and you can call this like: Foo foo = create<Foo>(); Doesn't work for java
though, but nicely shows the different approach in C#...
 
Reply With Quote
 
lscharen@d.umn.edu
Guest
Posts: n/a
 
      04-15-2008
On Apr 15, 2:34*am, lstephen <(E-Mail Removed)> wrote:
> 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?


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.

For example, I can create a Range class that takes a starting and
finishing value and implements Iterable<Integer>. I can write code
like this:

Range r = new Range(0, Integer.MAX_VALUE);
UnaryFunction sqr = new Square();

Iterable<Integer> squares = map( sqr, r );

for ( Integer x : squares )
{
// do something
if ( x > 100 )
break;
}


Here is the code outline:

public class UnaryFunction<R, T> {
public R eval( T arg ) { ... }
}

public static <R, T> Iterable<R> map( final UnaryFunction<R, T> f,
final Iterable<? extends T> list )
{
return new Iterable<R>() {
public Iterator<R> iterator() {
return map_iterator( f, list.iterator() );
}
}
}

private static <R, T> Iterator<R> map_iterator( final UnaryFunction<R,
T> f, final Iterator<? extends T> iterator )
{
return new Iterator<R>()
{
public boolean hasNext() {
return list.hasNext();
}
public R next() {
return f.eval( list.next() );
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      04-15-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
> public static <R, T> Iterable<R> map(
> final UnaryFunction<R, T> f,
> final Iterable<? extends T> list )


I'm just curious, why you chose to return an
Iterable, rather than an Iterator.

I haven't found any standard class that would
take an Iterable as argument, so the only thing
to do with the returnvalue is to obtain an
Iterator... did I miss something?

Even if you obtain an Iterator more than once,
each such Iterator will just recalculate the
same values.

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-15-2008
Andreas Leitgeb wrote:
....
> I haven't found any standard class that would
> take an Iterable as argument, so the only thing
> to do with the returnvalue is to obtain an
> Iterator... did I miss something?

....

There is one thing you can do with an Iterable but not an Iterator - use
it in an enhanced for-loop.

Patricia
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-15-2008
On Tue, 15 Apr 2008 00:34:07 -0700 (PDT), lstephen
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

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


What methods are you planning to use inside your map method? If it is
to be used on List, it must either be the List, Collection or Iterable
methods. So you must pass something of type of one of those three.

Java is a strongly typed language!
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      04-15-2008
Patricia Shanahan <(E-Mail Removed)> wrote:
> Andreas Leitgeb wrote:
>> ..., so the only thing
>> to do with the returnvalue is to obtain an
>> Iterator... did I miss something?

> There is one thing you can do with an Iterable but not an Iterator - use
> it in an enhanced for-loop.


Heck, I really missed *something* :-}

Thanks also to Peter Duniho.
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-15-2008
Andreas Leitgeb wrote:
> Patricia Shanahan <(E-Mail Removed)> wrote:
>> Andreas Leitgeb wrote:
>>> ..., so the only thing
>>> to do with the returnvalue is to obtain an
>>> Iterator... did I miss something?

>> There is one thing you can do with an Iterable but not an Iterator - use
>> it in an enhanced for-loop.

>
> Heck, I really missed *something* :-}
>
> Thanks also to Peter Duniho.


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.

Patricia
 
Reply With Quote
 
lstephen
Guest
Posts: n/a
 
      04-15-2008
On Apr 16, 5:34 am, Roedy Green <(E-Mail Removed)>
wrote:
> On Tue, 15 Apr 2008 00:34:07 -0700 (PDT), lstephen
> <(E-Mail Removed)> wrote, quoted or indirectly quoted someone
> who said :
>
> >But, I want I'd rather it not be List specific, so I was after
> >something like:

>
> What methods are you planning to use inside your map method? If it is
> to be used on List, it must either be the List, Collection or Iterable
> methods. So you must pass something of type of one of those three.


Sorry, I wasn't clear that map would have more than one
implementation. I'm enjoy playing around with translating concepts
from one language to others. In this case it's the Functor type class
from Haskell. So map would be an abstract method of a Functor class.
Then I could create ListFunctor, SetFunctor, MapFuctor etc. Then to
take more from Haskell MaybeFunctor, EitherFunctor etc.
So there is no practical problem to solve, just experimenting.

>
> Java is a strongly typed language!


Yep. I'm trying to express that if you pass in a List<A> you would get
back a List<B>, pass in Set<A> you get back Set<B>. i.e., the type of
collection you pass in, is what you get back.


> --
>
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com


Thanks,
Levi
 
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