Skip to content

Instantly share code, notes, and snippets.

View zerg000000's full-sized avatar

Albert Lai zerg000000

View GitHub Profile
@zerg000000
zerg000000 / simple-fizzbuzz.clj
Created January 6, 2012 03:02
A clojure version of fizzbuzz in 5 minutes
(defn mod3? [num] (= (mod num 3) 0))
(defn mod5? [num] (= (mod num 5) 0))
(defn fizzbuzz [num] (cond (and (mod3? num) (mod5? num)) "FizzBuzz"
(mod5? num) "Buzz"
(mod3? num) "Fizz"
:else num))
(map fizzbuzz (range 1 101))
@zerg000000
zerg000000 / bst.clj
Created February 15, 2013 03:35
A clojure implement of binary search tree
(ns com.example.bst)
; using {:val :left :right} as tree data structure
(defn node [val & [left right other]]
{:val val :left left :right right :bag other})
(defn insert [parent i-node]
(let [i-val (:val i-node)
p-val (:val parent)
side (cond (> i-val p-val) :left
@zerg000000
zerg000000 / gist:5019830
Last active December 14, 2015 03:19
Convert Java Bean to Clojure map
(bean (java.util.Date.))
{:seconds 40, :date 23, :class java.util.Date,
:minutes 47, :hours 21, :year 113,
:timezoneOffset -480, :month 1, :day 6,
:time 1361627260711}
(-> (frame :title "Why Swing, why?"
:on-close :exit
:content (label :text "Hiya"
:border 5
:background "#888"
:foreground :blue))
pack!
show!)
; copy from https://github.com/daveray/seesaw/blob/develop/src/seesaw/options.clj
(defrecord Option [name setter getter examples])
(defmacro bean-option
[name-arg target-type & [set-conv get-conv examples]]
(let [[option-name bean-property-name] (split-bean-option-name name-arg)
target (gensym "target")]
`(Option. ~option-name
(fn [~(with-meta target {:tag target-type}) value#]
function trickyToString(data, withComma) {
var outStr = "";
for( key in data ) {
outStr = outStr + key;
if(withComma) {
outStr = outStr + "," + data[key];
} else {
outStr = outStr + ";" + data[key];
}
outStr = outStr + "\n";
function trickyToString(data, withComma, withQuot) {
var outStr = "";
for( key in data ) {
outStr = outStr + quot(key, withQuot);
if(withComma) {
if(withQuot) {
outStr = outStr + "," + quot(data[key], withQuot);
} else {
outStr = outStr + ",";
}
// Parameter Object opts instead of withQuot and withComma
function trickyToString(data, opts) {
//trickyToString become a Template method
// quot and comma flag become Strategy of trickyToString method
var quotFunc = opts.withQuot || function(s){ return s; };
var commaFunc = opts.withComma || function(){ return ","; };
var outStr = "";
for( key in data ) {
outStr = outStr + quotFunc(key) + commaFunc() + quotFunc(data[key]) + "\n";
}
public class Mailer {
public void send(String content) {
String to = "abcd@gmail.com";
String from = "web@gmail.com";
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
@zerg000000
zerg000000 / repl.clj
Last active March 25, 2016 11:11
考起碩士生的小一數學題(ppp)解法 - 香港高登 http://m2.hkgolden.com/view.aspx?message=6307521&type=SW&page=1
(use 'clojure.core.logic)
(require '[clojure.core.logic.fd :as fd])
(def result
(run* [q]
(fresh [a b c d e f g h p ab cd ef gh ppp]
(fd/in a b c d e f g h p (fd/interval 1 9)) ; why not zero?
(fd/in ab cd ef gh (fd/interval 10 99))
(fd/in ppp (fd/interval 100 999))
(fd/eq