TypeScript#
Generic fetch wrapper with typed response:
async function fetchJson<T>(url: string, init?: RequestInit): Promise<T> {
const res = await fetch(url, init)
if (!res.ok) throw new Error(`HTTP ${res.status}: ${res.statusText}`)
return res.json() as Promise<T>
}
// Usage
const posts = await fetchJson<Post[]>('/api/posts')
Python#
Fibonacci with memoisation using functools.cache:
from functools import cache
@cache
def fib(n: int) -> int:
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
print([fib(i) for i in range(10)])
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Rust#
Ownership and borrowing example:
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() { x } else { y }
}
fn main() {
let s1 = String::from("long string");
let result;
{
let s2 = String::from("xyz");
result = longest(s1.as_str(), s2.as_str());
println!("longest: {result}");
}
}
Shell#
Deploy script snippet:
#!/usr/bin/env bash
set -euo pipefail
IMAGE="my-app:$(git rev-parse --short HEAD)"
docker build -t "$IMAGE" .
docker push "$IMAGE"
kubectl set image deployment/my-app app="$IMAGE" --record
echo "Deployed $IMAGE"
JSON config#
{
"compilerOptions": {
"target": "ES2022",
"module": "esnext",
"moduleResolution": "bundler",
"strict": true,
"paths": {
"@/*": ["./*"]
}
}
}