Skip to content

Instantly share code, notes, and snippets.

View redraiment's full-sized avatar
🏠
Working from home

Zhang, Zepeng (redraiment) redraiment

🏠
Working from home
View GitHub Profile
@redraiment
redraiment / ChineseSpringFestivalGift.c
Created September 6, 2012 11:56
A Gift for Chinese Spring Festival
#include <stdio.h>
long x[]={1, 3,6,8,0,0,0,0, 32,32,
4,32, 1022,32,1020,36 ,32,1022, 34,508,32 ,
32,509,34 , 508,32,36, 32,0,32,36,1022,508,80,
1023, 32,260 ,136,
32, 32 , 508 , 260,32 , 32,
0404, 514, 32,32 ,0,0,0 ,0x0,
994 , 0466, 0772, 47,548,950, 168, 296, 559,
694,69 ,174, 0x228, 0x3B6 , 01646,0141,01744,
01266 ,0124 ,2033
@redraiment
redraiment / linux2win.sh
Created September 6, 2012 12:02
For Chinese user, not only convert \r\n to \n, but also convert char encoding from GBK to UTF-8
#!/bin/sh
until [ -z $1 ]; do
unix2dos $1 2>/dev/null
iconv -f UTF-8 -t GBK $1 -o /tmp/unix2doc.tmp 2>/dev/null
if [ $? -eq 0 ]; then
mv /tmp/unix2doc.tmp $1
echo "linux2win: converting file $1 to WIN format ..."
else
@redraiment
redraiment / StringMul1.js
Created September 6, 2012 12:06
JavaScript: Multiplication for String
/* a clever way to implement string multiplication in JavaScript */
String.prototype.times = function(n) {
return Array.prototype.join.call({length:n+1}, this);
};
"js".times(5) // => "jsjsjsjsjs"
@redraiment
redraiment / comment_strip.rb
Last active October 10, 2015 07:27
Removes comments with C style (`//' and `/* */') in Ruby
require 'strscan'
lex = [/"[^\\"]*(?:\\.[^\\"]*)*"/, # string
/'[^\\']*(?:\\.[^\\']*)*'/, # char
/\/\*.*?\*\//m, # multi-line
/\/\/(?:.*?\\(?:\r?\n|\r))*.*/, # single-line
/.|\s+/] # rest
ARGV.each do |source|
stream = StringScanner.new File.read source
@redraiment
redraiment / if-let.lisp
Created December 25, 2012 02:50
If test is true, evaluates then with binding-form bound to the value of test, if not, yields else.
(defmacro if-let ((var test) then &optional else)
"(if-let binding then else?)
binding => binding-form test
If test is true, evaluates then with binding-form bound to the value of
test, if not, yields else"
`(let ((,var ,test))
(if ,var
,then
,else)))
@redraiment
redraiment / when-let.lisp
Last active December 10, 2015 02:58
When test is true, evaluates body with binding-form bound to the value of test.
(defmacro when-let ((var test) &body body)
"(when-let binding body)
binding => binding-form test
When test is true, evaluates body with binding-form bound to the value of test"
`(let ((,var ,test))
(when ,var
,@body)))
@redraiment
redraiment / sublist.lisp
Created December 25, 2012 02:56
Returns a sub list.
(defmacro sublist (1st start &optional (size nil))
"(sublist start size?)
Returns a list of the items in vector from start (inclusive)
to start + size (exclusive). If size is not supplied,
defaults to end of list."
`(loop for item in (nthcdr ,start ,1st)
,@(if size
`(for i from 1 to ,size))
collect item))
@redraiment
redraiment / dump-tree.lisp
Last active December 10, 2015 02:58
Define a recursive lambda.
; dump tree with a recursive lambda
(funcall
(fn dump-tree (e)
(if (atom e)
(format t "~A~%" e)
(mapc #'dump-tree e)))
'(1 (2 (3 (4) 5) 6) 7 (8 9)))
@redraiment
redraiment / Y Combinator 简介
Last active December 23, 2020 07:44
Y Combinator (Fixed-point Combinator) 不动点组合子
Y组合子是Lambda演算的一部分,也是函数式编程的理论基础。
它是一种方法/技巧,在没有赋值语句的前提下定义递归的匿名函数。
即仅仅通过Lambda表达式这个最基本的“原子”实现循环/迭代。
颇有道生一、一生二、二生三、三生万物的感觉。
虽然Y组合子在理论上很优美,但在实际开发中并不会真的用到。
想要了解Y组合子是什么,请参见维基百科:http://en.wikipedia.org/wiki/Fixed-point_combinator#Y_combinator
或者知乎上的回答:http://www.zhihu.com/question/20115649
@redraiment
redraiment / .zsh配置文件
Last active October 23, 2019 01:36
Z Shell 配置
ZSH在交互方面的确比Bash略胜一筹,但习惯了Bash下大小写无关补全等功能,需要对zsh的默认配置做一些调整。