Skip to content

Instantly share code, notes, and snippets.

View nbdd0121's full-sized avatar

Gary Guo nbdd0121

View GitHub Profile
@nbdd0121
nbdd0121 / Stream.js
Created December 24, 2014 12:19
Stream (like that of Java 8) for ES.next
class Stream {
constructor(content) {
this._content = content;
}
static from(arr) {
if (!arr[Symbol.iterator]) {
arr = Array.from(arr);
}
return new ArrayStream(arr);
}
@nbdd0121
nbdd0121 / mark-sweep-gc.cc
Created January 24, 2015 06:18
Simple mark-and-sweep GC (coded as a beginner of C++)
#include <iostream>
#include <vector>
#define debug(statement) statement
// #define debug(statement)
// Above one for release use
/* Declarations */
class GCObject;
@nbdd0121
nbdd0121 / decltype-test.cc
Created February 6, 2015 06:56
Test on degree of support of decltype on various compilers
#include <iostream>
template<typename T>
class Pointer{
private:
T* ptr_;
public:
Pointer(T* ptr): ptr_(ptr) {}
operator T*() const {
return ptr_;
@nbdd0121
nbdd0121 / apt-cyg
Created February 6, 2015 09:19
apt-cyg for Chinese users
#!/bin/bash
# vim: set ts=2 sw=2 tw=0 et :
#
# apt-cyg: install tool for cygwin similar to debian apt-get
#
# Copyright (C) 2005-9, Stephen Jungels
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
@nbdd0121
nbdd0121 / 24-points-ver1.js
Last active August 29, 2015 14:14
24-points javascript calculator unoptimized / no parenthesis removal / only single answer is shown
function wrap(num) {
return ' ' + num + ' ';
}
function singleStep(ans, a, b, arr, i, target, sym) {
arr[i] = ans;
return (target = calc24(arr, target)) &&
target.replace(wrap(ans), '(' + wrap(a) + sym + wrap(b) + ')');
}
@nbdd0121
nbdd0121 / bf-nlir-asm.asm
Created March 12, 2015 13:09
Brainf*ck Norlit IR Compiled to Assembly
[bits 64]
[global norlit.lib.zeroinitialize]
[extern memset]
norlit.lib.zeroinitialize:
mov r8, rdx
mov rdx, 0
jmp memset
[extern putchar]
[extern getchar]
[global main]
#include <iostream>
#include <vector>
int main(){
int length;
std::cin >> length;
int* data = new int[length];
for(int i=0;i<length;i++){
std::cin >> data[i];
}
@nbdd0121
nbdd0121 / promise.cc
Created April 29, 2015 11:29
C++ Promise via Coroutine
#include <functional>
#include <cstdio>
#include <vector>
#include <queue>
#include <memory>
#include "Coroutine.h"
using norlit::coroutine::Coroutine;
@nbdd0121
nbdd0121 / shadowdom-css.js
Created August 9, 2015 12:53
A CSS encapsulation polyfill that works on top of WebComponents.js
/*
* Copyright (c) 2015, Gary Guo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
@nbdd0121
nbdd0121 / timezone.js
Created September 28, 2018 21:55
Bilibili Timeline Timezone Fix
let timezoneOffset = 8 * 60; // in minutes
function timeshift(date) {
return new Date(+date + timezoneOffset * 60000);
}
// Seconds and Milliseconds are always same as timezone offset is always multiple of 0.5
for (let name of ['Date', 'Day', 'FullYear', 'Hours', 'Minutes', 'Month']) {
Date.prototype['get' + name] = function () {
return timeshift(this)['getUTC' + name]();