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

Kotlin’s Iterable and Sequence look exactly same. Why are two types required?

The key difference lies in the semantics and the implementation of the stdlib extension functions for Iterable<T> and Sequence<T>. For Sequence<T>, the extension functions perform lazily where possible, similarly to Java Streams intermediate operations. For example, Sequence<T>.map { … } returns another Sequence<R> and does not actually process the items until a terminal operation like … Read more

How does a Python for loop with iterable work?

feed.entry is property of feed and it’s value is (if it’s not, this code will fail) object implementing iteration protocol (array, for example) and has iter method, which returns iterator object Iterator has next() method, returning next element or raising exception, so python for loop is actually: iterator = feed.entry.__iter__() while True: try: party = … Read more

Why is Java’s Iterator not an Iterable?

An iterator is stateful. The idea is that if you call Iterable.iterator() twice you’ll get independent iterators – for most iterables, anyway. That clearly wouldn’t be the case in your scenario. For example, I can usually write: public void iterateOver(Iterable<String> strings) { for (String x : strings) { System.out.println(x); } for (String x : strings) … Read more