2022-12-17 20:19:42 +00:00
|
|
|
package funkin.util.tools;
|
2022-03-13 18:36:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A static extension which provides utility functions for Iterators.
|
2023-06-08 20:30:45 +00:00
|
|
|
*
|
2022-03-13 18:36:03 +00:00
|
|
|
* For example, add `using IteratorTools` then call `iterator.array()`.
|
2023-06-08 20:30:45 +00:00
|
|
|
*
|
2022-03-13 18:36:03 +00:00
|
|
|
* @see https://haxe.org/manual/lf-static-extension.html
|
|
|
|
*/
|
|
|
|
class IteratorTools
|
|
|
|
{
|
2023-01-23 03:25:45 +00:00
|
|
|
public static function array<T>(iterator:Iterator<T>):Array<T>
|
|
|
|
{
|
|
|
|
return [for (i in iterator) i];
|
|
|
|
}
|
2023-09-08 21:46:44 +00:00
|
|
|
|
|
|
|
public static function count<T>(iterator:Iterator<T>, ?predicate:(item:T) -> Bool):Int
|
|
|
|
{
|
|
|
|
var n = 0;
|
|
|
|
|
|
|
|
if (predicate == null)
|
|
|
|
{
|
|
|
|
for (_ in iterator)
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (x in iterator)
|
|
|
|
if (predicate(x)) n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
2022-03-13 18:36:03 +00:00
|
|
|
}
|