Add assignment 8

This commit is contained in:
Minseong Jang
2022-10-20 09:30:00 +09:00
parent c6805982d5
commit 871110175b
4 changed files with 113 additions and 0 deletions

33
scripts/grade-08.sh Executable file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -e
set -uo pipefail
IFS=$'\n\t'
# Imports library.
BASEDIR=$(dirname "$0")
source $BASEDIR/grade-utils.sh
RUNNERS=(
"cargo"
"cargo --release"
"cargo_asan"
"cargo_asan --release"
"cargo_tsan"
"cargo_tsan --release"
)
# Lints.
run_linters || exit 1
# Executes test for each runner.
for RUNNER in "${RUNNERS[@]}"; do
echo "Running with $RUNNER..."
TESTS=("--lib assignment08_grade")
if [ $(run_tests) -ne 0 ]; then
exit 1
fi
done
exit 0

View File

@@ -0,0 +1,48 @@
//! Assignment 8: First-class functions.
//!
//! The primary goal of this assignment is to get used to first-class functions.
//!
//! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade-08.sh` works fine.
//! See `assignment08_grade.rs` and `/scripts/grade-08.sh` for the test script.
/// Returns an anonymous function that applies the given function `f` for `n` times.
///
/// For instance, `repeat(3, f)(x)` roughly translates to `f(f(f(x)))`.
pub fn repeat<T, F: FnMut(T) -> T>(n: usize, mut f: F) -> impl FnMut(T) -> T {
todo!()
}
/// Applies the given function `f` for `i` times for the `i`-th element of the given vector.
///
/// For instance, `funny_map(f, [v0, v1, v2, v3])` roughly translates to `[v0, f(v1), f(f(v2)), f(f(f(v3)))]`.
pub fn funny_map<T, F: Fn(T) -> T>(f: F, vs: Vec<T>) -> Vec<T> {
todo!()
}
/// Either `T1`, or `T2`.
#[derive(Debug, PartialEq, Eq)]
pub enum Either2<T1, T2> {
/// Case 1.
Case1 {
/// The inner value.
inner: T1,
},
/// Case 1.
Case2 {
/// The inner value.
inner: T2,
},
}
impl<T1, T2> Either2<T1, T2> {
/// Maps the inner value.
///
/// If the inner value is case 1, apply `f1`, and if it is case 2, apply `f2`.
pub fn map<U1, U2, F1, F2>(self, f1: F1, f2: F2) -> Either2<U1, U2>
where
F1: FnOnce(T1) -> U1,
F2: FnOnce(T2) -> U2,
{
todo!()
}
}

View File

@@ -0,0 +1,30 @@
#[cfg(test)]
mod test {
use super::super::assignment08::*;
#[test]
fn test_repeat() {
for i in 0..10 {
assert_eq!(42 + 2 * i, repeat(i, |x| x + 2)(42));
}
}
#[test]
fn test_funny_map() {
assert_eq!(
vec![0, 3, 6, 9, 12],
funny_map(|x| x + 2, vec![0, 1, 2, 3, 4])
);
}
#[test]
fn test_either2_map() {
let v1 = Either2::<u32, f32>::Case1 { inner: 42 };
let u1 = Either2::<u32, f32>::Case1 { inner: 43 };
assert_eq!(u1, v1.map(|i| i + 1, |f| f + 1.0));
let v2 = Either2::<u32, f32>::Case2 { inner: 42.0 };
let u2 = Either2::<u32, f32>::Case2 { inner: 43.0 };
assert_eq!(u2, v2.map(|i| i + 1, |f| f + 1.0));
}
}

View File

@@ -15,3 +15,5 @@ pub mod assignment06;
mod assignment06_grade;
pub mod assignment07;
mod assignment07_grade;
pub mod assignment08;
mod assignment08_grade;