# Idioms

How to write common things with the current subset (`i32`, `bool`,
arithmetic, comparisons, functions, calls, variables, blocks, `if`, `while`). Each idiom names what replaces it once
the missing feature arrives.

## Name a value with `let`

To use a value twice without computing it twice, bind it with `let`. Prefer
`let`; use `var` only for a value that really changes.

```mul
// expect: 49
module demo

pub fn main() -> i32 {
    let side = 3 + 4
    return side * side
}
```

## Accumulate with `var`

A `var` collects a result step by step. Each assignment is evaluated in order,
so the first overflow stops the program at that step.

```mul
// expect: 1230
module demo

pub fn main() -> i32 {
    let net = 250
    var total = net * 4
    total = total + total * 23 / 100
    return total
}
```

## Limit a temporary to a block

Wrap helper values in `{ ... }` so they cannot be used by mistake later, and
so their names can be reused:

```mul
// expect: 30
module demo

pub fn main() -> i32 {
    var sum = 0
    {
        let part = 10
        sum = sum + part
    }
    {
        let part = 20
        sum = sum + part
    }
    return sum
}
```

## Name a constant with a function

There are no constants. A function without parameters plays that role:

```mul
// expect: 86400
module demo

fn secondsPerMinute() -> i32 {
    return 60
}

fn minutesPerDay() -> i32 {
    return 24 * 60
}

pub fn main() -> i32 {
    return secondsPerMinute() * minutesPerDay()
}
```

## Round instead of truncating

`/` truncates toward zero. For non-negative values, add half of the divisor
before dividing to round to the nearest integer:

```mul
// expect: 4
module demo

fn roundDiv(a: i32, b: i32) -> i32 {
    return (a + b / 2) / b
}

pub fn main() -> i32 {
    return roundDiv(5, 2) + roundDiv(4, 3) - roundDiv(1, 4)
}
```

## Non-negative remainder

`%` takes the sign of the dividend. For a positive `m`, `((a % m) + m) % m`
always lies in `0..m`:

```mul
// expect: 1
module demo

fn mod(a: i32, m: i32) -> i32 {
    return ((a % m) + m) % m
}

pub fn main() -> i32 {
    return mod(-7, 2)
}
```

## Loop with a counter

There is no `for`. Declare the counter as a `var` before a `while` and step
it at the end of the body. Prefer a loop to recursion for long iterations: a
loop uses no call depth (recursion stops at 10 000 calls, `MUL-R005`).

```mul
// expect: 21
module demo

fn fib(n: i32) -> i32 {
    var a = 0
    var b = 1
    var i = 0
    while i < n {
        let next = a + b
        a = b
        b = next
        i = i + 1
    }
    return a
}

pub fn main() -> i32 {
    return fib(8)
}
```

## Leave a loop early

There is no `break`. Put the loop in its own function and `return` from the
body, or add a `bool` `var` to the condition:

```mul
// expect: 4
module demo

fn firstSquareAbove(limit: i32) -> i32 {
    var n = 1
    while true {
        if n * n > limit {
            return n
        }
        n = n + 1
    }
    return 0
}

pub fn main() -> i32 {
    return firstSquareAbove(10)
}
```

## Return early

Handle special cases first with an `if` that returns; the rest of the function
then needs no `else`:

```mul
// expect: 4
module demo

fn safeDiv(a: i32, b: i32) -> i32 {
    if b == 0 {
        return 0
    }
    return a / b
}

pub fn main() -> i32 {
    return safeDiv(9, 2) + safeDiv(1, 0)
}
```

## `bool` to `i32`

There are no conversions. Choose the number with `if`:

```mul
// expect: 2
module demo

fn count(flag: bool) -> i32 {
    if flag {
        return 1
    }
    return 0
}

pub fn main() -> i32 {
    return count(3 > 2) + count(true) + count(1 == 2)
}
```

## Guard a risky operand

`&&` skips its right operand when the left one is `false`, so a check can
protect the operation after it:

```mul
// expect: 1
module demo

pub fn main() -> i32 {
    let d = 0
    if d != 0 && 10 / d > 1 {
        return 2
    }
    return 1
}
```

## Style

- Use `camelCase` for function, parameter and variable names.
- Declare a variable right before its first use, in the smallest block that needs it.
- Mark `main` and functions meant for other modules `pub`; keep helpers private.
- Prefer parentheses over relying on precedence when mixing `%` with `+`/`-`,
  or `&&` with `||`.
- Name conditions with a `let` (`let inRange = low <= x && x <= high`) when
  they are long or used twice.
