diff --git a/examples/fibonacci.ion b/examples/fibonacci.ion index 5c7d080ce03993cfb11f7e78cb1e49ca3529a815..6f3b723cef070533a1c6078e6a789097b7388728 100644 --- a/examples/fibonacci.ion +++ b/examples/fibonacci.ion @@ -1,17 +1,13 @@ fn fib n - if test $n -le 1 - echo $n - else - let output previous = 1 1 - for _ in 2..=$n - let temp = $output - let output += $previous - let previous = $temp - end - echo $output + let output previous = 0 1 + for _ in 0..$n + let temp = $output + let output += $previous + let previous = $temp end + echo $output end -for i in 1..=200 +for i in 0..200 fib $i end diff --git a/examples/fibonacci.out b/examples/fibonacci.out index ad4f1e545ca151e7be91f43d0bd8be18c8ed4162..5f97ad4df878b7c5066f498e99f1323e6db45207 100644 --- a/examples/fibonacci.out +++ b/examples/fibonacci.out @@ -1,3 +1,5 @@ +0 +1 1 2 3 @@ -196,5 +198,3 @@ 66233869353085490000000000000000000000000 107168651819712330000000000000000000000000 173402521172797830000000000000000000000000 -280571172992510160000000000000000000000000 -453973694165308000000000000000000000000000 diff --git a/members/ranges/src/parse.rs b/members/ranges/src/parse.rs index 6f432ddb885d6fb411bf47038d2c22edee876fc1..30f14ce97e3f4e8cadabaf1e867d6450ae63c4f2 100644 --- a/members/ranges/src/parse.rs +++ b/members/ranges/src/parse.rs @@ -65,24 +65,19 @@ fn stepped_range_chars<'a>( fn numeric_range<'a>( start: isize, - mut end: isize, + end: isize, step: isize, inclusive: bool, nb_digits: usize, ) -> Option<Box<Iterator<Item = small::String> + 'a>> { - if start < end { - if inclusive { - end += 1; - } - stepped_range_numeric(start, end, step, nb_digits) - } else if start > end { - if inclusive { - end -= 1; - } - stepped_range_numeric(start, end, step, nb_digits) + let end = if start < end && inclusive { + end + 1 + } else if start > end && inclusive { + end - 1 } else { - Some(Box::new(Some(start.to_string().into()).into_iter())) - } + end + }; + stepped_range_numeric(start, end, step, nb_digits) } #[inline] @@ -91,7 +86,7 @@ fn byte_is_valid_range(b: u8) -> bool { (b >= b'a' && b <= b'z') || (b >= b'A' & use std::u8; fn char_range<'a>( start: u8, - mut end: u8, + end: u8, step: isize, inclusive: bool, ) -> Option<Box<Iterator<Item = small::String> + 'a>> { @@ -107,52 +102,33 @@ fn char_range<'a>( v as u8 }; - if start < end { - if inclusive { - end += 1; - } - stepped_range_chars(start, end, char_step) - } else if start > end { - if inclusive { - end -= 1; - } - stepped_range_chars(start, end, char_step) + let end = if start < end && inclusive { + end + 1 + } else if start > end && inclusive { + end - 1 } else { - Some(Box::new(Some((start as char).to_string().into()).into_iter())) - } + end + }; + stepped_range_chars(start, end, char_step) } fn count_minimum_digits(a: &str) -> usize { - let mut has_leading_zero = false; for c in a.chars() { match c { '-' => (), - '0' => { - has_leading_zero = true; - break; - } + '0' => return a.len(), '1'...'9' => break, _ => panic!("count_minimum_digits should only be called for a valid number."), } } - if !has_leading_zero { - 0 - } else { - a.len() - } + 0 } fn strings_to_isizes(a: &str, b: &str) -> Option<(isize, isize, usize)> { - if let Ok(first) = a.parse::<isize>() { - if let Ok(sec) = b.parse::<isize>() { - let nb_digits = usize::max(count_minimum_digits(a), count_minimum_digits(b)); - Some((first, sec, nb_digits)) - } else { - None - } - } else { - None - } + let first: isize = a.parse().ok()?; + let second: isize = b.parse().ok()?; + let nb_digits = usize::max(count_minimum_digits(a), count_minimum_digits(b)); + Some((first, second, nb_digits)) } // TODO: Make this an iterator structure.