Skip to content

Instantly share code, notes, and snippets.

@zhy0216
zhy0216 / either.ts
Created February 1, 2023 14:09
typescript Either<L, R>
export abstract class Either<L, R> {
left!: L;
right!: R;
static Right = <T>(v: T) => new Right(v);
static Left = <T>(v: T) => new Left(v);
abstract map<T>(f: (a: R) => T): Either<L, T>;
abstract flatMap<T>(f: (a: R) => Either<L, T>): Either<L, T>;
abstract orElse<T>(a: Either<L, T>): Either<L, R>;
@zhy0216
zhy0216 / antd-upload-retry.ts
Created December 17, 2021 07:53
custom antd upload itemRender
// i tried to add retry button on antd upload list
// but no easy way to do it
// so i hack it in the following way
// the idea is interesting because i am kind of "modifying" React Node children
import produce from "immer"
const itemRender = (originNode: React.ReactElement, file: UploadFile, fileList: UploadFile[]) => {
if(file.status === "error") {
const {prefix} = this.props
@zhy0216
zhy0216 / camel_to_snake_case.py
Last active March 30, 2017 03:21
Camel to Snake case
def camel_to_snake_case(name: str) -> str:
# ord('a') == 97
r = []
new_group = [name[0].lower()]
for c in name[1:]:
if 97 <= ord(c) <= 122:
new_group.append(c)
else:
r.append("".join(new_group))
new_group = [c.lower()]
class Solution(object):
def invertTree(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if root is None:
return root
if root.left:
@zhy0216
zhy0216 / responsive-table.js
Created April 7, 2015 13:52
responsive table
$(function(){
var threadNames = [];
$('th').each(function(i){
threadNames.push($(this).html());
});
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = "@media "+
@zhy0216
zhy0216 / autocrop.py
Created March 28, 2015 01:53
Remove any unnecessary whitespace from the edges of the source image
# copy from easy_thumbnails
from PIL import Image, ImageChops
def is_transparent(image):
"""
Check to see if an image is transparent.
"""
if not isinstance(image, Image.Image):
# Can only deal with PIL images, fall back to the assumption that that
# it's not transparent.
@zhy0216
zhy0216 / gist:ed2cb6d57671b3701b72
Last active August 29, 2015 14:04
permutation.py
def gen_permutation(string):
if len(string) == 1:
yield string
for char in string:
rest = string.replace(char, "")
for p_string in gen_permutation(rest):
yield char + p_string
# -*- coding: utf-8 -*-
import codecs
STATE = "IDEL" # INSERT, SEQUENCE
f = open("1.sql", "r")
f_w = codecs.open("test.sql", 'w', 'utf-8')
@zhy0216
zhy0216 / gist:8398685
Created January 13, 2014 11:22
useful app
ngrok: https://ngrok.com/ # Introspected tunnels to localhost
@zhy0216
zhy0216 / font
Last active January 3, 2016 02:09
font collection
Redressed: [https://www.google.com/fonts/specimen/Redressed](https://www.google.com/fonts/specimen/Redressed)