Skip to content

Instantly share code, notes, and snippets.

@hackwaly
hackwaly / pipe_buffer.js
Last active December 26, 2015 20:39
simply pipe like buffer for nodejs, usefull for parsing incoming chunks.
function PipeBuffer(){
this._buffer = [];
this._buffered = 0;
}
PipeBuffer.prototype.push = function (chunk){
this._buffer.push(chunk);
this._buffered += chunk.length;
};
PipeBuffer.prototype.unshift = function (chunk){
this._buffer.unshift(chunk);
@hackwaly
hackwaly / red_black_tree.ts
Created March 10, 2015 15:26
Red black tree implemented in typescript
enum Color {
RED,
BLACK
}
class Node {
public parent: Node;
public left: Node;
public right: Node;
function *fibonacci() {
var i = 0;
var j = 1;
while (true) {
yield i;
i = i + j;
yield j;
j = i + j;
}
}
@hackwaly
hackwaly / coroutine.js
Last active October 28, 2020 02:30
Coroutine, go and call/cc implemented in javascript (use generator, less then 100 line).
var go;
var callcc;
(function () {
function noop() {}
var iteratorConstructor = function* () {}().constructor;
function isIterator(p) {
return p != null && p.constructor === iteratorConstructor;
}
@hackwaly
hackwaly / test_lazy.js
Created March 28, 2015 16:07
测试对象版本的lazy的性能
function RangeStream(start, end) {
this._start = start;
this._end = end;
}
RangeStream.prototype.writeTo = function (stream) {
for (var i = this._start; i < this._end; i++) {
stream.write(i);
}
};
@hackwaly
hackwaly / gist:d1c0b43c23cf8e19c8f4
Created May 21, 2015 09:17
A solution for Youtube's pjax problem.

I've noticed that Youtube use pjax for page navigation a long time.

It strongly improved the user experience. But there's a problem not solved: After user click a link. Instead of showing loading progress by browser, the loading progress is showing by Youtube's page. And before target page load complete, user can't cancel the navigation (through browser's Stop command).

Maybe, that's one reason of why Youtube provide a in-page progress bar. Without it, user can't know whether page is navigating or not.

I'll introduce a solution for it (demo). It makes browser show loading status (yes, you can use in-page progress bar same time), and allow user use Stop command cancel navigation.

@hackwaly
hackwaly / main.ml
Created May 13, 2016 02:24
ocaml + llvm
open Ctypes
open Foreign
let main () =
ignore (Llvm_executionengine.initialize ());
let ctx = Llvm.global_context () in
let m_main = Llvm.create_module ctx "hello" in
let builder = Llvm.builder ctx in
let t_i32 = Llvm.i32_type ctx in
let ft_main = Llvm.function_type t_i32 [||] in
@hackwaly
hackwaly / main.hs
Created May 31, 2016 11:26
Haskell type classes test
import Test (MyInt(MyInt))
import Test1 (test1)
import Test2 (test2)
main = do
putStrLn $ show (test1 MyInt MyInt)
putStrLn $ show (test2 MyInt MyInt)
@hackwaly
hackwaly / vue-load-route-data-mixin.js
Last active July 10, 2019 22:05
A simple vuejs mixin allow your vue-router page component load data async before route enter
export default (loader) => {
return {
created() {
let data = this.$route.meta.__data;
Object.assign(this.$data, data);
},
async beforeRouteEnter(to, from, next) {
let data = await loader(to);
to.meta.__data = data;
next();
@hackwaly
hackwaly / unify.ml
Last active May 4, 2023 00:11
Unify algorithm implemented in ocaml according to Alin Suciu's "Yet Another Efficient Unification Algorithm"
module type TERM = sig
type t
type tvar
val var_opt : t -> tvar option
val compare_var : tvar -> tvar -> int
val same_functor : t -> t -> bool
val args : t -> t list
end
module Make (Term : TERM) = struct