Vary Course

Vary Introduction

Ten short programs that show what Vary is for: typed code, pure boundaries, contracts, and tests strong enough to survive mutation.

Why Vary exists

Readable code with visible types

Compose a deploy label from typed parts.

def deploy_label(env: Str, region: Str) -> Str {
    return env + "-" + region
}

print(deploy_label("prod", "us-east-1"))

Run: vary run main.vary

Expected output:

prod-us-east-1

Separate calculation from effects

Sum response bytes in a pure helper and print at the edge.

data Chunk {
    label: Str
    bytes: Int
}

pure def total_bytes(chunks: List[Chunk]) -> Int {
    mut sum = 0
    for chunk in chunks {
        sum = sum + chunk.bytes
    }
    return sum
}

let response = [
    Chunk("headers", 412),
    Chunk("body", 2048),
    Chunk("trailers", 96),
]

print("bytes=" + str(total_bytes(response)))

Run: vary run main.vary

Expected output:

bytes=2556

Think about test strength

Compute an exponential backoff so the next lessons have an obvious answer.

pure def backoff_ms(attempt: Int) -> Int {
    if attempt <= 0 {
        return 100
    }
    if attempt == 1 {
        return 200
    }
    if attempt == 2 {
        return 400
    }
    return 800
}

print("backoff=" + str(backoff_ms(2)) + "ms")

Run: vary run main.vary

Expected output:

backoff=400ms

Language choices

Model a closed choice

Choose a release path with an enum and a top-level match.

enum ReleaseKind {
    PATCH
    MINOR
    MAJOR
}

let release = ReleaseKind.MINOR

match release {
    case ReleaseKind.PATCH {
        print("release=patch")
    }
    case ReleaseKind.MINOR {
        print("release=minor")
    }
    case ReleaseKind.MAJOR {
        print("release=major")
    }
}

Run: vary run main.vary

Expected output:

release=minor

Make mutation explicit

Count rate-limited responses while keeping the response list immutable.

let responses: List[Int] = [200, 200, 429, 200, 503, 429, 200]
mut throttled = 0

for status in responses {
    if status == 429 {
        throttled = throttled + 1
    }
}

print("throttled=" + str(throttled))

Run: vary run main.vary

Expected output:

throttled=2

Target the JVM

Describe a deploy target with typed fields.

data DeployTarget {
    service: Str
    runtime: Str
    arch: Str
}

let target = DeployTarget("billing-api", "JVM 21", "linux/amd64")
print(target.service + " -> " + target.runtime + " on " + target.arch)

Run: vary run main.vary

Expected output:

billing-api -> JVM 21 on linux/amd64

Name assumptions

Return the first non-empty message and declare both ends of the contract.

pure def first_message(messages: List[Str]) -> Str {
    in {
        len(messages) > 0
    }
    out (value) {
        len(value) > 0
    }
    for message in messages {
        if len(message) > 0 {
            return message
        }
    }
    return "unknown"
}

let errors = ["", "timeout after 30s", "connection refused"]
print("first=" + first_message(errors))

Run: vary run main.vary

Expected output:

first=timeout after 30s

Workflow

Check before running

Define a typed record and a pure predicate suitable for vary check.

data Response {
    endpoint: Str
    status: Int
}

pure def is_2xx(response: Response) -> Bool {
    return response.status >= 200 and response.status < 300
}

let response = Response("/api/users", 200)

if is_2xx(response) {
    print("checked: " + response.endpoint)
}

Run: vary run main.vary

Expected output:

checked: /api/users

Prefer observable outputs

Compute a health verdict in a pure helper that a future test can assert against.

pure def health(failing: Int) -> Str {
    if failing == 0 {
        return "green"
    }
    if failing <= 2 {
        return "amber"
    }
    return "red"
}

let probe_results = [True, True, False, True]
mut failing = 0

for ok in probe_results {
    if not ok {
        failing = failing + 1
    }
}

print(health(failing))

Run: vary run main.vary

Expected output:

amber

The confidence loop

Sum the timeout budget across a typed list of pipeline stages.

data Stage {
    name: Str
    timeout_s: Int
}

let pipeline = [
    Stage("compile", 60),
    Stage("unit", 30),
    Stage("integration", 120),
    Stage("deploy", 90),
]

mut budget = 0
for stage in pipeline {
    budget = budget + stage.timeout_s
}

print("pipeline budget=" + str(budget) + "s")

Run: vary run main.vary

Expected output:

pipeline budget=300s