How to perform Stream functions on an Iterable? [duplicate]

My similar question got marked as duplicate, but here is the helper methods I’ve used to avoid some of the boilerplate: public static <T> Stream<T> stream(Iterable<T> in) { return StreamSupport.stream(in.spliterator(), false); } public static <T> Stream<T> parallelStream(Iterable<T> in) { return StreamSupport.stream(in.spliterator(), true); }

Printing the same character several times without a loop

The original answer is from 2014, so there must have been some updates to the Dart language: a simple string multiplied by an int works. main() { String title=”Dart: Strings can be “multiplied””; String line=”-” * title.length print(line); print(title); print(line); } And this will be printed as: ——————————— Dart: Strings can be “multiplied” ——————————— See … Read more

How to write a trait which checks whether a type is iterable

You may create a trait for that: namespace detail { // To allow ADL with custom begin/end using std::begin; using std::end; template <typename T> auto is_iterable_impl(int) -> decltype ( begin(std::declval<T&>()) != end(std::declval<T&>()), // begin/end and operator != void(), // Handle evil operator , ++std::declval<decltype(begin(std::declval<T&>()))&>(), // operator ++ void(*begin(std::declval<T&>())), // operator* std::true_type{}); template <typename T> std::false_type … Read more

what’s the point of having both Iterator.forEachRemaining() and Iterable.forEach()?

To understand why the two methods both exist, you need to first understand what are Iterator and Iterable. An Iterator basically is something that has a “next element” and usually, an end. An Iterable is something that contains elements in a finite or infinite sequence and hence, can be iterated over by keep getting the … Read more

Check if a variable type is iterable?

You may create a trait for that: namespace detail { // To allow ADL with custom begin/end using std::begin; using std::end; template <typename T> auto is_iterable_impl(int) -> decltype ( begin(std::declval<T&>()) != end(std::declval<T&>()), // begin/end and operator != void(), // Handle evil operator , ++std::declval<decltype(begin(std::declval<T&>()))&>(), // operator ++ void(*begin(std::declval<T&>())), // operator* std::true_type{}); template <typename T> std::false_type … Read more