The advantage of going via a call to 'map' instead of an explicit loop would be that the map call could be one that magically knows whether it's worth parallelising.
On Thu, Jan 6, 2011 at 12:28 PM, Carl Jokl <[email protected]> wrote: > It still looks like it may not be possible yet in core Java without > using google collections or another external library. I suppose the > desire to be able to do this kind of things would be a driver for > wanting to add closure to Java. > > I get the principle that what I am really doing is mapping one value > to another value (Remembering Haskell from University) and that order > does not really matter. > > If it were an array of 8 values and a machine had 8 cores then > potentially the entire operation could be done together. The theory is > fair enough but I wonder about the overhead of breaking up something > like this over multiple threads and whether for more trivial > operations, the overhead of splitting up the computation and doing it > in parallel might outweigh the time savings of being able to do > parallel batches. I imagine there is some kind of threshold where the > parallel processing benefit is greater than the overhead. It may be a > lower threshold than I realise. > > On Jan 6, 12:14 pm, Ricky Clarkson <[email protected]> wrote: >> I think you're looking for map. >> >> import fj.F; >> import fj.data.Array; >> >> Integer[] legs = Array(animals).map(new F<Animal, Integer>() { >> public Integer f(Animal animal) { >> return animal.legs; >> } >> >> }).array(Integer.class); >> >> fj = Functional Java. >> >> >> >> On Thu, Jan 6, 2011 at 11:47 AM, Carl Jokl <[email protected]> wrote: >> > One item which I have heard mention recently is that the for loop >> > syntax is potentially harmful to teach because the for loop with an >> > index is inherently only processable with a single thread vs the >> > foreach loop which can potentially use parallel processing. >> >> > I use mostly foreach loops but there is one situation in Java where I >> > seem stuck with using the indexed version. This may be a shortcoming >> > in Java because the order in which iteration is done does not matter. >> >> > The situation is when doing operations which involve copying or >> > deriving values from one array to another array. The foreach loop in >> > Java is fine for taking values out of an array but I don't think as >> > far as I have been able to find out >> > that these loops can be used for receiving values. In this case the >> > for loop with the index is used to get an element out of an array at a >> > given index and the derived value is stored in the destination array >> > at the same index. >> >> > For example: >> >> > Type[] types = Type.values(); >> > char[] mnemonics = char[types.length]; >> > for (int index = 0; index < mnemonics.length; index++) { >> > mnemonics[index] = types[index].getMnemonic(); >> > } >> >> > I come across this situation a fair amount but don't think it can be >> > done in foreach style in Java though I think some kind of closure in >> > other languages would allow the source to be mapped to the destination >> > as I understand it. >> >> > -- >> > You received this message because you are subscribed to the Google Groups >> > "The Java Posse" group. >> > To post to this group, send email to [email protected]. >> > To unsubscribe from this group, send email to >> > [email protected]. >> > For more options, visit this group >> > athttp://groups.google.com/group/javaposse?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "The Java Posse" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/javaposse?hl=en. > > -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
