Class Iterables

java.lang.Object
com.ontology2.pidove.iterables.Iterables

public class Iterables extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <X> Iterable<X>
    accumulate(BinaryOperator<X> func, Iterable<X> value)
    Like the accumulate function from Python's itertools.
    static boolean
    all(Iterable<Boolean> values)
    Imitates the "all" function from Python.
    static boolean
    any(Iterable<Boolean> values)
    Imitates the "any" function from Python.
    static <X> List<X>
    asList(Iterable<X> values)
    Imitates the toList method of the Java Streams API
    static <X, Y> Map<X,Y>
    asMap(Iterable<Pair<X,Y>> pairs)
     
    static <X> Set<X>
    asSet(Iterable<X> values)
    Imitates the toSet method in the Java Streams API
    static <X> X
    at(int index, Iterable<X> values)
    Like the indexing operator in Python.
    static <X> X
    at(int index, X[] values)
    Like the indexing operator in Python.
    static void
    close(Object that)
    Given an arbitrary object, calls the close method if it is AutoCloseable, otherwise do nothing
    static <X, Y, Z> Z
    collect(Collector<? super X,Y,Z> collector, Iterable<X> values)
    Imitates the collect() method in the streams API
    static <X> Iterable<X>
    compress(Iterable<Boolean> filter, Iterable<X> value)
    Like the compress function from Python's itertools.
    static <X> TidyIterable<X>
    concat(Iterable<X>... values)
    Imitates the "concat" function in the Java Streams API.
    static <X> long
    count(Iterable<X> values)
    Imitates the "count" method in the Java Streams API
    static <X> TidyIterable<X>
    cycle(long times, Iterable<X> values)
    Like the cycle function from Python's itertools but you can specify how many cycles you want
    static <X> TidyIterable<X>
    cycle(Iterable<X> values)
    Based on the cycle function from the Python itertools modules
    static <X> Set<X>
    distinct(Iterable<X> values)
    Imitates the "distinct" method in the java streams API.
    static <X> TidyIterable<X>
    dropWhile(Predicate<? super X> predicate, Iterable<X> values)
    Imitates the "dropWhile" method in the java streams API.
    static Iterable<?>
    Imitates the "empty" method in the Java Streams API
    static <X> Iterable<Pair<Long,X>>
    enumerate(Iterable<X> values)
    Based on Python's enumerate() function, which given an iterable that returns A, B, C, ...
    static <X> Iterable<Pair<Long,X>>
    enumerate(Long start, Iterable<X> values)
    Based on Python's enumerate() function, which given an iterable that returns A, B, C, ...
    static <X> TidyIterable<X>
    filter(Predicate<? super X> predicate, Iterable<X> values)
    Imitates the filter method in the Streams API
    static <X> Iterable<X>
    filterFalse(Predicate<X> p, Iterable<X> values)
    Like the filterfalse function from Python's itertools.
    static <X> Optional<X>
    first(Iterable<X> values)
    Imitates the "first" method in the Java Stream API
    static <X, Y> TidyIterable<Y>
    flatMap(Function<X,? extends Iterable<Y>> fn, Iterable<X> values)
    Imitates the flatmap method in the Java Streams API
    static <X> TidyIterable<X>
    flatten(Iterable<? extends Iterable<X>> values)
     
    static <X> void
    forEach(Consumer<? super X> action, Iterable<X> values)
    Imitates the forEach/forEachOrdered methods of the Java Stream API since pidove does an ordered iteration.
    static <X> Iterable<X>
    Similar to the generate method in the Java Streams API but sane.
    static <X> Iterable<X>
    iterate(X seed, UnaryOperator<X> f)
    Imitates the iterate method of the Java Strsams API.
    static String
    joinOn(CharSequence separator, Iterable<? extends CharSequence> values)
    Unique to pidove.
    static int
    Similar to the len function in Python.
    static <T> int
    len(Iterable<T> i)
    If the iterable implements collections, this method uses the size method of the collection, otherwise it counts the members.
    static <T> int
    len(T[] a)
     
    static <X> TidyIterable<X>
    limit(int amount, Iterable<X> values)
    Imitates the limit method of the Java Streams API
    static <X, Y> TidyIterable<Y>
    map(Function<X,Y> fn, Iterable<X> values)
    Imitates the map method of the Java Streams API
    static <X extends Comparable<X>>
    Optional<X>
    max(Iterable<X> values)
    Similar to the max method of the Java Streams API but uses the natural ordering for convenience.
    static <X> Optional<X>
    max(Iterable<X> values, Comparator<X> comparator)
    Imitates the max method of the Java Streams API
    static <X extends Comparable<X>>
    Optional<X>
    min(Iterable<X> values)
    Similar to the min method of the Java Streams API but uses the natural ordering for convenience.
    static <X> Optional<X>
    min(Comparator<X> comparator, Iterable<X> values)
    Imitates the min method of the Java Streams API
    static boolean
    Similar to the noneMatch function of the Java Streams API but with a pythonic signature.
    static <X> Iterable<X>
    of(X x)
    Imitates the of method of the Java Streams API
    static <X> Iterable<X>
    of(X... x)
    Imitates the of method of the Java Streams API
    static <X> Iterable<X>
    Imitates the ofNullable method of the java streams API
    over(com.ontology2.pidove.iterables.Iterables.SupplierOfBufferedReader source)
    Unique to pidove Given a Supplier of an Input Stream, opens the stream and iterates over lines in the file.
    over(com.ontology2.pidove.iterables.Iterables.SupplierOfInputStream source)
    Unique to pidove Given a Supplier of an Input Stream, opens the stream and iterates over lines in the file.
    Unique to pidove
    over(Path path)
    Unique to pidove Given a path, opens the file in character mode and returns an iterable of the lines in the file
    static <X, Y> Iterable<Pair<X,Y>>
    over(Map<X,Y> that)
    Unique to pidove.
    static <X> Iterable<X>
    over(X[] x)
    Unique to pidove
    static <X> Iterable<Pair<X,X>>
    pairwise(Iterable<X> values)
    Like the pairwise function from Python's itertools.
    static <X> TidyIterable<X>
    peek(Consumer<X> listener, Iterable<X> values)
    Imitates the peek method from the Java Streams API
    static <X, Y> Iterable<Pair<X,Y>>
    product(Iterable<X> left, Iterable<Y> right)
    Generates the Cartesian product of two iterators.
    static <X, Y, Z> Iterable<Trio<X,Y,Z>>
    product(Iterable<X> left, Iterable<Y> middle, Iterable<Z> right)
    Generates the Cartesian product of three iterators.
    static Iterable<Long>
    range(long stop)
    This is like the range function from the Python standard library.
    static Iterable<Long>
    range(long start, long stop)
    This is like the range function from the Python standard library range(10,12) produces the values 10,11
    static Iterable<Long>
    range(long start, long stop, long skip)
    This is like the range function from the Python standard library
    static <X> Optional<X>
    reduce(BinaryOperator<X> accumulator, Iterable<X> values)
    Imitates the reduce method from the Java Streams API
    static <X> X
    reduce(X identity, BinaryOperator<X> accumulator, Iterable<X> values)
    Imitates the reduce method from the Java Streams API
    static <X> Iterable<X>
    repeat(long times, X value)
    Like the repeat function in Python's itertools
    static <X> Iterable<X>
    reversed(Iterable<X> values)
    Based on the Python reversed() function.
    static <X> TidyIterable<X>
    skip(int amount, Iterable<X> values)
    Imitates the skip method from the Java Streams API
    splitOn(String regex, int limit, String source)
    Unique to pidove
    splitOn(String regex, String source)
    Unique to pidove
    static double
    Convenience method to get sum of Double Iterable
    static int
    Convenience method to get sum of integer Iterable
    static long
    Convenience method to get sum of Long Iterable
    static <X> Iterable<X>
    tail(int amount, Iterable<X> x)
    Note this uses somewhat different algorithms for different cases, since it is easy to pick the last N elements off the end of a RandomAccess List, but quite a hassle in comparison in the generic case where we have to keep a running memory of the last N elements we've seen.
    static <X> TidyIterable<X>
    takeWhile(Predicate<? super X> predicate, Iterable<X> values)
    Imitates the takeWhile method of the Java Streams API Stops iterating when the predicate evaluates false
    static <X, Y> Iterable<Y>
    window(int length, Collector<X,?,Y> collector, Iterable<X> values)
    Unique to pidove, but taking advantage of Collectors from the Stream API Gathers values yielded by values into overlapping groups of length and passes these through the collector (in the case of N=2 and the toList() collector this has an effect similar to the pairwise() method.) Could be used to compute moving averages and other convolutions
    static <X, Y> Iterable<Pair<X,Y>>
    zip(Iterable<X> one, Iterable<Y> two)
    Based on the Python zip function
    static <X, Y, Z> Iterable<Trio<X,Y,Z>>
    zip(Iterable<X> one, Iterable<Y> two, Iterable<Z> three)
    Based on the Python zip function Note that Python's zip function is capable of combining an arbitrary number of iterables into tuples.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Iterables

      public Iterables()
  • Method Details

    • all

      public static boolean all(Iterable<Boolean> values)
      Imitates the "all" function from Python. Stops iterating when the first false value is seen.
      Parameters:
      values - an iterable of boolean values
      Returns:
      true if all values are true, otherwise false
    • any

      public static boolean any(Iterable<Boolean> values)
      Imitates the "any" function from Python. Stops iterating when the first true value is seen
      Parameters:
      values - an iterable of boolean values
      Returns:
      true if any values is true, otherwise false
    • collect

      public static <X, Y, Z> Z collect(Collector<? super X,Y,Z> collector, Iterable<X> values)
      Imitates the collect() method in the streams API
      Type Parameters:
      X - the type of the values in the iterable
      Y - the type of the data structure that the collector store immediate results in
      Z - the type of the result returned by the collector.
      Parameters:
      collector - a collector
      values - an iterable
      Returns:
      the result of the collector, of type Z
    • concat

      @SafeVarargs public static <X> TidyIterable<X> concat(Iterable<X>... values)
      Imitates the "concat" function in the Java Streams API.
      Type Parameters:
      X - the type of the values
      Parameters:
      values - zero or more iterables
      Returns:
      the values in all of the iterables concatenated together
    • count

      public static <X> long count(Iterable<X> values)
      Imitates the "count" method in the Java Streams API
      Type Parameters:
      X - the type of the values in the iterable
      Parameters:
      values - an iterable
      Returns:
      how many values are returned by the iterable
    • distinct

      public static <X> Set<X> distinct(Iterable<X> values)
      Imitates the "distinct" method in the java streams API. Is a little different from other methods because it materializes the set and returns the set which is an Iterable, but it doesn't get updated if the base Iterable changes the way other functions in this class do
      Type Parameters:
      X - the type of values of the iterable
      Parameters:
      values - an iterable
      Returns:
      a set of all distinct values that appear in said iterable
    • dropWhile

      public static <X> TidyIterable<X> dropWhile(Predicate<? super X> predicate, Iterable<X> values)
      Imitates the "dropWhile" method in the java streams API.
      Type Parameters:
      X - the type of the iterable
      Parameters:
      predicate - boolean function of an X value
      values - a set of values
      Returns:
      an iterable that starts at the first point in values where the predicate returns false
    • empty

      public static Iterable<?> empty()
      Imitates the "empty" method in the Java Streams API
      Returns:
      an empty iterable
    • first

      public static <X> Optional<X> first(Iterable<X> values)
      Imitates the "first" method in the Java Stream API
      Type Parameters:
      X - type of values in iterable
      Parameters:
      values - an interable
      Returns:
      an Optional with the first value in the iterable unless the iterable is empty in which case returns Optional.empty()
    • filter

      public static <X> TidyIterable<X> filter(Predicate<? super X> predicate, Iterable<X> values)
      Imitates the filter method in the Streams API
      Type Parameters:
      X - the type of the input iterable
      Parameters:
      predicate - a boolean function of X
      values - an iterable
      Returns:
      an iterable of all values from values where predicate is true
    • flatten

      public static <X> TidyIterable<X> flatten(Iterable<? extends Iterable<X>> values)
      Type Parameters:
      X - the type of the input Iterable
      Parameters:
      values - an Iterable of Iterables
      Returns:
      the values returned by all the inner iterables
    • flatMap

      public static <X, Y> TidyIterable<Y> flatMap(Function<X,? extends Iterable<Y>> fn, Iterable<X> values)
      Imitates the flatmap method in the Java Streams API
      Type Parameters:
      X - the input iterable type
      Y - the output iterable type
      Parameters:
      fn - a function that turns X into an iterable of Y
      values - an iterable
      Returns:
      an iterable with all the values of type Y returned by fn
    • generate

      public static <X> Iterable<X> generate(Supplier<Supplier<X>> source)
      Similar to the generate method in the Java Streams API but sane. That is, instead of taking a Supplier are an argument it takes a Supplier of a Supplier, so that like a normal Iterable it can start the iteration fresh each time
      Type Parameters:
      X - the output type
      Parameters:
      source - a supplier that generates a supplier that supplies values of type X
      Returns:
      values returned by the innter supplier
    • iterate

      public static <X> Iterable<X> iterate(X seed, UnaryOperator<X> f)
      Imitates the iterate method of the Java Strsams API.
      Type Parameters:
      X - type of values in the outut iterable
      Parameters:
      seed - the first value
      f - a function that is applied repeatedly to the seed then it's own output to getnerate the values out of the output iterable
      Returns:
      values generated by that function
    • limit

      public static <X> TidyIterable<X> limit(int amount, Iterable<X> values)
      Imitates the limit method of the Java Streams API
      Type Parameters:
      X - type of values in iterable
      Parameters:
      amount - how many values to take
      values - an iterable
      Returns:
      the first amount values of the iterable
    • map

      public static <X, Y> TidyIterable<Y> map(Function<X,Y> fn, Iterable<X> values)
      Imitates the map method of the Java Streams API
      Type Parameters:
      X - input iterable type
      Y - output iterable type
      Parameters:
      fn - a function from X to Y
      values - an iterable
      Returns:
      an iterable produced by applying fn to all values of the input iterable
    • max

      public static <X> Optional<X> max(Iterable<X> values, Comparator<X> comparator)
      Imitates the max method of the Java Streams API
      Type Parameters:
      X - the type of values in the input iterable
      Parameters:
      values - an iterable
      comparator - a comparison function over the iterable
      Returns:
      the maximum value determined by that comparison function
    • max

      public static <X extends Comparable<X>> Optional<X> max(Iterable<X> values)
      Similar to the max method of the Java Streams API but uses the natural ordering for convenience.
      Type Parameters:
      X - the type of values in the input iterable, bounded above by Comparable
      Parameters:
      values - an iterable of comparable values
      Returns:
      the maximum value determined by the natural order over X
    • min

      public static <X> Optional<X> min(Comparator<X> comparator, Iterable<X> values)
      Imitates the min method of the Java Streams API
      Type Parameters:
      X - the type of values in the input iterable
      Parameters:
      values - an iterable
      comparator - a comparison function over the iterable
      Returns:
      the minimum value determined by that comparison function
    • min

      public static <X extends Comparable<X>> Optional<X> min(Iterable<X> values)
      Similar to the min method of the Java Streams API but uses the natural ordering for convenience.
      Type Parameters:
      X - the type of values in the input iterable, bounded above by Comparable
      Parameters:
      values - an iterable of comparable values
      Returns:
      the minimum value determined by the natural order over X
    • none

      public static boolean none(Iterable<Boolean> values)
      Similar to the noneMatch function of the Java Streams API but with a pythonic signature. Stops iterating and Returns early if any value is true.
      Parameters:
      values - an iterable of boolean values
      Returns:
      true if all values are false, otherwise false
    • of

      public static <X> Iterable<X> of(X x)
      Imitates the of method of the Java Streams API
      Type Parameters:
      X - the type of values in the output iterable
      Parameters:
      x - a value of type X
      Returns:
      an iterable that has only value x in it
    • of

      @SafeVarargs public static <X> Iterable<X> of(X... x)
      Imitates the of method of the Java Streams API
      Type Parameters:
      X - type of the output iterable
      Parameters:
      x - an arbitrary number of values of type X
      Returns:
      those values in an iterable
    • ofNullable

      public static <X> Iterable<X> ofNullable(X x)
      Imitates the ofNullable method of the java streams API
      Type Parameters:
      X - the type of the output iterable
      Parameters:
      x - a value
      Returns:
      a single item iterable with that value unless the value is null in which case an empty iterable
    • over

      public static Iterable<Character> over(CharSequence s)
      Unique to pidove
      Parameters:
      s - a CharSequence (ex. a String)
      Returns:
      the characters in the CharSequence as an Iterable
    • over

      public static <X> Iterable<X> over(X[] x)
      Unique to pidove
      Type Parameters:
      X - type of the output iterable
      Parameters:
      x - an array
      Returns:
      an iterable of the values in the array
    • over

      public static <X, Y> Iterable<Pair<X,Y>> over(Map<X,Y> that)
      Unique to pidove. Similar to iterating over the items() of a map but returns sane immutable pairs that have nothing to do with the internals of the Map
      Type Parameters:
      X - type of the keys
      Y - type of the pairs
      Parameters:
      that - a Map
      Returns:
      key-value pairs
    • over

      public static Iterable<String> over(Path path)
      Unique to pidove Given a path, opens the file in character mode and returns an iterable of the lines in the file
      Parameters:
      path - a path to the file system
      Returns:
      lines in the file
    • over

      public static Iterable<String> over(com.ontology2.pidove.iterables.Iterables.SupplierOfInputStream source)
      Unique to pidove Given a Supplier of an Input Stream, opens the stream and iterates over lines in the file. Note that if we used a Supplier<InputStream> here we couldn't also make an over method that takes a Supplier<BufferedReader> thanks to type erasure. Unerasing the type by subclassing Supplier to make it complete makes it easy to supply a lambda here.
      Parameters:
      source - a supplier of an input stream
      Returns:
      '\n'-separated lines from the input stream
    • over

      public static Iterable<String> over(com.ontology2.pidove.iterables.Iterables.SupplierOfBufferedReader source)
      Unique to pidove Given a Supplier of an Input Stream, opens the stream and iterates over lines in the file. Note that if we used a Supplier<BufferedReader> here we couldn't also make an over method that takes a Supplier<InputStream> thanks to type erasure. Unerasing the type by subclassing Supplier to make it complete makes it easy to supply a lambda here.
      Parameters:
      source - a supplier of an input stream
      Returns:
      '\n'-separated lines from the input stream
    • splitOn

      public static Iterable<String> splitOn(String regex, String source)
      Unique to pidove
      Parameters:
      regex - regular expression
      source - source string
      Returns:
      Iterable of parts of the input string split by the regex
    • splitOn

      public static Iterable<String> splitOn(String regex, int limit, String source)
      Unique to pidove
      Parameters:
      regex - regular expression
      limit - maximum number of splits
      source - source string
      Returns:
      Iterable of parts of the input string split by the regex
    • peek

      public static <X> TidyIterable<X> peek(Consumer<X> listener, Iterable<X> values)
      Imitates the peek method from the Java Streams API
      Type Parameters:
      X - type of the input and output iterables
      Parameters:
      listener - this consuemr receives a copy of each item from the iterable as it is received
      values - an input iterable
      Returns:
      the same values as the input iterable
    • range

      public static Iterable<Long> range(long stop)
      This is like the range function from the Python standard library. range(4) produces the values 0,1,2,3
      Parameters:
      stop - iteration starts at zero and stops when we reach stop
      Returns:
      an iterable that counts from 0 to stop
    • range

      public static Iterable<Long> range(long start, long stop)
      This is like the range function from the Python standard library range(10,12) produces the values 10,11
      Parameters:
      start - iteration starts at this value
      stop - iteration ends at this value
      Returns:
      an iterable that counts from start to stop
    • range

      public static Iterable<Long> range(long start, long stop, long skip)
      This is like the range function from the Python standard library
      Parameters:
      start - the beginning of the range
      stop - iteration stops after we are equal to stop, greater, or less depending on if step is positive or negative
      skip - how much we add for each iteration
      Returns:
      an iterable that counts from start to stop with skip as stride
    • reduce

      public static <X> Optional<X> reduce(BinaryOperator<X> accumulator, Iterable<X> values)
      Imitates the reduce method from the Java Streams API
      Type Parameters:
      X - type of the iterable's values
      Parameters:
      accumulator - a binary operator that combines two values of type X
      values - an iterable
      Returns:
      the result of applying the binary operator repeatedly over values from the iterable in order wrapped in an optional, empty if values is empty
    • reduce

      public static <X> X reduce(X identity, BinaryOperator<X> accumulator, Iterable<X> values)
      Imitates the reduce method from the Java Streams API
      Type Parameters:
      X - type of the iterable's values
      Parameters:
      identity - initial value
      accumulator - a binary operator that combines two values of type X
      values - the result of applying the binary operator repeatedly over values from the iterable in order
      Returns:
      the result of applying the binary operator repeatedly over values from the iterable in order, identity if the iterable is empty
    • skip

      public static <X> TidyIterable<X> skip(int amount, Iterable<X> values)
      Imitates the skip method from the Java Streams API
      Type Parameters:
      X - type of items from the iterable
      Parameters:
      amount - number of items to skip
      values - an iterable
      Returns:
      items from the input iterable skipping the first amount
    • sumInt

      public static int sumInt(Iterable<Integer> values)
      Convenience method to get sum of integer Iterable
      Parameters:
      values - Iterable with integer items
      Returns:
      the sum of the iterable's items
    • sumLong

      public static long sumLong(Iterable<Long> values)
      Convenience method to get sum of Long Iterable
      Parameters:
      values - Iterable with long items
      Returns:
      the sum of the iterable's items
    • sumDouble

      public static double sumDouble(Iterable<Double> values)
      Convenience method to get sum of Double Iterable
      Parameters:
      values - Iterable with double items
      Returns:
      the sum of the iterable's items
    • takeWhile

      public static <X> TidyIterable<X> takeWhile(Predicate<? super X> predicate, Iterable<X> values)
      Imitates the takeWhile method of the Java Streams API Stops iterating when the predicate evaluates false
      Type Parameters:
      X - type of the items in the Iterable
      Parameters:
      predicate - boolean-valued function of X
      values - an iterable
      Returns:
      items from values so long as the predicate evaluates true
    • asList

      public static <X> List<X> asList(Iterable<X> values)
      Imitates the toList method of the Java Streams API
      Type Parameters:
      X - type of items in the iterable
      Parameters:
      values - an iterable
      Returns:
      a List populated with items of the iterable in order
    • forEach

      public static <X> void forEach(Consumer<? super X> action, Iterable<X> values)
      Imitates the forEach/forEachOrdered methods of the Java Stream API since pidove does an ordered iteration. Unlike the Streams API, this method closes the iterator when the iteration is done if the iterator supports AutoClosable.
      Type Parameters:
      X - type of items in the iterable
      Parameters:
      action - consumer called for each item in the iterable
      values - an iterable
    • asSet

      public static <X> Set<X> asSet(Iterable<X> values)
      Imitates the toSet method in the Java Streams API
      Type Parameters:
      X - type of items in the iterable
      Parameters:
      values - an iterable
      Returns:
      a set of the unique members of the iterable
    • joinOn

      public static String joinOn(CharSequence separator, Iterable<? extends CharSequence> values)
      Unique to pidove.
      Parameters:
      separator - separator between items
      values - an iterable
      Returns:
      a string consisting of iterable items alternating with separators
    • asMap

      public static <X, Y> Map<X,Y> asMap(Iterable<Pair<X,Y>> pairs)
      Type Parameters:
      X - type of the keys
      Y - type of the values
      Parameters:
      pairs - an iterable of key value pairs
      Returns:
      a map with those key value pairs
    • close

      public static void close(Object that) throws RuntimeException
      Given an arbitrary object, calls the close method if it is AutoCloseable, otherwise do nothing
      Parameters:
      that - any Object
      Throws:
      RuntimeException - if the AutoCloseable#close method is called and throws one
    • enumerate

      public static <X> Iterable<Pair<Long,X>> enumerate(Iterable<X> values)
      Based on Python's enumerate() function, which given an iterable that returns A, B, C, ... returns the following pairs (0,A), (1,B), (2,C), ...
      Type Parameters:
      X - type of the inner iterable
      Parameters:
      values - an input iterable
      Returns:
      an iterable of numbered pairs
    • enumerate

      public static <X> Iterable<Pair<Long,X>> enumerate(Long start, Iterable<X> values)
      Based on Python's enumerate() function, which given an iterable that returns A, B, C, ... returns the following pairs (start,A), (start+1,B), (start+2,C), ...
      Type Parameters:
      X - type of the inner iterable
      Parameters:
      start - beginning value of iterator
      values - an input iterable
      Returns:
      an iterable of numbered pairs
    • reversed

      public static <X> Iterable<X> reversed(Iterable<X> values)
      Based on the Python reversed() function. Stores items in a Deque so potentially these can be deallocated after they are consumed.
      Type Parameters:
      X - type of the output and input
      Parameters:
      values - an iterator
      Returns:
      the same items in reversed order
    • zip

      public static <X, Y> Iterable<Pair<X,Y>> zip(Iterable<X> one, Iterable<Y> two)
      Based on the Python zip function
      Type Parameters:
      X - type of items from the first iterable
      Y - type of items from the second iterable
      Parameters:
      one - first iterable
      two - second iterable
      Returns:
      an iterable of pairs taken from the first and second iterable
    • zip

      public static <X, Y, Z> Iterable<Trio<X,Y,Z>> zip(Iterable<X> one, Iterable<Y> two, Iterable<Z> three)
      Based on the Python zip function Note that Python's zip function is capable of combining an arbitrary number of iterables into tuples. It's not so natural to do this in Java because of the desire for tuples to be typed.
      Type Parameters:
      X - type of items from first iterable
      Y - type of items from second iterable
      Z - type of items from third iterable
      Parameters:
      one - first iterable
      two - second iterable
      three - third iterable
      Returns:
      Trios taken from the three iterables
    • cycle

      public static <X> TidyIterable<X> cycle(Iterable<X> values)
      Based on the cycle function from the Python itertools modules
      Type Parameters:
      X - the type of items in the input and output iterables
      Parameters:
      values - an iterable
      Returns:
      the items in the iterable repeated forever
    • cycle

      public static <X> TidyIterable<X> cycle(long times, Iterable<X> values)
      Like the cycle function from Python's itertools but you can specify how many cycles you want
      Type Parameters:
      X - arbitrary type
      Parameters:
      times - number of cycles
      values - we draw from these values
      Returns:
      an Iterable that repeats values times times
    • repeat

      public static <X> Iterable<X> repeat(long times, X value)
      Like the repeat function in Python's itertools
      Type Parameters:
      X - type of the value
      Parameters:
      times - number of times to repeat the value
      value - a value that will be repeated
      Returns:
      an iterable that repeats the value times times
    • accumulate

      public static <X> Iterable<X> accumulate(BinaryOperator<X> func, Iterable<X> value)
      Like the accumulate function from Python's itertools. Like the reduce method in pidove, this method repeatedly applies a binary operator to values from the iterable, but instead of returning only the final result, it returns all of the intermediate results
      Type Parameters:
      X - type of items in the input and output iterables
      Parameters:
      func - a function that accumulates values
      value - an iterable
      Returns:
      items derived by applying func cumulatively to the values in the iterable
    • compress

      public static <X> Iterable<X> compress(Iterable<Boolean> filter, Iterable<X> value)
      Like the compress function from Python's itertools.
      Type Parameters:
      X - the type of the input and output interables
      Parameters:
      filter - Iterable of boolean values
      value - Iterable of input values
      Returns:
      Those values from values for which the corresponding value from filter is true
    • filterFalse

      public static <X> Iterable<X> filterFalse(Predicate<X> p, Iterable<X> values)
      Like the filterfalse function from Python's itertools. Similar to the filter method in pidove except that the sense of matching is reversed
      Type Parameters:
      X - type of the input and output iterables.
      Parameters:
      p - a boolean function of X
      values - an iterable that yields values of type X
      Returns:
      all values for which p evaluates false
    • at

      public static <X> X at(int index, X[] values)
      Like the indexing operator in Python. Has the advantage that methods with the same name can be used for arrays, Lists and all Iterables and Collections. Also allows indexing from the end of the array started by -1
      Type Parameters:
      X - component type of the array
      Parameters:
      index - index into the array
      values - an array
      Returns:
      the index-th element from the beginning (starting from zero) for a positive index and will return the (-index)-th element from the end (starting at -1) for a negative index
    • at

      public static <X> X at(int index, Iterable<X> values)
      Like the indexing operator in Python. Has the advantage that methods with the same name can be used for arrays, Lists and all Iterables and Collections. Also allows indexing from the end of the array started by -1 This function uses optimized algorithms for lists (the get() method) and collections (where we know how what the length of the stucture is)
      Type Parameters:
      X - type contained by iterable.
      Parameters:
      index - index into the iterable
      values - an iterable
      Returns:
      the index-th element from the beginning (starting from zero) for a positive index and will return the (-index)-th element from the end (starting at -1) for a negative index
    • len

      public static int len(CharSequence s)
      Similar to the len function in Python. Something nice about Python is that you use the same function to get the length of anything that has a length. Pidove does that for you in Java
      Parameters:
      s - a CharSequence
      Returns:
      the length of the CharSequence in characters
    • len

      public static <T> int len(T[] a)
      Type Parameters:
      T - the component type of the array
      Parameters:
      a - an array
      Returns:
      the length of the array
    • len

      public static <T> int len(Iterable<T> i)
      If the iterable implements collections, this method uses the size method of the collection, otherwise it counts the members.
      Type Parameters:
      T - the component type of the array
      Parameters:
      i - an iterable
      Returns:
      the length of the iterable
    • tail

      public static <X> Iterable<X> tail(int amount, Iterable<X> x)
      Note this uses somewhat different algorithms for different cases, since it is easy to pick the last N elements off the end of a RandomAccess List, but quite a hassle in comparison in the generic case where we have to keep a running memory of the last N elements we've seen.
      Type Parameters:
      X - type returned by iterable
      Parameters:
      amount - number of elements
      x - an iterable
      Returns:
      an iterable of the last "amount" elements of the iterable
    • pairwise

      public static <X> Iterable<Pair<X,X>> pairwise(Iterable<X> values)
      Like the pairwise function from Python's itertools. If the input is [a,b,c,d,e,f], the output is [(a,b), (b,c), (c,d), (d,e), (e,f)]
      Type Parameters:
      X - type of the input iterable
      Parameters:
      values - an iterable that yields values of type X
      Returns:
      pairs of consequetive values from values
    • window

      public static <X, Y> Iterable<Y> window(int length, Collector<X,?,Y> collector, Iterable<X> values)
      Unique to pidove, but taking advantage of Collectors from the Stream API Gathers values yielded by values into overlapping groups of length and passes these through the collector (in the case of N=2 and the toList() collector this has an effect similar to the pairwise() method.) Could be used to compute moving averages and other convolutions
      Type Parameters:
      X - input type
      Y - output type
      Parameters:
      length - length of the window
      collector - a Collector function that gets applied to each window derived from the input
      values - an iterable that yields type X
      Returns:
      an iterable of type Y values generated by the Collector
    • product

      public static <X, Y> Iterable<Pair<X,Y>> product(Iterable<X> left, Iterable<Y> right)
      Generates the Cartesian product of two iterators. If left is [1,2,3] and right is [a,b] the output is [(1,a),(1,b),(2,a),(2,b),(3,a),(3,b)] Similar to the product function in Python's itertools but different in that instead of materializing the input iterables, it iterates over the iterables repeatedly. If you want to materialize the iterables turn them into lists. Another difference from Python (similar to the zip function) we don't support an arbitrary number of input iterables because the Java equivalent of a Tuple is typed and thus needs a class declared for every tuple length.
      Type Parameters:
      X - type of left
      Y - type of right
      Parameters:
      left - first input iterable
      right - second input iterable
      Returns:
      an iterable of pairs of the cartesian product of the two inputs
    • product

      public static <X, Y, Z> Iterable<Trio<X,Y,Z>> product(Iterable<X> left, Iterable<Y> middle, Iterable<Z> right)
      Generates the Cartesian product of three iterators.
      Type Parameters:
      X - type of left
      Y - type of middle
      Z - type of right
      Parameters:
      left - first input iterable
      middle - second input iterable
      right - third input iterable
      Returns:
      an iterable of trios of the cartesian product of the three inputs