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 / 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 / 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 / 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 / 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 / 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 / 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