Combinators

Exercises

Combinators 1

// 1. Complete the divide function to get the first four tests to pass.
// 2. Get the remaining tests to pass by completing the result_with_list and
//    list_of_results functions.

#[derive(Debug, PartialEq, Eq)]
pub enum DivisionError {
    NotDivisible(NotDivisibleError),
    DivideByZero,
}

#[derive(Debug, PartialEq, Eq)]
pub struct NotDivisibleError {
    dividend: i32,
    divisor: i32,
}

// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
// Otherwise, return a suitable error.
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
    todo!();
}

// Complete the function and return a value of the correct type so the test passes.
// Desired output: Ok([1, 11, 1426, 3])
fn result_with_list() -> () {
    let numbers = vec![27, 297, 38502, 81];
    let division_results = numbers.into_iter().map(|n| divide(n, 27));
}

// Complete the function and return a value of the correct type so the test passes.
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
fn list_of_results() -> () {
    let numbers = vec![27, 297, 38502, 81];
    let division_results = numbers.into_iter().map(|n| divide(n, 27));
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_success() {
        assert_eq!(divide(81, 9), Ok(9));
    }

    #[test]
    fn test_not_divisible() {
        assert_eq!(
            divide(81, 6),
            Err(DivisionError::NotDivisible(NotDivisibleError {
                dividend: 81,
                divisor: 6
            }))
        );
    }

    #[test]
    fn test_divide_by_0() {
        assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
    }

    #[test]
    fn test_divide_0_by_something() {
        assert_eq!(divide(0, 81), Ok(0));
    }

    #[test]
    fn test_result_with_list() {
        assert_eq!(format!("{:?}", result_with_list()), "Ok([1, 11, 1426, 3])");
    }

    #[test]
    fn test_list_of_results() {
        assert_eq!(
            format!("{:?}", list_of_results()),
            "[Ok(1), Ok(11), Ok(1426), Ok(3)]"
        );
    }
}
Solution
#![allow(unused)]
fn main() {
#[derive(Debug, PartialEq, Eq)]
pub enum DivisionError {
  NotDivisible(NotDivisibleError),
  DivideByZero,
}

#[derive(Debug, PartialEq, Eq)]
pub struct NotDivisibleError {
  dividend: i32,
  divisor: i32,
}

pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
  if b == 0 {
      return Err(DivisionError::DivideByZero);
  } else if a % b != 0 {
      return Err(DivisionError::NotDivisible(NotDivisibleError { dividend: a, divisor: b }));
  }
  Ok(a/b)
}

fn result_with_list() -> Result<Vec<i32>, DivisionError> {
  let numbers = vec![27, 297, 38502, 81];
  let division_results = numbers.into_iter().map(|n| divide(n, 27));
  let mut nums = vec![];
  for res in division_results {
      nums.push(res?);
  }
  Ok(nums)
  
}

fn list_of_results() -> Vec<Result<i32, DivisionError>> {
  let numbers = vec![27, 297, 38502, 81];
  let division_results = numbers.into_iter().map(|n| divide(n, 27)).collect();
  division_results
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_success() {
      assert_eq!(divide(81, 9), Ok(9));
  }

  #[test]
  fn test_not_divisible() {
      assert_eq!(
          divide(81, 6),
          Err(DivisionError::NotDivisible(NotDivisibleError {
              dividend: 81,
              divisor: 6
          }))
      );
  }

  #[test]
  fn test_divide_by_0() {
      assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
  }

  #[test]
  fn test_divide_0_by_something() {
      assert_eq!(divide(0, 81), Ok(0));
  }

  #[test]
  fn test_result_with_list() {
      assert_eq!(format!("{:?}", result_with_list()), "Ok([1, 11, 1426, 3])");
  }

  #[test]
  fn test_list_of_results() {
      assert_eq!(
          format!("{:?}", list_of_results()),
          "[Ok(1), Ok(11), Ok(1426), Ok(3)]"
      );
  }
}
}

Combinators 2

// Let's define a simple model to track bootcamp exercise progress. Progress
// will be modelled using a hash map. The name of the exercise is the key and
// the progress is the value. Two counting functions were created to count the
// number of exercises with a given progress. These counting functions use
// imperative style for loops. Recreate this counting functionality using
// iterators. Only the two iterator methods (count_iterator and
// count_collection_iterator) need to be modified.
// Make the code compile and the tests pass.

use std::collections::HashMap;

#[derive(Clone, Copy, PartialEq, Eq)]
enum Progress {
    None,
    Some,
    Complete,
}

fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
    let mut count = 0;
    for val in map.values() {
        if val == &value {
            count += 1;
        }
    }
    count
}

fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
    // map is a hashmap with String keys and Progress values.
    // map = { "variables1": Complete, "from_str": None, ... }
    todo!();
}

fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
    let mut count = 0;
    for map in collection {
        for val in map.values() {
            if val == &value {
                count += 1;
            }
        }
    }
    count
}

fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
    // collection is a slice of hashmaps.
    // collection = [{ "variables1": Complete, "from_str": None, ... },
    //     { "variables2": Complete, ... }, ... ]
    todo!();
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn count_complete() {
        let map = get_map();
        assert_eq!(3, count_iterator(&map, Progress::Complete));
    }

    #[test]
    fn count_equals_for() {
        let map = get_map();
        assert_eq!(
            count_for(&map, Progress::Complete),
            count_iterator(&map, Progress::Complete)
        );
    }

    #[test]
    fn count_collection_complete() {
        let collection = get_vec_map();
        assert_eq!(
            6,
            count_collection_iterator(&collection, Progress::Complete)
        );
    }

    #[test]
    fn count_collection_equals_for() {
        let collection = get_vec_map();
        assert_eq!(
            count_collection_for(&collection, Progress::Complete),
            count_collection_iterator(&collection, Progress::Complete)
        );
    }

    fn get_map() -> HashMap<String, Progress> {
        use Progress::*;

        let mut map = HashMap::new();
        map.insert(String::from("variables1"), Complete);
        map.insert(String::from("functions1"), Complete);
        map.insert(String::from("hashmap1"), Complete);
        map.insert(String::from("arc1"), Some);
        map.insert(String::from("as_ref_mut"), None);
        map.insert(String::from("from_str"), None);

        map
    }

    fn get_vec_map() -> Vec<HashMap<String, Progress>> {
        use Progress::*;

        let map = get_map();

        let mut other = HashMap::new();
        other.insert(String::from("variables2"), Complete);
        other.insert(String::from("functions2"), Complete);
        other.insert(String::from("if1"), Complete);
        other.insert(String::from("from_into"), None);
        other.insert(String::from("try_from_into"), None);

        vec![map, other]
    }
}
Solution
#![allow(unused)]
fn main() {
use std::collections::HashMap;

#[derive(Clone, Copy, PartialEq, Eq)]
enum Progress {
  None,
  Some,
  Complete,
}

fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
  let mut count = 0;
  for val in map.values() {
      if val == &value {
          count += 1;
      }
  }
  count
}

fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
  map.values().map(|v| if v==&value { 1 } else { 0 }).sum()
}

fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
  let mut count = 0;
  for map in collection {
      for val in map.values() {
          if val == &value {
              count += 1;
          }
      }
  }
  count
}

fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
  collection.iter().map(|h| count_iterator(h, value)).sum()
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn count_complete() {
      let map = get_map();
      assert_eq!(3, count_iterator(&map, Progress::Complete));
  }

  #[test]
  fn count_equals_for() {
      let map = get_map();
      assert_eq!(
          count_for(&map, Progress::Complete),
          count_iterator(&map, Progress::Complete)
      );
  }

  #[test]
  fn count_collection_complete() {
      let collection = get_vec_map();
      assert_eq!(
          6,
          count_collection_iterator(&collection, Progress::Complete)
      );
  }

  #[test]
  fn count_collection_equals_for() {
      let collection = get_vec_map();
      assert_eq!(
          count_collection_for(&collection, Progress::Complete),
          count_collection_iterator(&collection, Progress::Complete)
      );
  }

  fn get_map() -> HashMap<String, Progress> {
      use Progress::*;

      let mut map = HashMap::new();
      map.insert(String::from("variables1"), Complete);
      map.insert(String::from("functions1"), Complete);
      map.insert(String::from("hashmap1"), Complete);
      map.insert(String::from("arc1"), Some);
      map.insert(String::from("as_ref_mut"), None);
      map.insert(String::from("from_str"), None);

      map
  }

  fn get_vec_map() -> Vec<HashMap<String, Progress>> {
      use Progress::*;

      let map = get_map();

      let mut other = HashMap::new();
      other.insert(String::from("variables2"), Complete);
      other.insert(String::from("functions2"), Complete);
      other.insert(String::from("if1"), Complete);
      other.insert(String::from("from_into"), None);
      other.insert(String::from("try_from_into"), None);

      vec![map, other]
  }
}
}