Added some methods to "@range"
#[no_std, cache_output]
+
+fix_step = (value: @number) => value.abs().floor() if is !=0 else 1
+
impl @range {
reverse: #[desc("Reverses the range."), example(u"
let range = 10..5..50
@@ -7,4 +10,54 @@ impl @range {
(self) -> @range {
return self.end..self.step_size..self.start
},
+ _plus_: #[desc("Adds a value to the range."), example(u"
+ let range = 10..5..50
+ $.assert(range + 10 == 20..5..60)
+ ")]
+ (self, value: @number) -> @range {
+ return self.start + value..self.step_size..self.end + value
+ },
+ _minus_: #[desc("Subtracts a value from the range."), example(u"
+ let range = 10..5..50
+ $.assert(range - 10 == 0..5..40)
+ ")]
+ (self, value: @number) -> @range {
+ return self.start - value..self.step_size..self.end - value
+ },
+ _times_: #[desc("Multiplies the range by a value."), example(u"
+ let range = 10..5..50
+ $.assert(range * 10 == 100..50..500)
+ ")]
+ (self, value: @number) -> @range {
+ return self.start * value..fix_step(self.step_size * value)..self.end * value
+ },
+ _divided_by_: #[desc("Divides the range by a value."), example(u"
+ let range = 10..5..50
+ $.assert(range / 10 == 1..1..5) // sadly steps on ranges must be integers
+ $.assert(range / -10 == -1..1..-5)
+ ")]
+ (self, value: @number) -> @range {
+ return match value {
+ ==0: (){
+ throw "Cannot divide range by zero."
+ }(),
+ else: self.start / value..fix_step(self.step_size / value)..self.end / value,
+ }
+ },
+ _pow_: #[desc("Raises the range to a power."), example(u"
+ let range = 10..5..50
+ $.assert(range ^ 2 == 100..25..2500)
+ ")]
+ (self, value: @number) -> @range {
+ return self.start ^ value..fix_step(self.step_size ^ value)..self.end ^ value
+ },
+ // this is not possible currently
+ // _in_: #[desc("Returns true if the value is in the range."), example(u"
+ // let range = 10..5..50
+ // $.assert(10 in range)
+ // $.assert(!(5 in range))
+ // ")]
+ // (self, value: @number) -> @bool {
+ // return value in @array(self)
+ // },
}
