Arrays methods

Brian Goetz brian.goetz at oracle.com
Thu Mar 14 14:54:55 PDT 2013


Here's what these would look like:

New SAMS: {Int,Long,Double}To{Int,Long,Double}Function

New Arrays methods:

     /**
      * Initialize all elements of the specified array, using the 
provided generator function to compute each element.
      * @param array Array to be initialized
      * @param generator Function accepting an index and producing the 
desired value for that position
      * @param <T> Type of elements of the array
      */
     public<T> void setAll(T[] array, IntFunction<T> generator) {
         Streams.intRange(0, array.length).forEach(i -> { array[i] = 
generator.apply(i); });
     }

     /**
      * Initialize all elements of the specified array, in parallel, 
using the provided generator function to
      * compute each element.
      * @param array Array to be initialized
      * @param generator Function accepting an index and producing the 
desired value for that position
      * @param <T> Type of elements of the array
      */
     public<T> void parallelSetAll(T[] array, IntFunction<T> generator) {
         Streams.intRange(0, array.length).parallel().forEach(i -> { 
array[i] = generator.apply(i); });
     }

     /**
      * Initialize all elements of the specified array, using the 
provided generator function to compute each element.
      * @param array Array to be initialized
      * @param generator Function accepting an index and producing the 
desired value for that position
      */
     public void setAll(int[] array, IntUnaryOperator generator) {
         Streams.intRange(0, array.length).forEach(i -> { array[i] = 
generator.applyAsInt(i); });
     }

     /**
      * Initialize all elements of the specified array, in parallel, 
using the provided generator function to
      * compute each element.
      * @param array Array to be initialized
      * @param generator Function accepting an index and producing the 
desired value for that position
      */
     public<T> void parallelSetAll(int[] array, IntUnaryOperator 
generator) {
         Streams.intRange(0, array.length).parallel().forEach(i -> { 
array[i] = generator.applyAsInt(i); });
     }

     /**
      * Initialize all elements of the specified array, using the 
provided generator function to compute each element.
      * @param array Array to be initialized
      * @param generator Function accepting an index and producing the 
desired value for that position
      */
     public void setAll(long[] array, IntToLongFunction generator) {
         Streams.intRange(0, array.length).forEach(i -> { array[i] = 
generator.applyAsLong(i); });
     }

     /**
      * Initialize all elements of the specified array, in parallel, 
using the provided generator function to
      * compute each element.
      * @param array Array to be initialized
      * @param generator Function accepting an index and producing the 
desired value for that position
      */
     public<T> void parallelSetAll(long[] array, IntToLongFunction 
generator) {
         Streams.intRange(0, array.length).parallel().forEach(i -> { 
array[i] = generator.applyAsLong(i); });
     }

     /**
      * Initialize all elements of the specified array, using the 
provided generator function to compute each element.
      * @param array Array to be initialized
      * @param generator Function accepting an index and producing the 
desired value for that position
      */
     public void setAll(double[] array, IntToDoubleFunction generator) {
         Streams.intRange(0, array.length).forEach(i -> { array[i] = 
generator.applyAsDouble(i); });
     }

     /**
      * Initialize all elements of the specified array, in parallel, 
using the provided generator function to
      * compute each element.
      * @param array Array to be initialized
      * @param generator Function accepting an index and producing the 
desired value for that position
      */
     public<T> void parallelSetAll(double[] array, IntToDoubleFunction 
generator) {
         Streams.intRange(0, array.length).parallel().forEach(i -> { 
array[i] = generator.applyAsDouble(i); });
     }


On 3/14/2013 12:26 PM, Brian Goetz wrote:
> Next roadblock: no suitable SAMs for int -> long, int -> double.
>
> Add?
>
> On 3/13/2013 6:39 PM, Mike Duigou wrote:
>> Yes
>>
>>
>>
>> On 2013-03-13, at 14:44, Brian Goetz <brian.goetz at oracle.com> wrote:
>>
>>> Fill implies "set all elements"; a set name would probably have to
>>> say "setAll":
>>>
>>>   Arrays.setAll(array, fn)
>>>   Arrays.parallelSetAll(array, fn)
>>>
>>> OK?
>>>
>>> On 3/13/2013 5:30 PM, Joe Bowbeer wrote:
>>>> I agree with the critique of 'fill' names.
>>>>
>>>> I like 'set' names.
>>>>
>>>>
>>>>
>>>>
>>>> On Wed, Mar 13, 2013 at 1:28 PM, Mike Duigou <mike.duigou at oracle.com
>>>> <mailto:mike.duigou at oracle.com>> wrote:
>>>>
>>>>     Arrays.indexFill(array, fn)
>>>>     Arrays.indexedFill(array, fn)
>>>>     Arrays.fillIndexed(array, fn)
>>>>     Arrays.indexedSet(array, fn)
>>>>
>>>>     I think it might be better to stay away from "fill" names because
>>>>     the current fill methods all have the property that every array
>>>>     element is assigned the same value. This new operation allows a
>>>>     different value to be assigned to each element.
>>>>
>>>>     Mike
>>>>
>>>>     On Mar 13 2013, at 12:25 , Brian Goetz wrote:
>>>>
>>>>      >> If we added
>>>>      >>
>>>>      >>      <T> void fill(T[], IntFunction<T> gen)
>>>>      >>
>>>>      >> then existing calls to
>>>>      >>
>>>>      >>   fill(array, null)
>>>>      >>
>>>>      >> would become ambiguous.  Doh.  (But the other 17 forms are not
>>>>      >> problematic.)
>>>>      >>
>>>>      >> Any suggestions for alternate names?
>>>>      >
>>>>      > Arrays.generate(array, fn)
>>>>      > Arrays.fillApplying(array, fn)
>>>>      > Arrays.initialize(array, fn)
>>>>      > Arrays.setAll(array, fn)
>>>>      >
>>>>      > ...
>>>>
>>>>


More information about the lambda-libs-spec-experts mailing list