A crate for treating arrays and tuples a little bit more like vectors.
At the moment, this crate only provides .push(val)
and .pop()
through the OrdesInc
and OrdesDec
methods respectively, though I think I could potentially add .insert(val)
, .concatenate(array)
, and .remove(index)
in the nightly version without too much trouble. Soon™, I guess.
This crate was born out of my stubborn desire to use iterators wherever possible instead of for
loops, which makes packing data together a bit of a pain in the neck at times. For instance, consider an iterator producing all lowercase four letter "words":
rust
('a'..='z')
.flat_map(|l1| ('a'..='z').map(move |l2| (l1, l2)))
.flat_map(|(l1, l2)| ('a'..='z').map(move |l3| (l1, l2, l3)))
.flat_map(|(l1, l2, l3)| ('a'..='z').map(move |l4| (l1, l2, l3, l4)))
.for_each(|(l1, l2, l3, l4)| println!("{}{}{}{}", l1, l2, l3, l4));
As we can see, this is both:
1. Not particularly pleasant to write or look at
2. Not something a sane person would do
This crate offers an alternative:
rust
use ordes::OrdesInc;
('a'..='z')
.flat_map(|l1| ('a'..='z').map(move |l2| (l1, l2)))
.flat_map(|chars| ('a'..='z').map(move |l3| chars.push(l3)))
.flat_map(|chars| ('a'..='z').map(move |l4| chars.push(l4)))
.for_each(|(l1, l2, l3, l4)| println!("{}{}{}{}", l1, l2, l3, l4));
There's not any real magic going on here. .push(val)
produces a value of a new type. In the first example, chars.push(l3)
, the input type is (char, char)
and the output is (char, char, char)
. Similar story for chars.push(l4)
- input of (char, char, char)
, output of (char, char, char, char)
. A nearly identical implementation can be made with arrays as well:
rust
use ordes::OrdesInc;
('a'..='z')
.flat_map(|l1| ('a'..='z').map(move |l2| [l1, l2]))
.flat_map(|chars| ('a'..='z').map(move |l3| chars.push(l3)))
.flat_map(|chars| ('a'..='z').map(move |l4| chars.push(l4)))
.for_each(|[l1, l2, l3, l4]| println!("{}{}{}{}", l1, l2, l3, l4));
In this case, chars.push(l3)
takes in [char; 2]
and produces [char; 3]
, and chars.push(l4)
takes in [char; 3]
and produces [char; 4]
.
In this crate I implement OrdesDec
and OrdesInc<T>
for arrays and tuples, but with some caveats:
- On stable, both traits are only implemented for array lengths up to 32.
- On both nightly and stable, both traits are only implemented for tuples up to 32 types.
Note that on nightly, both traits are implemented for arrays of arbitrary lengths (through the incomplete const_generics
and const_evaluatable_checked
features, ~~but I'm sure it's fine :^)~~ its not fine actually: I got an ICE (here) and two errors (one, two), so please use the stable version).
Both .push(val)
and .pop()
consume self
and produce a new type that's longer or shorter (or with an extra or dropping the last type in the case of tuples). I'm well aware this is a very limited use case, but it's useful to me, and hopefully a few other people.