Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Generics - Is this possible? (http://www.velocityreviews.com/forums/t605653-generics-is-this-possible.html)

lstephen 04-15-2008 07:34 AM

Generics - Is this possible?
 
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




Andreas Leitgeb 04-15-2008 03:33 PM

Re: Generics - Is this possible?
 
Lew <lew@lewscanon.com> 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.


Jan Thomä 04-15-2008 04:30 PM

Re: Generics - Is this possible?
 
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#...

lscharen@d.umn.edu 04-15-2008 04:53 PM

Re: Generics - Is this possible?
 
On Apr 15, 2:34*am, lstephen <levi.step...@gmail.com> 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();
}
};
}

Andreas Leitgeb 04-15-2008 08:20 PM

Re: Generics - Is this possible?
 
lscharen@d.umn.edu <lscharen@d.umn.edu> 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.


Patricia Shanahan 04-15-2008 08:32 PM

Re: Generics - Is this possible?
 
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

Roedy Green 04-15-2008 08:34 PM

Re: Generics - Is this possible?
 
On Tue, 15 Apr 2008 00:34:07 -0700 (PDT), lstephen
<levi.stephen@gmail.com> 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

Andreas Leitgeb 04-15-2008 08:52 PM

Re: Generics - Is this possible?
 
Patricia Shanahan <pats@acm.org> 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.

Patricia Shanahan 04-15-2008 10:19 PM

Re: Generics - Is this possible?
 
Andreas Leitgeb wrote:
> Patricia Shanahan <pats@acm.org> 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

lstephen 04-15-2008 11:19 PM

Re: Generics - Is this possible?
 
On Apr 16, 5:34 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Tue, 15 Apr 2008 00:34:07 -0700 (PDT), lstephen
> <levi.step...@gmail.com> 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


All times are GMT. The time now is 05:02 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.