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
# -*- 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 / 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 / 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)
@zhy0216
zhy0216 / collection.css
Created December 16, 2013 01:55
some useful or tricky css
.yue {
font: 400 18px/1.62 "Georgia", "Xin Gothic", "Hiragino Sans GB", "WenQuanYi Micro Hei", "Microsoft YaHei", "SimSun", sans-serif;
color: #333332;
}
@zhy0216
zhy0216 / router
Created November 15, 2013 05:23
an easy js router
/**
Usage:
app.addRoute("/user/<userid>/", function(){
console.log("userid:", this.userid);
}).addRoute("/post/<postid>/", function(){
console.log("postid:", this.postid);
}).addRoute("/post/<postid>/comments", function(){
console.log("comment of post");
})
app.startMain("/user/1/"); // >>> userid: 1
@zhy0216
zhy0216 / sublime-keymap
Created October 28, 2013 03:17
sublime keybinding
[
{ "keys": ["ctrl+d"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Line.sublime-macro"} },
{ "keys": ["ctrl+up"], "command": "swap_line_up" },
{ "keys": ["ctrl+down"], "command": "swap_line_down" },
{ "keys": ["ctrl+alt+l"], "command": "beautify" },
]