How to implement `range ()`

Introduction

How do I write a public static range method or Range Class in Java 6 so that it at least encompasses common functions implemented in other programming languages?

If you want to answer a question, you can simply ignore the following.


ABOUT

From time to time, I miss the functionality that other languages ​​have. I seem to have a different coding style for any language I write, and I don't want to change this habit. Therefore, if I want to reuse an algorithm that I wrote in another language, then I have to do annoying little hacks or workarounds to cover the missing functionality. I would like to find a permanent and effective fix for range().

For me, the method range()returns an input range, perhaps in a lazy way and has default values. At any moment, he has a beginning, an end condition, and a way to get the next element. It should work perfectly in for each cycle , as well as outside of it.

Notes

- , Google Guava . , , , JDK .

alt text

, :

  • by How to write - , .
  • loop code,
  • : P

, , , , BigInteger Joda DateTime.

  • for-each
  • generic, typafe
  • ables , :.cycle(),.filter(),.all(),.any(), transform()

range() :

/**TODO: Think about why / how to add step, limit, offset and cycle detection. */
public static <T extends Comparable<T>> Iterable<T> //
range(T from, Predicate<T> to, Function<T, T> fNext) //
        throws NullPointerException, IllegalArgumentException {

, Range Builder.

Scala, Python 3, Groovy, .Net ( linq) [#, f #, vb ++], ruby ​​, PHP... .

, ( ).

public static <T extends Comparable<T>> Iterable<T> //
range(T from, Predicate<T> to, Function<T, T> fNext) //
        throws NullPointerException {
    Preconditions.checkNotNull(from);
    Preconditions.checkNotNull(to);
    Preconditions.checkNotNull(fNext);

    T current = from;
    ArrayList<T> result = Lists.newArrayList();
    if (to.apply(current)) result.add(current);
    while (to.apply(current = Preconditions.checkNotNull(fNext.apply(current))))
        result.add(current);
    return result;
}

//eats first element
public static <T extends Comparable<T>> Iterator<T> //
range2(final T from, final Predicate<T> to, final Function<T, T> fNext)
        throws NullPointerException, UnsupportedOperationException {
    Preconditions.checkNotNull(from);
    Preconditions.checkNotNull(to);
    Preconditions.checkNotNull(fNext);
    return new Iterator<T>() {
        T current = from;
        @Override public boolean hasNext() {return to.apply(current);}
        @Override public T next() {return current = Preconditions.checkNotNull(fNext.apply(current));}
        @Override public void remove() {throw new UnsupportedOperationException();}
    };
}
+3
2

, , :

  public static Function<Integer, Integer> intIncrementer(final int step) {
    class IntIncrementer implements Function<Integer, Integer> {
      private final int _step = step;

      @Override public Integer apply(Integer i) {
        return i + _step;
      }

      @Override public boolean equals(Object obj) {
        return (obj instanceof IntIncrementer) 
           && ((IntIncrementer) obj)._step == _step;
      }

      @Override public int hashCode() {
        return _step;
      }
    }

    return new IntIncrementer();
  }

i + _step , , (BigInteger, long ..)

<T extends Comparable<T>> <T>. .

Iterable . , Range, , .

public interface Range<T> extends Iterable<T> {
  // TODO: write the terms of the contract
  @Override public boolean equals(Object obj);
  @Override public int hashCode();
  // TODO: other useful methods?
}

, - . for.

+1

. :

interface Steppable<T>{
    T defaultStep(); //Returns 1 for most number types
    T value(); //Returns the value itself
    Steppable<T> step(T amount); //the stepping logic
}

, (ints, longs, date ..). range() .

, . , .

0

Source: https://habr.com/ru/post/1779710/


All Articles