Skip to content

Instantly share code, notes, and snippets.

View Plummtw's full-sized avatar

Plummtwparrot Plummtw

  • DORTS
  • Taipei, Taiwan
View GitHub Profile
@Plummtw
Plummtw / ipv6-expander.clj
Last active October 24, 2015 08:39
Expand all possibilities of ipv6-notation.
(def *stack (ref nil))
(defn push [x]
#_(println x)
(dosync (ref-set *stack (cons x @*stack))))
(defn pre-push [v]
(doseq [i (range 8) j (range 8) :when (<= i j)]
#_(println (subvec v i (inc j)))
(when (every? #(= % "0") (subvec v i (inc j)))
(let [new-v (cond
(= i 0)
@Plummtw
Plummtw / gist:5131355
Last active December 14, 2015 18:39
guess how many rolls
(def +score+ (atom 0))
(def +answer+ (atom nil))
(defn new-game []
(reset! +answer+ (->> (range 4)
(map #(list % (rem (inc %) 4)))
(map (comp inc first shuffle))
(shuffle)))
(reset! +score+ 0))
@Plummtw
Plummtw / gist:3856846
Created October 9, 2012 05:45
Max visits
(defn max-visits [visits time]
(reduce (fn [num t] (if (> time t) (dec num) num))
(reduce (fn [num t] (if (> time t) (inc num) num)) 0 (first visits))
(second visits)))
(def GUESTS 30)
(def starts (map (fn [x] (int (* (rand) 24))) (range GUESTS)))
(def ends (map #(int (+ (* (- 24 %) (rand)) %)) starts))
(def visits [(sort > starts) (sort > ends)])
@Plummtw
Plummtw / gist:3798135
Created September 28, 2012 05:51
DB to Excel
(use 'korma.core)
(use 'korma.db)
(import '(java.io FileOutputStream))
(import '(org.apache.poi.ss.usermodel Cell Row Sheet Workbook))
(import '(org.apache.poi.xssf.streaming SXSSFWorkbook))
(defdb +db24+
(mssql
{:user "xx"
:password "xxxxxxxxxx"
(defmacro defenum [class & symbols]
(try (Class/forName (str class))
(catch java.lang.ClassNotFoundException e
(gen-and-load-class (str class) :extends java.lang.Enum)))
(cons 'do
(map (fn [sym val]
`(def ~symbol (new ~class ~(str sym) ~val)))
symbols (iterate inc 1))))
@Plummtw
Plummtw / gist:2936698
Created June 15, 2012 14:15
time-hash
(def +time-hashes+ (agent []))
(def +time-hashes-gc-interval+ 1800000) ; 30 minutes
(def ^:dynamic *time-hashes-gc-stop?* false)
(defn time-hash []
(let [result (ref {:shutdown? false :hash {}})]
(send +time-hashes+ (partial cons result))
result))
// http://code.google.com/p/clojure/downloads/list
// javac TestClojureScript.java
// java -cp .;clojure.jar TestClojureScript script1.clj # => 20
// java -cp .;clojure.jar TestClojureScript script2.clj # => 40
import clojure.lang.RT;
import clojure.lang.Var;
public class TestClojureScript {
public static void main(String[] args){