Skip to content

Instantly share code, notes, and snippets.

View nomyfan's full-sized avatar
🍵
得閑飲茶

Kim Chan nomyfan

🍵
得閑飲茶
View GitHub Profile
@nomyfan
nomyfan / main.py
Last active August 16, 2023 08:52
learn-you-a-python
import numpy as np
print("List comprehensions")
print([x**2 for x in range(10) if x % 2 == 0])
print("Nested list comprehensions")
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
@nomyfan
nomyfan / package.json
Created July 24, 2023 15:55
npm-optional-dependencies
{
"name": "optional-deps-demo",
"version": "1.0.0",
"optionalDependencies": {
"@esbuild/darwin-arm64": "^0.18.16",
"@esbuild/win32-x64": "^0.18.16"
}
}
@nomyfan
nomyfan / calculator.ts
Last active April 29, 2023 09:14
Calculate a image size to fit different platforms
type Width = number;
type Height = number;
type AspectRatio = [Width, Height];
type LowerUpperBounds = [number, number];
function unreachable(): never {
throw new Error("unreachable!");
}
function calculate(width: number, height: number, options: { ratioRange: LowerUpperBounds }): AspectRatio {
@nomyfan
nomyfan / App.tsx
Created March 20, 2023 14:37
react-hook-form + zod
import { useForm, Controller } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import * as z from 'zod';
const schema = z.object({
name: z.string().min(1, { message: 'Required' }).url(),
age: z.coerce
.number()
.default(0)
.refine(
@nomyfan
nomyfan / index.js
Created February 6, 2023 06:53
JavaScript strict mode
/**
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#strict_mode_for_modules
* @param a
* @param b
*/
function esmIsStrictByDefault(a: number, b = 2) {
return a + b;
}
/**
@nomyfan
nomyfan / index.js
Created February 6, 2023 02:51
Sourcemap
function greet(name) {
const prefix = "hello";
return prefix + " " + name;
}
@nomyfan
nomyfan / CovariantContravariant.cs
Created October 14, 2022 14:57
Covariant vs. Contravariant
using System;
using System.Collections.Generic;
using System.Reflection.Metadata.Ecma335;
namespace CovariantContravariant
{
class Program
{
static void Main(string[] args)
{
@nomyfan
nomyfan / main.rs
Last active September 26, 2022 15:37
Rust Fn vs. FnMut vs. FnOnce
#[derive(Copy, Clone)]
struct Foo {
val: i32,
}
impl Foo {
fn new(val: i32) -> Self {
Foo { val }
}
}
@nomyfan
nomyfan / observable.ts
Last active September 4, 2022 05:23
Basic implementation of observable
// https://youtu.be/m40cF91F8_A
interface Observer<T> {
next: (value: T) => void;
error: (error: any) => void;
complete: () => void;
}
interface Unsubscriable {
unsubscribe: () => void;
@nomyfan
nomyfan / App.css
Created August 28, 2022 05:19
Color tinting - wrapper solution
.box {
width: 300px;
height: 250px;
background-color: cornflowerblue;
transition: 0.5s background-color;
background-size: cover;
background-position: center;
}