/**
  * Returns a composed {@link BiIntToFloatFunction} that first applies the {@code before} functions
  * to its input, and then applies this function to the result. If evaluation of either operation
  * throws an exception, it is relayed to the caller of the composed operation. This method is just
  * convenience, to provide the ability to execute an operation which accepts {@code int} input,
  * before this primitive function is executed.
  *
  * @param before1 The first function to apply before this function is applied
  * @param before2 The second function to apply before this function is applied
  * @return A composed {@code BiIntToFloatFunction} that first applies the {@code before} functions
  *     to its input, and then applies this function to the result.
  * @throws NullPointerException If given argument is {@code null}
  * @implSpec The input argument of this method is a able to handle primitive values. In this case
  *     this is {@code int}.
  */
 @Nonnull
 default BiIntToFloatFunction composeFromInt(
     @Nonnull final IntFunction<? extends T> before1, @Nonnull final IntToDoubleFunction before2) {
   Objects.requireNonNull(before1);
   Objects.requireNonNull(before2);
   return (value1, value2) -> applyAsFloat(before1.apply(value1), before2.applyAsDouble(value2));
 }
Example #2
0
 public static <T> T[] apply(Class<T> type, int count, IntFunction<T> generator) {
   T[] array = (T[]) Array.newInstance(type, count);
   for (int i = 0; i < count; i++) {
     array[i] = generator.apply(i);
   }
   return array;
 }
Example #3
0
 /**
  * Returns a composed {@link BiIntPredicate} that first applies the {@code before} functions to
  * its input, and then applies this predicate to the result. If evaluation of either operation
  * throws an exception, it is relayed to the caller of the composed operation. This method is just
  * convenience, to provide the ability to execute an operation which accepts {@code int} input,
  * before this primitive predicate is executed.
  *
  * @param before1 The first function to apply before this predicate is applied
  * @param before2 The second function to apply before this predicate is applied
  * @return A composed {@code BiIntPredicate} that first applies the {@code before} functions to
  *     its input, and then applies this predicate to the result.
  * @throws NullPointerException If given argument is {@code null}
  * @implSpec The input argument of this method is a able to handle primitive values. In this case
  *     this is {@code int}.
  */
 @Nonnull
 default BiIntPredicate composeFromInt(
     @Nonnull final IntFunction<? extends T> before1, @Nonnull final IntToCharFunction before2) {
   Objects.requireNonNull(before1);
   Objects.requireNonNull(before2);
   return (value1, value2) -> test(before1.apply(value1), before2.applyAsChar(value2));
 }
  private static long timer(int n, IntFunction<Long> aMethod) {

    long startTime = System.nanoTime();
    long returnLong = aMethod.apply(n);
    long duration = System.nanoTime() - startTime;
    System.out.print("Elapsed duration: " + duration + " (ns). Result =  ");
    return returnLong;
  }
Example #5
0
 /**
  * Returns a composed {@link TriIntConsumer} that first applies the {@code before} functions to
  * its input, and then applies this consumer to the result. If evaluation of either operation
  * throws an exception, it is relayed to the caller of the composed operation. This method is just
  * convenience, to provide the ability to execute an operation which accepts {@code int} input,
  * before this primitive consumer is executed.
  *
  * @param before1 The first function to apply before this consumer is applied
  * @param before2 The second operator to apply before this consumer is applied
  * @param before3 The third operator to apply before this consumer is applied
  * @return A composed {@code TriIntConsumer} that first applies the {@code before} functions to
  *     its input, and then applies this consumer to the result.
  * @throws NullPointerException If given argument is {@code null}
  * @implSpec The input argument of this method is a able to handle primitive values. In this case
  *     this is {@code int}.
  */
 @Nonnull
 default TriIntConsumer composeFromInt(
     @Nonnull final IntFunction<? extends T> before1,
     @Nonnull final IntUnaryOperator before2,
     @Nonnull final IntUnaryOperator before3) {
   Objects.requireNonNull(before1);
   Objects.requireNonNull(before2);
   Objects.requireNonNull(before3);
   return (value1, value2, value3) ->
       accept(before1.apply(value1), before2.applyAsInt(value2), before3.applyAsInt(value3));
 }
 /**
  * Returns a composed {@link TriIntToCharFunction} that first applies the {@code before} functions
  * to its input, and then applies this function to the result. If evaluation of either operation
  * throws an exception, it is relayed to the caller of the composed operation. This method is just
  * convenience, to provide the ability to execute an operation which accepts {@code int} input,
  * before this primitive function is executed.
  *
  * @param before1 The first function to apply before this function is applied
  * @param before2 The second function to apply before this function is applied
  * @param before3 The third function to apply before this function is applied
  * @return A composed {@code TriIntToCharFunction} that first applies the {@code before} functions
  *     to its input, and then applies this function to the result.
  * @throws NullPointerException If given argument is {@code null}
  * @implSpec The input argument of this method is a able to handle primitive values. In this case
  *     this is {@code int}.
  */
 @Nonnull
 default TriIntToCharFunction composeFromInt(
     @Nonnull final IntFunction<? extends T> before1,
     @Nonnull final IntToByteFunction before2,
     @Nonnull final IntToByteFunction before3) {
   Objects.requireNonNull(before1);
   Objects.requireNonNull(before2);
   Objects.requireNonNull(before3);
   return (value1, value2, value3) ->
       applyAsChar(
           before1.apply(value1), before2.applyAsByte(value2), before3.applyAsByte(value3));
 }
  private <E extends RuntimeException> void assertIntFunction(
      IntFunction<Object> test, Class<E> type) {
    assertNotNull(test);
    try {
      test.apply(0);
      fail();
    } catch (RuntimeException e) {
      assertException(type, e, "0");
    }

    try {
      IntStream.of(1, 2, 3).mapToObj(test);
    } catch (RuntimeException e) {
      assertException(type, e, "1");
    }
  }
  public static <E, C extends Collection<E>> ArrayBuilderFactory<E, C> collection(
      IntFunction<C> collectionFactory) {
    return length -> {
      C collection = collectionFactory.apply(length);
      return new ArrayBuilderFactory.Builder<E, C>() {
        @Override
        public void add(@Nullable E element) {
          collection.add(element);
        }

        @Override
        public C build() {
          return collection;
        }
      };
    };
  }
  public static <E> ArrayBuilderFactory<E, E[]> array(IntFunction<E[]> arrayFactory) {
    return length -> {
      E[] array = arrayFactory.apply(length);
      return new ArrayBuilderFactory.Builder<E, E[]>() {
        private int i = 0;

        @Override
        public void add(@Nullable E element) {
          array[i++] = element;
        }

        @Override
        public E[] build() {
          return array;
        }
      };
    };
  }
Example #10
0
 /**
  * Obtains an instance with entries filled using a function.
  *
  * <p>The function is passed the row index, returning the column values.
  *
  * @param rows the number of rows
  * @param columns the number of columns
  * @param valuesFunction the function used to populate the values
  * @return a matrix initialized using the function
  */
 public static DoubleMatrix ofArrays(int rows, int columns, IntFunction<double[]> valuesFunction) {
   if (rows == 0 || columns == 0) {
     return EMPTY;
   }
   double[][] array = new double[rows][columns];
   for (int i = 0; i < array.length; i++) {
     double[] values = valuesFunction.apply(i);
     if (values.length != columns) {
       throw new IllegalArgumentException(
           Messages.format(
               "Function returned array of incorrect length {}, expected {}",
               values.length,
               columns));
     }
     array[i] = values.clone();
   }
   return new DoubleMatrix(array, rows, columns);
 }