ranges.rs

View source code here on GitHub!

This module directly ports some of the logic found in Python's range.

Includes

pub fn ranges::range_entry3<I>(start: I, idx: I, step: I) -> I where I: NumAssign

Returns the factorial of a given number. Note that it only accepts a u8 because any number that requires a larger type is guaranteed to overflow.

pub fn ranges::range_entry4<I>(start: I, stop: I, idx: I, step: I) -> Option<I> where I: NumAssign + PartialOrd + Copy

Returns the number of ways to choose r items from a set of n.

 1use std::cmp::PartialOrd;
 2use std::marker::Copy;
 3
 4use num_traits::{one,zero,NumAssign};
 5
 6pub fn range_entry3<I>(start: I, idx: I, step: I) -> I
 7where I: NumAssign
 8{
 9    return start + idx * step;
10}
11
12pub fn range_entry4<I>(start: I, stop: I, idx: I, step: I) -> Option<I>
13where I: NumAssign + PartialOrd + Copy
14{
15    let length = if step > zero() && start < stop {
16        one::<I>() + (stop - one() - start) / step
17    } else if step < zero() && start > stop {
18        one::<I>() + (stop + one() - start) / step
19    } else {
20        zero::<I>()
21    };
22
23    let i = if idx < zero() {
24        length + idx
25    } else {
26        idx
27    };
28
29    if i < length {
30        return Some(range_entry3(start, i, step));
31    }
32    return None;
33}