Skip to content

Instantly share code, notes, and snippets.

@willie-hung
willie-hung / index.ts
Last active August 2, 2023 19:34
post_32
export function add(...args: number[]): number {
return args.reduce((a, b) => a + b, 0);
}
// in-source test suites
if (import.meta.vitest) {
const { it, expect } = import.meta.vitest;
it("add", () => {
expect(add()).toBe(0);
expect(add(1)).toBe(1);
import sum from "./sum";
import { describe, expect, it } from "vitest";
describe("#SUM", () => {
it("should return 0 for empty array", () => {
expect(sum()).toBe(0);
});
it("should return 1 for [1]", () => {
expect(sum(1)).toBe(1);
@willie-hung
willie-hung / sum.ts
Last active August 2, 2023 19:32
post_32
// Purpose: Function to sum a list of numbers.
export default function sum(...args: number[]): number {
return args.reduce((a, b) => a + b, 0);
}
@willie-hung
willie-hung / main.rs
Created August 1, 2023 17:47
post_31
fn main() {
let i = add_one(69);
println!("Hello, world! {}", i);
}
fn add_one(x: i32) -> i32 {
x + 1;
}
@willie-hung
willie-hung / main.rs
Created August 1, 2023 17:32
post_31
fn main() {
let i = add_one(69);
println!("Hello, world! {}", i);
}
fn add_one(x: i32) -> i32 {
x + 1
}
@willie-hung
willie-hung / main.rs
Created August 1, 2023 17:28
post_31
fn main() {
let i = {
// This is a statement
let j = 69;
// This is an expression
j + 1
};
println!("Hello, world! {}", i);
}
@willie-hung
willie-hung / main.rs
Created August 1, 2023 17:21
post_31
// This code doesn't compile!
fn main() {
let x = (let y = 0);
}
@willie-hung
willie-hung / main.rs
Created August 1, 2023 17:16
post_31
fn main() {
let x: i32 = 69;
}
@willie-hung
willie-hung / app.ts
Created August 1, 2023 01:41
post_30
app.get("/photos", async (req: Request, res: Response) => {
const albumId = req.query.albumId;
// Check the cache
const value = await client.get(`photos?albumId=${albumId}`);
if (value) {
console.log("Cache hit!");
res.json(JSON.parse(value));
} else {
console.log("Cache miss!");
@willie-hung
willie-hung / app.ts
Last active August 1, 2023 01:49
post_30
import express, { Request, Response } from "express";
import axios from "axios";
const app = express();
// Create Redis Client
import { createClient } from "redis";
const client = createClient();
client.on("error", (err) => console.log("Redis Client Error", err));