Skip to content

Instantly share code, notes, and snippets.

@breeze1990
breeze1990 / main.cpp
Last active December 23, 2022 05:46
detect if a class has specific static field of specific type in C++
#include <iostream>
#include "type_checker.h"
typedef struct payload {} payload;
typedef struct payload2 {
static std::string id;
} payload2;
std::string payload2::id;
@breeze1990
breeze1990 / import.py
Last active September 14, 2022 04:17
Python: recursively import modules under a folder. https://stackoverflow.com/a/25562415/4107682
import importlib
import pkgutil
def import_submodules(package, recursive=True):
""" Import all submodules of a module, recursively, including subpackages
:param recursive: bool
:param package: package (name or actual module)
:type package: str | module
window.globalVar = 0;
function eventHandler() { // handler
if (window.globalVar == 0) {
setTimeout(function() { // inside callback
window.globalVar = window.globalVar + 1;
});
}
}
// Intuitively window.globalVar won't exceed 1 no matter how many times the eventHandler is triggered
// But consider the case: the eventHandler is attached to click event and two consecutive clicks happen so fast that
@breeze1990
breeze1990 / replace_gotcha.js
Created June 12, 2015 00:33
JavaScript string replace with subgroups
// \1 \2... can refer to matched subgroup and be used in regex
// $1 $2... can refer to matched subgroup and be used in newString
//
"abba".replace(/(a)(b)\2\1/g,"$2$2$1$1$1") // =="bbaaa"
// What if I want to insert "$1" literally in newString
// $$ can be used
"abba".replace(/(a)(b)\2\1/g,"$$1$2$1") // =="$1ba"
@breeze1990
breeze1990 / gist:ffcf00f07293d3195ad3
Last active August 29, 2015 14:15
Nested loop equivalent. for i=1 to n, for j=i+1 to n;
comb lst = [0..(length lst-1)] >>= \i -> drop (i+1) lst >>= \j -> return (lst !! i, j)
package main
import (
"fmt"
)
type Fetcher interface {
// Fetch returns the body of URL and
// a slice of URLs found on that page.
Fetch(url string) (body string, urls []string, err error)