Doing One Operation on All Array Members (Without Iteration)

H

Hal Vaughan

I don't think this is possible, but I figure it won't hurt to ask. I just
might learn something new.

I have an object I've created with a boolean isActive. I can set it my
setting the field directly (myObject.isActive = true) or with a setter
(myObject.setActive(true)). I will be working with an array, like this:

myObject[]

Is there any way to do the same operation on all members of that array
without using a loop to iterate through each item in the array? In this
case, I'd like to set all the members to active at once. It would be like
doing:

myObject[].setActive(true)


I don't think this is possible, but is there a way to do it?

Thanks!

Hal
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hal Vaughan schreef:
I don't think this is possible, but I figure it won't hurt to ask. I just
might learn something new.

I have an object I've created with a boolean isActive. I can set it my
setting the field directly (myObject.isActive = true) or with a setter
(myObject.setActive(true)). I will be working with an array, like this:

myObject[]

Is there any way to do the same operation on all members of that array
without using a loop to iterate through each item in the array? In this
case, I'd like to set all the members to active at once. It would be like
doing:

myObject[].setActive(true)


I don't think this is possible, but is there a way to do it?

How would you imagine this to work? Suppose, just for a moment, that

myObject[].setActive(true)

were defined. Then how would it be supposed to work? Unless you have a
multi-CPU machine, it will have to go through the array sequentially
anyway, so you might as well do the looping yourself, and do
null-checking as well in the meantime.

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 v1.4.2 (GNU/Linux)

iD8DBQFFASzae+7xMGD3itQRAiXiAJ9U74Du0JXcY1qZsca4MovNNdq5rACcC0o0
UsIICl4kQhIoBCZVKtRT3k0=
=zJLJ
-----END PGP SIGNATURE-----
 
M

M.J. Dance

Hendrik said:
Hal Vaughan schreef:
I don't think this is possible, but I figure it won't hurt to ask. I just
might learn something new.

I have an object I've created with a boolean isActive. I can set it my
setting the field directly (myObject.isActive = true) or with a setter
(myObject.setActive(true)). I will be working with an array, like this:

myObject[]

Is there any way to do the same operation on all members of that array
without using a loop to iterate through each item in the array? In this
case, I'd like to set all the members to active at once. It would be like
doing:

myObject[].setActive(true)


I don't think this is possible, but is there a way to do it?

How would you imagine this to work? Suppose, just for a moment, that

myObject[].setActive(true)

were defined. Then how would it be supposed to work? Unless you have a
multi-CPU machine, it will have to go through the array sequentially
anyway, so you might as well do the looping yourself, and do
null-checking as well in the meantime.

Appart from utilizing multiple threads / CPUs to do the job faster is possible,
the main point is to remove unnecessary code, thereby making it shorter,
simpler, easyer to read and, in final consequence, getting rid of possible
hiding places for bugs.

Of course, implementing (& using) such a feature could be problematic. For
example, having an array containing instances of objects not all of which have a
..setActive(boolean) method. OK, an OperationNotSupportedException could be
thrown, which means one would have to catch it somewhere and those few lines of
code we were trying to get rid of, are here again. So I guess it's down to
generics: making sure, that all the entries are suitable.

<WishfullThinking>
Come to think of it, Mathematica has some powerful features related to such a
notion: http://documents.wolfram.com/mathematica/functions/Map (see also Map*,
Apply and Operate on the same page). I hear Java is eyeing closures for one its
future versions - maybe it could come with Map*, Apply and/or Operate at the
same time.
</WishfullThinking>
 
D

Daniel Dyer

I don't think this is possible, but I figure it won't hurt to ask. I
just
might learn something new.

I have an object I've created with a boolean isActive. I can set it my
setting the field directly (myObject.isActive = true) or with a setter
(myObject.setActive(true)). I will be working with an array, like this:

myObject[]

Is there any way to do the same operation on all members of that array
without using a loop to iterate through each item in the array? In this
case, I'd like to set all the members to active at once. It would be
like
doing:

myObject[].setActive(true)


I don't think this is possible, but is there a way to do it?

What you are probably looking for is something like the functional
programming concept of "higher-order functions" (in particular the map
function of languages like Haskell and Miranda). Java doesn't really do
this (although something similar can be hacked with objects, as it seems
to have been in the Jakarta Commons link posted by Manish). Regardless,
this just hides the looping from you, something under the covers will
still need to make sure that the operation is applied to each element.

