Doing One Operation on All Array Members (Without Iteration)

Discussion in 'Java' started by Hal Vaughan, Sep 8, 2006.

  1. Hal Vaughan

    Hal Vaughan Guest

    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
     
    Hal Vaughan, Sep 8, 2006
    #1
    1. Advertising

  2. -----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-----
     
    Hendrik Maryns, Sep 8, 2006
    #2
    1. Advertising

  3. Hi,

    There is a forAllDo(Collection c, Closure c) in Apache Commons
    CollectionUtils that might help you, but I am quite positive it ends up
    iterating the collection anyway..just a little more fancy ;-)

    http://jakarta.apache.org/commons/c...ache/commons/collections/CollectionUtils.html

    -cheers,
    Manish


    Hal Vaughan wrote:
    > 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
     
    Manish Pandit, Sep 8, 2006
    #3
  4. Hal Vaughan

    M.J. Dance Guest

    Hendrik Maryns wrote:

    > 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>
     
    M.J. Dance, Sep 8, 2006
    #4
  5. Hal Vaughan

    M.J. Dance Guest

    M.J. Dance wrote:
    > Appart from utilizing multiple threads / CPUs to do the job faster is
    > possible,


    Make that _if_ possible.
     
    M.J. Dance, Sep 8, 2006
    #5
  6. Hal Vaughan

    Daniel Dyer Guest

    On Fri, 08 Sep 2006 08:54:22 +0100, Hal Vaughan <>
    wrote:

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

    --
    Daniel Dyer
    http://www.dandyer.co.uk
     
    Daniel Dyer, Sep 8, 2006
    #6
  7. Hal Vaughan

    Stefan Ram Guest

    Hal Vaughan <> writes:
    >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.
     
    Stefan Ram, Sep 8, 2006
    #7
  8. Hal Vaughan

    Oliver Wong Guest

    "M.J. Dance" <> wrote in message
    news:17bMg.4874$...
    > Hendrik Maryns wrote:
    >
    >> 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
     
    Oliver Wong, Sep 8, 2006
    #8
  9. Hal Vaughan

    M.J. Dance Guest

    Oliver Wong wrote:

    >> 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.
     
    M.J. Dance, Sep 11, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    3
    Views:
    819
    Natty Gur
    Aug 28, 2003
  2. Replies:
    5
    Views:
    423
    Steven Bethard
    Mar 3, 2006
  3. david ullua
    Replies:
    13
    Views:
    669
  4. raan
    Replies:
    2
    Views:
    452
  5. Rudi
    Replies:
    5
    Views:
    5,030
Loading...

Share This Page