Skip to content

Instantly share code, notes, and snippets.

View yxy's full-sized avatar

BBB yxy

  • gggg
View GitHub Profile
@yxy
yxy / two-railway-programing-in-python.py
Last active September 16, 2021 17:03
Railway Oriented Programming in python. inspired by https://fsharpforfunandprofit.com/rop/
class Success(object):
def __init__(self, value):
self.value = value
class Error(object):
def __init__(self, value):
self.value = value
class wrapper(object):
def __init__(self, result):
@yxy
yxy / Heap.py
Created September 21, 2016 14:47
Generic Maximum or Minimum Heap implemenation
class Heap(object):
def __init__(self, test_func):
self._data = []
self._pos = 0
self.test_func = test_func
def size(self):
return self._pos
def add(self, val):
@yxy
yxy / white-falcon.py
Last active August 11, 2016 17:18
White Falcon And Tree Problem
# TODO: this program too slow, got be some nice solution. :(
# some graph related knownledge
def f(node, x):
return node[0] * x + node[1]
paths = {}
def cache(fn):
def _(u, v, *args, **kwargs):
@yxy
yxy / sample_usage.py
Created June 24, 2016 09:06 — forked from luke14free/sample_usage.py
Simple type checked objects in Python
#!/usr/bin/env python
from type_checked_entities import entity_factory
Giraffe = entity_factory( # let's define what is a giraffe!
"giraffe",
name=str, # my name is a string
age=float, # my age is an int
eats=object, # I eat pretty much everything.
)
"""
A mutlimethod decorator, dispatch based on the input types.
"""
registry = {}
def multimethod(*types):
def wrapper(fn):
# registry key=>func
_types = tuple(t for t in types)
registry[_types] = fn
@yxy
yxy / flatten.js
Last active May 13, 2016 09:01
flatten array snippet
flatten = function (a, b) {
if (!Array.isArray(a)) {
a = [a];
}
return a.concat(b);
}
// usage
var aa = [[1], [2, 3], [4, 5]];
@yxy
yxy / aserver.py
Last active March 10, 2016 09:47
# server.py
# fib microservice
from socket import *
# from threading import Thread
from collections import deque
from select import select
from concurrent.futures import ThreadPoolExecutor as Pool
from concurrent.futures import ProcessPoolExecutor as Pool
@yxy
yxy / wc.l
Created February 29, 2016 07:42
unix tool wc written in lex
%{
int nchar, nword, nline;
%}
%%
\n { nline++; nchar++ ;}
[^ \t\n]+ { nword++; nchar += yyleng; }
. { nchar++; }
%%
#-*- encoding: utf8 -*-
from sre_parse import Pattern, SubPattern, parse
from sre_compile import compile as sre_compile
from sre_constants import BRANCH, SUBPATTERN
class Scanner(object):
def __init__(self, rules, flags=0):
@yxy
yxy / simple_thread_pool.py
Created October 29, 2015 09:06
simple thread pool workers
#-*- encoding: utf8 -*-
import pickle
import sys
from threading import Thread
PY2 = sys.version_info[0] == 2
if PY2:
from Queue import Queue