Joel had an interesting article on first class functions a few weeks ago
(http://www.joelonsoftware.com/items/2006/08/01.html). The article on
Java that he links to, "Execution in the Kingdom of the Nouns"
(http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html),
is also well worth reading.

Dan.
 
S

Stefan Ram

Hal Vaughan said:
myObject[].setActive(true)

An approximation would be as follows:

interface Operation<Domain>{ void of( Domain value ); }

class Thing
{ public void setActive( final java.lang.Boolean booleanValue )
{ java.lang.System.out.println( "setActive." ); }}

class Array<T>
{ final T[] value;
Array( final T[] value ){ this.value = value; }
public void map( final Operation<T> operation )
{ for( final T t : value )operation.of( t ); }}

public class Main
{
static final Array<Thing> things = new Array<Thing>
( new Thing[]{ new Thing(), new Thing(), new Thing() });

public static void main( final java.lang.String[] commandLineArguments )
{
things.map( new Operation<Thing>()
{ public void of( final Thing thing ){ thing.setActive( true ); }}); }}

setActive.
setActive.
setActive.
 
O

Oliver Wong

M.J. Dance said:
Hendrik said:
Hal Vaughan schreef:
I don't think this is possible, but I figure it won't hurt to ask. I
just
might learn something new.

I have an object I've created with a boolean isActive. I can set it my
setting the field directly (myObject.isActive = true) or with a setter
(myObject.setActive(true)). I will be working with an array, like this:

myObject[]

Is there any way to do the same operation on all members of that array
without using a loop to iterate through each item in the array? In this
case, I'd like to set all the members to active at once. It would be
like
doing:

myObject[].setActive(true)


I don't think this is possible, but is there a way to do it?

How would you imagine this to work? Suppose, just for a moment, that

myObject[].setActive(true)

were defined. Then how would it be supposed to work? Unless you have a
multi-CPU machine, it will have to go through the array sequentially
anyway, so you might as well do the looping yourself, and do
null-checking as well in the meantime.

Appart from utilizing multiple threads / CPUs to do the job faster is
possible, the main point is to remove unnecessary code, thereby making it
shorter, simpler, easyer to read and, in final consequence, getting rid of
possible hiding places for bugs.

Of course, implementing (& using) such a feature could be problematic. For
example, having an array containing instances of objects not all of which
have a .setActive(boolean) method. OK, an OperationNotSupportedException
could be thrown, which means one would have to catch it somewhere and
those few lines of code we were trying to get rid of, are here again. So I
guess it's down to generics: making sure, that all the entries are
suitable.

Presumably, myObject has a type, and the operations you could perform on
myObject[] would be limited to those defined on the type of myObject.

List[] myLists = /*initialize somehow*/

myLists.toString(); /*Calls the method defined on the array*/
myLists[].toString(); /*Calls the method n times, on each element in
myList*/

myLists[].add(null); /*This works, as add(Object) is defined in the
interface List*/
myLists[].dance(); /*Compile time error, as the interface List doesn't
define a method "dance"*/

- Oliver
 
M

M.J. Dance

Oliver said:
Of course, implementing (& using) such a feature could be problematic.
For example, having an array containing instances of objects not all
of which have a .setActive(boolean) method. OK, an
OperationNotSupportedException could be thrown, which means one would
have to catch it somewhere and those few lines of code we were trying
to get rid of, are here again. So I guess it's down to generics:
making sure, that all the entries are suitable.

Presumably, myObject has a type, and the operations you could perform
on myObject[] would be limited to those defined on the type of myObject.

List[] myLists = /*initialize somehow*/

myLists.toString(); /*Calls the method defined on the array*/
myLists[].toString(); /*Calls the method n times, on each element in
myList*/

myLists[].add(null); /*This works, as add(Object) is defined in the
interface List*/
myLists[].dance(); /*Compile time error, as the interface List doesn't
define a method "dance"*/

Sure. But consider this.

List<Perform> list = ...;
Object[] array = list.toArray();

Although Perform could implement .dance(), one couldn't call array.dance(),
because Object (and thus Object[]) doesn't.

There are many such cases in Java API. Until generics, those seemed reasonable.
Now we have a strange mixture.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top