3 ms·
Why reverse the slice at all? Collect the input into a slice, then return an iterator that navigates the slice in backward order. What this sort of thing lacks
by atombender 2y ago
Why reverse the slice at all? Collect the input into a slice, then return an iterator that navigates the slice in backward order.
What this sort of thing lacks is any ability to optimize. For example, let's say the operation is Reverse().Take(20). There's no reason for the reverser to keep more than 20 elements in its buffer. But to express that, you have to make sure the iterator can be introspected and then rewritten to merge the operators and maybe unroll some of the loops to get better cache locality. This is what Haskell can achieve via Stream Fusion, which is pretty neat. But not so viable in Go.
- dilap 2y agoThanks, good points.
- ted_dunning 2y agoJulia does similar optimizations with the broadcast operator. The results can be mind bogglingly fast.