Skip to content

Instantly share code, notes, and snippets.

@t1anchen
t1anchen / gist:7305345
Created November 4, 2013 16:37
factorial
;; Take k-th product while n!(n-1)!...(n-k+1)!...2!1!
;;
(map (fn [x] (reduce * x)) (take 10 (iterate rest (range 10 0 -1))))
;; (3628800 362880 40320 5040 720 120 24 6 2 1)
(reduce *' (map (fn [x] (reduce *' x)) (take 10 (iterate rest (range 10 0 -1)))))
;; 6658606584104736522240000000N
@t1anchen
t1anchen / gist:7249422
Last active December 27, 2015 01:59
maplist
;; Leiningen 2.3.2
;; Clojure 1.5.1
;; REPL
;;
(defn maplist [f coll] (map #(apply f %) (take (count coll) (iterate rest coll))))
(maplist min (range 10))
;; (0 1 2 3 4 5 6 7 8 9)
(maplist max (range 10))
;; (9 9 9 9 9 9 9 9 9 9)
(maplist * (range 1 11))
@t1anchen
t1anchen / BufferedScannerPerfTest.java
Last active December 20, 2015 00:19
Benchmark for reading and parsing 10000000 integers
package org.coursera.algs4partII002.perf;
import org.junit.Test;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
@t1anchen
t1anchen / timestamp.el
Last active December 19, 2015 04:08
emacs handy functions
(defun insert-textual-timestamp-in-diary ()
;; Set timestamp insert function. This function comes from
;; http://www.patd.net/~ciggieposeur/other/startup.el
;; RFC 3339 and ISO 8601
(interactive "*")
(insert
(concat "["
(format-time-string "%Y-%m-%dT%T%z" (current-time))
"] ")))
(global-set-key (kbd "C-c 1 `") 'insert-textual-timestamp-in-diary)
@t1anchen
t1anchen / typoglycemia.clj
Last active December 18, 2015 13:48
Simply generate typoglycemia
(use 'clojure.string)
(defn gen-rnd-idx-seq [s]
(let [ordered-idx (range (count s))
randomed-idx (map #(rand-int %) ordered-idx)
arrayed-s (into-array s)]
(join (reduce #(let [foo-idx (first %2)
bar-idx (last %2)
foo (nth arrayed-s foo-idx)
bar (nth arrayed-s bar-idx)]
@t1anchen
t1anchen / gist:5693672
Created June 2, 2013 13:35
Remove all white spaces of all files under current directory
find . -type f -regex '.*\ .*' | awk '{gsub(/\(/,"\\(",$0);gsub(/\)/,"\\)",$0);gsub(/\ /,"\\ ",$0);orig_fname=$0;gsub(/ /,".",$0);system("mv -v " orig_fname " " $0)}'
@t1anchen
t1anchen / rename.py
Last active September 29, 2019 08:45
#!/usr/bin/python
import os
import sys
import getopt
import re
import os.path
def main():
(defun distl (m N)
(cond
((null N) nil)
(t (cons (list m (car N))
(distl m (cdr N))))))
(print (distl 'b '(x y z)))
(defun cartesian (M N)
(cond
((null M) nil)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; golang mode
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(add-to-list 'load-path "C:/go/misc/emacs/")
(require 'go-mode-load)
@t1anchen
t1anchen / Makefile
Created October 2, 2012 15:07
C++0x Lambda Demo
CXX = i686-pc-mingw32-g++
CXXFLAGS = -Wall
CXXDEBUGFLAG = -g
CXXOPTFLAG = -O3
CXX0XFLAG = -std=c++0x
CXXLINKFLAG = -static
BINS = cxx0x-address
all: $(BINS)