I’ll have a look. I’ve been looking at the data flow API thinking about how to make it more functional and almost certainly less generic, and this is the same sort of thing.
Note that this was just in the process of progressively refactoring like in the python version, so it’s doing by hand what Dataset.batched does a bit later.
As @sgugger mentioned, often the dupe code is intentional, so we can show how it’s refactored later. However if we don’t refactor it later in the notebook, then that’s a mistake!
I suspect his could be made much shorter just by defining a closure of AnySequence.
But it seems like there might be a didactic purpose behind not simplifying some of the loops, so I’ll just leave it as is for now. (Unless I can’t stop myself and start tweaking it tonight. )
The gist above defines a batchedIndices(n:Int,batchSize:Int), which works like you suggested, as well as a generic batchedIndices<C:Collection>(coll:C,batchSize:Int), which might be safer to use in case someone wanted to generates batches over a collection where startIndex was not zero (which can happen with ArraySlice, if I recall correctly).
It’s an annotation to indicate that once you call makeIterator() on an object, then the object is invalid for producing another iterator. This is the usual contract for Sequence so I stuck it in out of habit and following standard library patterns.
But now that I think of it, it’s not actually true in this case, since RangeStrideThrough has no mutable state.
So in short, it shouldn’t be there. But it has no effect now. But might produce a performance cost in some future version of Swift that was trying to be more clever about move operations.
Interesting. I didn’t know about that API! That’s not quite what’s needed but there’s another one, func sequence<T, State>(state: State, next: @escaping (inout State) -> T?) which does the trick. That gives you the following
I also like this SEP because it is another piece of evidence for my semi-facetious thesis that Swift has every functional programming language feature which people no longer think of as functional programming, either because it has become mainstream, or because has been renamed.