Package com.ontology2.pidove.iterables
Class Iterables
java.lang.Object
com.ontology2.pidove.iterables.Iterables
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <X> Iterable<X>
accumulate
(BinaryOperator<X> func, Iterable<X> value) Like the accumulate function from Python's itertools.static boolean
Imitates the "all" function from Python.static boolean
Imitates the "any" function from Python.static <X> List<X>
Imitates the toList method of the Java Streams APIstatic <X,
Y> Map<X, Y> static <X> Set<X>
Imitates the toSet method in the Java Streams APIstatic <X> X
Like the indexing operator in Python.static <X> X
at
(int index, X[] values) Like the indexing operator in Python.static void
Given an arbitrary object, calls the close method if it is AutoCloseable, otherwise do nothingstatic <X,
Y, Z> Z Imitates the collect() method in the streams APIstatic <X> Iterable<X>
Like the compress function from Python's itertools.static <X> TidyIterable<X>
Imitates the "concat" function in the Java Streams API.static <X> long
Imitates the "count" method in the Java Streams APIstatic <X> TidyIterable<X>
Like the cycle function from Python's itertools but you can specify how many cycles you wantstatic <X> TidyIterable<X>
Based on the cycle function from the Python itertools modulesstatic <X> Set<X>
Imitates the "distinct" method in the java streams API.static <X> TidyIterable<X>
Imitates the "dropWhile" method in the java streams API.static Iterable<?>
empty()
Imitates the "empty" method in the Java Streams APIBased on Python's enumerate() function, which given an iterable that returns A, B, C, ...Based on Python's enumerate() function, which given an iterable that returns A, B, C, ...static <X> TidyIterable<X>
Imitates the filter method in the Streams APIstatic <X> Iterable<X>
filterFalse
(Predicate<X> p, Iterable<X> values) Like the filterfalse function from Python's itertools.static <X> Optional<X>
Imitates the "first" method in the Java Stream APIstatic <X,
Y> TidyIterable<Y> Imitates the flatmap method in the Java Streams APIstatic <X> TidyIterable<X>
static <X> void
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
len
(CharSequence s) Similar to the len function in Python.static <T> int
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>
Imitates the limit method of the Java Streams APIstatic <X,
Y> TidyIterable<Y> Imitates the map method of the Java Streams APIstatic <X extends Comparable<X>>
Optional<X>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 APIstatic <X extends Comparable<X>>
Optional<X>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 APIstatic 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 APIstatic <X> Iterable<X>
of
(X... x) Imitates the of method of the Java Streams APIstatic <X> Iterable<X>
ofNullable
(X x) Imitates the ofNullable method of the java streams APIover
(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.over
(CharSequence s) Unique to pidoveUnique to pidove Given a path, opens the file in character mode and returns an iterable of the lines in the fileUnique to pidove.static <X> Iterable<X>
over
(X[] x) Unique to pidoveLike the pairwise function from Python's itertools.static <X> TidyIterable<X>
Imitates the peek method from the Java Streams APIGenerates the Cartesian product of two iterators.Generates the Cartesian product of three iterators.range
(long stop) This is like the range function from the Python standard library.range
(long start, long stop) This is like the range function from the Python standard library range(10,12) produces the values 10,11range
(long start, long stop, long skip) This is like the range function from the Python standard librarystatic <X> Optional<X>
reduce
(BinaryOperator<X> accumulator, Iterable<X> values) Imitates the reduce method from the Java Streams APIstatic <X> X
reduce
(X identity, BinaryOperator<X> accumulator, Iterable<X> values) Imitates the reduce method from the Java Streams APIstatic <X> Iterable<X>
repeat
(long times, X value) Like the repeat function in Python's itertoolsstatic <X> Iterable<X>
Based on the Python reversed() function.static <X> TidyIterable<X>
Imitates the skip method from the Java Streams APIUnique to pidoveUnique to pidovestatic double
Convenience method to get sum of Double Iterablestatic int
Convenience method to get sum of integer Iterablestatic long
Convenience method to get sum of Long Iterablestatic <X> Iterable<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>
Imitates the takeWhile method of the Java Streams API Stops iterating when the predicate evaluates falsestatic <X,
Y> Iterable<Y> 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 convolutionsBased on the Python zip functionBased on the Python zip function Note that Python's zip function is capable of combining an arbitrary number of iterables into tuples.
-
Constructor Details
-
Iterables
public Iterables()
-
-
Method Details
-
all
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
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
Imitates the collect() method in the streams API- Type Parameters:
X
- the type of the values in the iterableY
- the type of the data structure that the collector store immediate results inZ
- the type of the result returned by the collector.- Parameters:
collector
- a collectorvalues
- an iterable- Returns:
- the result of the collector, of type Z
-
concat
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
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
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
Imitates the "dropWhile" method in the java streams API.- Type Parameters:
X
- the type of the iterable- Parameters:
predicate
- boolean function of an X valuevalues
- a set of values- Returns:
- an iterable that starts at the first point in values where the predicate returns false
-
empty
Imitates the "empty" method in the Java Streams API- Returns:
- an empty iterable
-
first
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
Imitates the filter method in the Streams API- Type Parameters:
X
- the type of the input iterable- Parameters:
predicate
- a boolean function of Xvalues
- an iterable- Returns:
- an iterable of all values from values where predicate is true
-
flatten
- 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 typeY
- the output iterable type- Parameters:
fn
- a function that turns X into an iterable of Yvalues
- an iterable- Returns:
- an iterable with all the values of type Y returned by fn
-
generate
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
Imitates the iterate method of the Java Strsams API.- Type Parameters:
X
- type of values in the outut iterable- Parameters:
seed
- the first valuef
- 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
Imitates the limit method of the Java Streams API- Type Parameters:
X
- type of values in iterable- Parameters:
amount
- how many values to takevalues
- an iterable- Returns:
- the first amount values of the iterable
-
map
Imitates the map method of the Java Streams API- Type Parameters:
X
- input iterable typeY
- output iterable type- Parameters:
fn
- a function from X to Yvalues
- an iterable- Returns:
- an iterable produced by applying fn to all values of the input iterable
-
max
Imitates the max method of the Java Streams API- Type Parameters:
X
- the type of values in the input iterable- Parameters:
values
- an iterablecomparator
- a comparison function over the iterable- Returns:
- the maximum value determined by that comparison function
-
max
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
Imitates the min method of the Java Streams API- Type Parameters:
X
- the type of values in the input iterable- Parameters:
values
- an iterablecomparator
- a comparison function over the iterable- Returns:
- the minimum value determined by that comparison function
-
min
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
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
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
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
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
Unique to pidove- Parameters:
s
- a CharSequence (ex. a String)- Returns:
- the characters in the CharSequence as an Iterable
-
over
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
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 keysY
- type of the pairs- Parameters:
that
- a Map- Returns:
- key-value pairs
-
over
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
Unique to pidove- Parameters:
regex
- regular expressionsource
- source string- Returns:
- Iterable of parts of the input string split by the regex
-
splitOn
Unique to pidove- Parameters:
regex
- regular expressionlimit
- maximum number of splitssource
- source string- Returns:
- Iterable of parts of the input string split by the regex
-
peek
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 receivedvalues
- an input iterable- Returns:
- the same values as the input iterable
-
range
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
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 valuestop
- iteration ends at this value- Returns:
- an iterable that counts from start to stop
-
range
This is like the range function from the Python standard library- Parameters:
start
- the beginning of the rangestop
- iteration stops after we are equal to stop, greater, or less depending on if step is positive or negativeskip
- how much we add for each iteration- Returns:
- an iterable that counts from start to stop with skip as stride
-
reduce
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 Xvalues
- 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
Imitates the reduce method from the Java Streams API- Type Parameters:
X
- type of the iterable's values- Parameters:
identity
- initial valueaccumulator
- a binary operator that combines two values of type Xvalues
- 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
Imitates the skip method from the Java Streams API- Type Parameters:
X
- type of items from the iterable- Parameters:
amount
- number of items to skipvalues
- an iterable- Returns:
- items from the input iterable skipping the first amount
-
sumInt
Convenience method to get sum of integer Iterable- Parameters:
values
- Iterable with integer items- Returns:
- the sum of the iterable's items
-
sumLong
Convenience method to get sum of Long Iterable- Parameters:
values
- Iterable with long items- Returns:
- the sum of the iterable's items
-
sumDouble
Convenience method to get sum of Double Iterable- Parameters:
values
- Iterable with double items- Returns:
- the sum of the iterable's items
-
takeWhile
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 Xvalues
- an iterable- Returns:
- items from values so long as the predicate evaluates true
-
asList
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
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 iterablevalues
- an iterable
-
asSet
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
Unique to pidove.- Parameters:
separator
- separator between itemsvalues
- an iterable- Returns:
- a string consisting of iterable items alternating with separators
-
asMap
- Type Parameters:
X
- type of the keysY
- type of the values- Parameters:
pairs
- an iterable of key value pairs- Returns:
- a map with those key value pairs
-
close
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
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
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 iteratorvalues
- an input iterable- Returns:
- an iterable of numbered pairs
-
reversed
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
Based on the Python zip function- Type Parameters:
X
- type of items from the first iterableY
- type of items from the second iterable- Parameters:
one
- first iterabletwo
- second iterable- Returns:
- an iterable of pairs taken from the first and second iterable
-
zip
public static <X,Y, Iterable<Trio<X,Z> Y, zipZ>> (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 iterableY
- type of items from second iterableZ
- type of items from third iterable- Parameters:
one
- first iterabletwo
- second iterablethree
- third iterable- Returns:
- Trios taken from the three iterables
-
cycle
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
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 cyclesvalues
- we draw from these values- Returns:
- an Iterable that repeats values times times
-
repeat
Like the repeat function in Python's itertools- Type Parameters:
X
- type of the value- Parameters:
times
- number of times to repeat the valuevalue
- a value that will be repeated- Returns:
- an iterable that repeats the value times times
-
accumulate
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 valuesvalue
- an iterable- Returns:
- items derived by applying func cumulatively to the values in the iterable
-
compress
Like the compress function from Python's itertools.- Type Parameters:
X
- the type of the input and output interables- Parameters:
filter
- Iterable of boolean valuesvalue
- Iterable of input values- Returns:
- Those values from values for which the corresponding value from filter is true
-
filterFalse
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 Xvalues
- 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 arrayvalues
- 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
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 iterablevalues
- 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
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
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
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 elementsx
- an iterable- Returns:
- an iterable of the last "amount" elements of the iterable
-
pairwise
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
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 typeY
- output type- Parameters:
length
- length of the windowcollector
- a Collector function that gets applied to each window derived from the inputvalues
- an iterable that yields type X- Returns:
- an iterable of type Y values generated by the Collector
-
product
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 leftY
- type of right- Parameters:
left
- first input iterableright
- second input iterable- Returns:
- an iterable of pairs of the cartesian product of the two inputs
-
product
public static <X,Y, Iterable<Trio<X,Z> Y, productZ>> (Iterable<X> left, Iterable<Y> middle, Iterable<Z> right) Generates the Cartesian product of three iterators.- Type Parameters:
X
- type of leftY
- type of middleZ
- type of right- Parameters:
left
- first input iterablemiddle
- second input iterableright
- third input iterable- Returns:
- an iterable of trios of the cartesian product of the three inputs
-