Rustコトハジメ

プログラミング言語Rustと競プロに関する情報をお届けします。

Implement crazy math teacher in Rust

Teacher is the absolute

Foreign people often say that Japanese people are obedient to rules: they never cross the road while the signal is red, they keep the side in escalator and never run, they are always on time etc. This is because Japanese people are trained since in the elementary school and before.

In Japanese elementary school, kids are tough to never break rules and the teacher is the rule. If teacher says the crow is white and students should never say crow is black no matter what the truth is.

Ordering of multiplication problem

In the same way, if the teacher says the area of the rectangle should be calculated in height x width manner it is the rule. If a kid calculates it by width x height then the teacher gives him x; sometimes the teacher calls up his parents to school and have a parent-student-teacher meeting for an hour to blame the parent's home education for the rebellious attitude of the student. In Japan, this is known as "Ordering of multiplication problem". The problem has been known back to 1970s and the same education has been given for half a century and still it is going on.

f:id:akiradeveloper529:20190307140847j:plain

Let's write a joke program to emulate the crazy teacher

In this post I will implement a joke program to statically reject width x height calculation.

We can do this by wrapping values by a type like Height and Width and then implements Height x Width multiplication, let the opposite undefined. I've heard some of the industries do this kind of wrapping by type technique to avoid the raw numbers are used in wrong way like Yen + Dollar and the technique is called value type.

Here it is:

use std::ops::{Add, Mul};

#[derive(Copy, Clone)]
struct Height(i32);
#[derive(Copy, Clone)]
struct Width(i32);
#[derive(Debug)]
struct Area(i32);

impl Mul<Width> for Height {
    type Output = Area;
    fn mul(self, rhs: Width) -> Area {
        Area(self.0 * rhs.0)
    }
}
impl Add for Area {
    type Output = Area;
    fn add(self, rhs: Area) -> Area {
        Area(self.0 + rhs.0)
    }
}

fn main() {
    let heightA = Height(5);
    let widthA = Width(3);
    // let areaA = widthA * heightA; // can't compile
    let areaA = heightA * widthA;
    
    let heightB = Height(3);
    let widthB = Width(7);
    // let areaB = widthB * heightB; // can't compile
    let areaB = heightB * widthB;
    
    let areaAB = areaA + areaB;
    println!("area is {:?}", areaAB);
}
area is Area(36)

Other problem: Human pyramid

Other than the ordering of multiplication problem there are lot of ridiculous things in elementary school in Japan. Human pyramid is one of them.

f:id:akiradeveloper529:20190113193428j:plain

Kids are forced to do this in P.E. Some kid have ever been dead by accident but still it is going on to teach them how important to support the elderlies in companies and society.

f:id:akiradeveloper529:20190307140901j:plain

Japan is not a good country. Only desperation (絶望 - Zetsubou) is here.