itertools icon indicating copy to clipboard operation
itertools copied to clipboard

Const generics iterator candidates

Open conradludgate opened this issue 4 years ago • 6 comments

Discussed in #546 where I've written an alternative to tuple_combinations, array_combinations which is an iterator that outputs [T; N]. This feature requires const generics which were introduced in Rust 1.51. It was suggested that we can bump the MSRV in a major release (ie itertools 0.11) but it would be sensible to add more features along side.

This crate uses a lot of tuples where arrays make more sense. Tuples are good when you have multiple types (eg cartesian product or zip) but when you have a collection of the same type, a slice/vec/array makes more sense.

Here's a list of functions that seem to qualify using arrays:

  • circular_tuple_windows -> circular_array_windows
  • tuples -> arrays
  • collect_tuple -> collect_array
  • next_tuple -> next_array

These all work on a single iterator over the single I::Item type, and since these use tuples, the number of elements is known at compile time, making them perfect for using arrays instead of tuples.

conradludgate avatar May 25 '21 13:05 conradludgate

Note that things like array_windows for iterators are under discussion for iterators in std, see conversations like https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Iterator.3A.3A.7Bpairwise.2C.20windows.7D/near/224587771

As I understand it std didn't want the tuple ones because of the extra trait complications in the interface, but with const generics available the array ones might be able to live there instead of here.

scottmcm avatar May 25 '21 21:05 scottmcm

Could this be added and gated on rust 1.51? That'd maintain backwards compatibility without any problem

Plecra avatar Feb 02 '24 15:02 Plecra

@Plecra We use 1.43.1 at the moment but will move to 1.51 at some point and use const generics, no doubt. I don't think there is a discussion about when we will do it but I suppose after #755 (well underway) and #844 (not started).

Philippe-Cholet avatar Feb 02 '24 16:02 Philippe-Cholet