Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Generics - Is this possible?

 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      04-19-2008
Patricia Shanahan <(E-Mail Removed)> wrote:
> Here's a solution that I just worked out for my own use:
> public class IteratorToIterable<T> implements Iterable<T> {
> [...]
> }


final Iterator<String> itStr= Arrays.asList("1","2","3").iterator();
for (String s:
new Iterable<String>() {
public Iterator<String> iterator() { return itStr; }
}
)
{
System.out.println(s);
}

Perhaps less elegant, but shorter
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      04-20-2008
Peter Duniho wrote:
> On Sat, 19 Apr 2008 20:45:25 -0700, Patricia Shanahan <(E-Mail Removed)> wrote:
>> Peter Duniho wrote:
>> ...
>>> I've yet to meet a language that absolutely prohibits what it
>>> "considers" dangerous coding practices (inasmuch as a programming
>>> language considers anything ). There's always some way around the
>>> nominal restrictions (note that I'm talking about coding constructs
>>> here, not things like security restrictions that might be imposed,
>>> sandboxing, etc.)

>> ...
>>
>> How about goto, one of the oldest and most basic coding constructs?

>
> How about it? Are you suggesting that Java doesn't have the equivalent?


goto is a reserved word in Java, but is not and never will be
implemented.

Arne
 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      04-20-2008
>>>> Patricia Shanahan <(E-Mail Removed)> wrote:
>>>>> Here's a solution that I just worked out for my own use:
>>>>> public class IteratorToIterable<T> implements Iterable<T> { [...] }
>>> Andreas Leitgeb wrote:
>>>> [anonymous Iterable-subclass whose .iterator() returns the iterator]

>> Lew wrote:
>>> [ misses the point, that we were starting from iterator, not iterable ]

> Patricia Shanahan wrote:
>> I'm sure the itStr declaration was just a way of getting into that
>> situation, and Andreas would not use such an indirect approach to
>> iterate over an array.

100% correct.

Lew <(E-Mail Removed)> wrote:
> Perhaps I did miss it, or perhaps I made a larger point about how the lack of
> an Iterable may be much less of a problem in practice.


I offer a different example:

for (String s: new Iterable<String>() {
public Iterator<String> iterator() { return new Scanner(System.in); }
}) {
System.out.println(s);
}

Scanner is an Iterator<String>, but not an Iterable.
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      04-20-2008
Peter Duniho <(E-Mail Removed)> wrote:
> I've yet to meet a language that absolutely prohibits what it "considers"
> dangerous coding practices (inasmuch as a programming language considers
> anything ).


I do think, that in Java the dangerous coding pracise of
pointer-arithmetics is really really prohibited

 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      04-20-2008
Peter Duniho <(E-Mail Removed)> wrote:
>> How about goto, one of the oldest and most basic coding constructs?

> How about it? Are you suggesting that Java doesn't have the equivalent?


The real pendant for goto would be a switch wrapped in an endless loop.

> Do you have an example of code that uses "goto" that you could not
> accomplish in any other way?


If you see forward jumps as structured "if", and backwards-jumps
as structured loops, then there can easily exist some mixture that
doesn't really map to a proper nesting of structured elements.
like jump into the middle of a loop, or jumping from one loop
to another or even from one subroutine to another.

I don't know if there are any theories about what spaghetti-code
can be structurized, and what (if any) not, or if there even exist
algorithms for this transformation.

 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      04-20-2008
lstephen 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?
>
> Thanks,
> Levi
>
>
>


How about this:

public static <A, B, T extends Collection<B>> T map(Iterable<A> source,
T dest, UnaryFunction<A, B> f) {
for (A a: source) {
dest.add(f.call(a));
}
return dest;
}

Example usage:

Set<Foo> foos = map(inputCollection, new HashSet<Foo>(), myFunction);

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      04-20-2008
Lew <(E-Mail Removed)> wrote:
> In other words, for those who missed my point, the premise that we have to
> start from an Iterator, not an Iterable, is itself of little significance
> because in such situations it will be more feasible to recast the problem
> to a more easily solved one.


Can you explain me, how to do the recast, when starting from a Scanner?

Since those situations, where we do start with an Iterable are trivial,
it's not very interesting to deal with those in this thread.
Your point seems to be, that the situation "start with an iterator"
doesn't ever happen in practise, and the Iterable's iterator() call
is always just preceding the currently focussed snippet of code.

But there's also Scanner and there may be other Iterator-subclasses,
for iterating ressources that can be iterated only once. So starting
with an Iterator (and not an Iterable) is not just an artifical problem.

You obviously avoided to comment on the Scanner-example that I added
to my previous posting.

 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      04-20-2008
Peter Duniho <(E-Mail Removed)> wrote:
> Well, inasmuch as in theory, two languages that are both "Turing-complete"
> should be able to implement identical algorithms, whether there's a
> well-defined mapping process or not it should be obvious that you can
> always come up with an equivalent.


Using turing-completeness for argumentation does not answer this problem.

Even if we assumed some "unlimited Java", the one existing implementation
(or even the nicest of all them) might just be an interpreter for the
verbatim original implementation.

Turing completeness of any structured target language is no proof that
a structured pendant exists for each spaghetti code.

 
Reply With Quote
 
lstephen
Guest
Posts: n/a
 
      04-21-2008
On Apr 21, 3:08 am, Daniel Pitts
<(E-Mail Removed)> wrote:
> lstephen 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?

>
> > Thanks,
> > Levi

>
> How about this:
>
> public static <A, B, T extends Collection<B>> T map(Iterable<A> source,
> T dest, UnaryFunction<A, B> f) {
> for (A a: source) {
> dest.add(f.call(a));
> }
> return dest;
>
> }
>
> Example usage:
>
> Set<Foo> foos = map(inputCollection, new HashSet<Foo>(), myFunction);
>
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


I agree that this covers the common case. But, I was looking for a way
to improve this in two ways.

1. the 'map' concept can be applied to other, non-collection, types
(e.g., Haskell's Maybe type)
2. I wanted to express the restriction that the type passed in was
what came back (e.g., If a Set was passed in, a Set would be returned)

Note that there would be more than one implementation of map around,
so the general type would only appear in the interface.

Thanks,
Levi
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      04-21-2008
Peter Duniho <(E-Mail Removed)> wrote:
>> Using turing-completeness for argumentation does not answer this problem.

> Why not?


Because we argue with two different meanings of "structured".
(Sorry I didn't notice that before - would have saved us some
iterations already)

Yours seems to be such that since Java has no goto, every legal
Java program is automatically "structured".

My definition of "structured" is a bit stricter, and would
exclude certain practices, like the one shown in this snippet:

int step=10;
while (step>0) {
switch (step) {
case 10: if (blah()) { step=30; continue; }
case 20: if (foo()) { step=0; break; }
case 30: if (snarf()) { step=10; continue; }
case 40: step=30; continue;
}
}
While it's legal Java code(-excerpt), it's not "structured"
according to my own humpty dumpty definition

I've looked at "http://en.wikipedia.org/wiki/Structured_programming"
but wasn't able to make out any sharp definition. The sample I
provided probably fits well to the "State machines" paragraph.
Quote: " It is possible to structure these systems by making each
state-change a separate subprogram..."
This could be interpreted, that such state-machine code is not yet
structured.

If there is some other "official" definition of "structured" that
supports your point, then, well, then you've won 10 points for
a correct statement, even though one of no use, due to my
(as it now appears) inexact question.

I'm interested, what others consider "structured" to mean.

 
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