Skip to content

Instantly share code, notes, and snippets.

View jasonlvhit's full-sized avatar
💭
I may be slow to respond.

辣椒面 jasonlvhit

💭
I may be slow to respond.
View GitHub Profile

这两天过了遍Lua,Lua的一个语言特性是支持尾递归,这是我接触的第一个支持尾递归的语言(尾递归优化)

什么是尾递归

尾递归,是尾调用的一种类型,后者更加准确,因为实际的正确尾递归(Proper Tail Recursion)是不涉及递归的。尾调用是指一个函数里的最后一个动作是一个函数调用的情形:即这个调用的返回值直接被当前函数返回的情形。这种情形下称该调用位置为尾位置。若这个函数在尾位置调用本身(或是一个尾调用本身的其他函数等等),则称这种情况为尾递归,是递归的一种特殊情形。尾调用不一定是递归调用,但是尾递归特别有用,也比较容易实现。

我们直接的来看一下Lua中尾调用:

function f(x)
@jasonlvhit
jasonlvhit / URL_re.py
Last active August 29, 2015 13:57
URL regex representation
import re
cctlds = ['AC','AD','AE','AF','AG','AI','AL','AM','AN','AO','AQ','AR','AS','AT','AU','AW','AX','AZ','BA','BB','BD',\
'BE','BF','BG','BH','BI','BJ','BL','BM','BN','BO','BQ','BR','BS','BT','BV','BW','BY','BZ','CA','CC','CD',\
'CF','CG','CH','CI','CK','CL','CM','CN','CO','CO','CR','CU','CV','CW','CX','CY','CZ','DE','DJ','DK','DM',\
'DO','DZ','EC','EE','EG','EH','ER','ES','ET','EU','FI','FJ','FK','FM','FO','FR','GA','GB','GD','GE','GF',\
'GG','GH','GI','GL','GM','GN','GP','GQ','GR','GS','GT','GU','GW','GY','HK','HM','HN','HR','HT','HU','ID',\
'IE','IL','IM','IN','IO','IQ','IR','IS','IT','JE','JM','JO','JP','KE','KG','KH','KI','KM','KN','KP','KR',\
'KW','KY','KZ','LA','LB','LC','LI','LK','LR','LS','LT','LU','LV','LY','MA','MC','MD','ME','MF','MG','MH',\
'MK','ML','MM','MN','MO','MP','MQ','MR','MS','MT','MU','MV','MW','MX','MY','MZ','NA','NC','NE','NF','NG',\
@jasonlvhit
jasonlvhit / escape.py
Created March 15, 2014 11:58
Python 3.3 Lib/html/__init__.py
_escape_map = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;'}
_escape_map_full = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;',
ord('"'): '&quot;', ord('\''): '&#x27;'}
# NB: this is a candidate for a bytes/string polymorphic interface
def escape(s, quote=True):
"""
Replace special characters "&", "<" and ">" to HTML-safe sequences.
If the optional flag quote is true (the default), the quotation mark
@jasonlvhit
jasonlvhit / HeapSort.py
Created March 4, 2014 13:06
堆排序
#heap sort
#intruduction to algorithm
import sys
from math import floor
def Max_Heapify(data,i):
l = 2*i
r = 2*i + 1
if l <= len(data) - 1 and data[l] > data[i]:
largest = l
/*写多了Java和Python,写C++全是坑。。。。。:| */
#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <cassert>
class Student
@jasonlvhit
jasonlvhit / douban_user_login.py
Last active August 29, 2015 13:56
豆瓣OAuth2用户认证登录, Flask应用。
from sqlalchemy.exc import IntegrityError
from flask import g, request, session, redirect, flash
import urllib
import urllib2
from app import models
@app.route('/douban/login')
def get_auth_code():
return redirect("https://www.douban.com/service/auth2/auth?client_id=0fb837361a070e2d2f28a5a24e152a87&redirect_uri=https://douping.sinaapp.com/auth&response_type=code")
@jasonlvhit
jasonlvhit / KMP.cpp
Last active August 29, 2015 13:56
很久之前的KMP
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
void match_list(char * pattern, int match[] , size_t len)
{
memset(match, 0 ,sizeof(int)*len);
#C数组下标的实现
"""
其实一个例子就能说明,C中数组下标的实现方法。
................
int array[4];
array[2] 和 2[array]是等价的。
................
2[array],这是个什么玩意儿。
2[array]等价于:
#代码来自Python 2.7 Lib/wsgiref/utils.py
#__contains__判断一个字典中是否有key, 等同于has_key
_hoppish = {
'connection':1, 'keep-alive':1, 'proxy-authenticate':1,
'proxy-authorization':1, 'te':1, 'trailers':1, 'transfer-encoding':1,
'upgrade':1
}.__contains__
def is_hop_by_hop(header_name):
//JavaScript : The Good Parts
//闭包,存储数据
var memoizer = function (memo, formula) {
var recur = function(n){
var result = memo[n];
if (typeof result !== 'number') {
result = formula(recur, n);
memo[n] = result;
};
return result;