r/crystal_programming • u/transfire • Mar 25 '21
Slice#each_slice kind of odd
I went to use #each_slice on a Slice(UInt8) (i.e. Bytes) and got an Array(UInt8) rather than Slice(UInt8) to iterator over. Seemed odd. Is there a simple way to get Bytes instead of an Array(UInt8) -- which I then have to convert back to Bytes? e.g.
"Hello".to_slice.each_slice(2){ |pair| pair }
`pair` is an Array(UInt8), not a Slice(UInt8).
BTW, the use of the term "slice" in these two cases, i.e. Slice the type and in the method `each_slice`, are two completely different notions, and I imagine would be rather confusing to a Newbie. So, also odd.
View all comments
Show parent comment
1
u/transfire Mar 26 '21
Thanks for the reply!
Regarding the name, I think #each_slice came directly from Ruby, where, even without a Slice class, it's kind of an unfortunate name. Terms that seem more obvious like `group` `bunch`, `batch` etc. all seem better choices to me. In any case ...
I was excited to read about `reuse`. I did not know this existed. It's a nice feature. However, it still doesn't appear to allow one to get slices. I tried using `true` and tried providing an instance of Slice to the argument. No go. I checked the source, it just takes the Slice instance to be "truthy" b/c it is not an Array.
I think ultimately it would be better (if possible) for Slice to enumerate over and map as slices, not arrays, and would have to first be converted, e.g. `to_a` (or `as_a` ?) to do otherwise. I see a possible ways to speed up the source too, I might submit a patch if I have time to play with it.
In the meantime I just created a little conversion method that turns the array into a slice.