Skip to content

Instantly share code, notes, and snippets.

View athos's full-sized avatar
🤷‍♂️
I know the value and cost of nothing

Shogo Ohta athos

🤷‍♂️
I know the value and cost of nothing
View GitHub Profile
@athos
athos / gist:895752
Created March 31, 2011 03:08
an idea of alternative macros for letfn (I hate its too deep indents)
(letfn [(kons [x y]
(cons x y))]
(kons 1 nil))
(defmacro letfn1 [name args & form]
(let [fbody (butlast form)
body (last form)]
`(letfn [(~name ~args ~@fbody)]
~body)))
(ns analyze)
(defmulti expr->clj-obj class)
(defmethod expr->clj-obj :default [x] x)
(defn exprs->clj-obj [xs]
(vec (map expr->clj-obj xs)))
(def EVAL clojure.lang.Compiler$C/EVAL)
@athos
athos / gist:1817230
Created February 13, 2012 14:20
Sudoku(4x4) solver in Alloy
sig Digit {}
one sig One, Two, Three, Four extends Digit {}
sig Cell {content: one One + Two + Three + Four}
sig Group {
cells: set Cell
} {
no disj c, c': cells | c.content = c'.content
}
@athos
athos / gist:1924255
Created February 27, 2012 14:37
https://twitter.com/#!/wtakuo/status/173781581570387968 の解法を探るために使ったAlloyコード
open util/ordering [Array]
sig Num {}
sig Array {
at: Num -> one Num
}
pred update(a, a': Array, i, v: Num) {
a'.at = a.at ++ i -> v
}
@athos
athos / gist:2398796
Created April 16, 2012 13:27
an example of Clojure 1.4 reader literals
(defn defn-reader [body]
(let [name (symbol (name (first (keys (dissoc (meta body) :tag)))))
args (read-string (:tag (meta body)))]
`(defn ~name ~args ~(seq body))))
(.bindRoot #'default-data-readers {'defn #'defn-reader})
#defn ^:fact ^"[x]"
[
if (= x 0)
@athos
athos / letrec.clj
Created July 8, 2012 03:15
optimizing mutual tail recursion without trampoline
(ns letrec
;; add [org.clojure/tools.macro "0.1.1"] to :dependencies if you use Leiningen,
;; or download it from https://github.com/clojure/tools.macro
(:use [clojure.tools.macro :only [macrolet]]))
(defmacro letrec [bindings & body]
(let [fnames (map first bindings)
fname->label (zipmap fnames (range))
fsym (gensym)
max-nargs (reduce #(max %1 (count %2)) 0 (map second bindings))
(use 'syntactic-closure.core)
(define-syntax aif
(sc-macro-transformer
(fn [[_ test then else] env]
(quasiquote
(let [it ~(make-syntactic-closure env nil test)]
(if it
~(make-syntactic-closure env '[it] then)
~(make-syntactic-closure env nil else)))))))
(defmacro hash-map-by-names [names]
(zipmap (map keyword names) names))
(def x 100)
(def y 200)
(let [y 2
z 3]
(hash-map-by-names [x y])) ; {:y 2, :x 100}
@athos
athos / gist:4224221
Created December 6, 2012 12:50
clojure.coreで定義されている変数・関数名の長さのリスト。名前の平均の長さは約8.3文字。
user=> (def freq (frequencies (map #(count (str %)) (keys (ns-publics 'clojure.core)))))
#'user/freq
user=> (doseq [[l n] freq] (printf "%2d %s\n" l (apply str (repeat n \*))))
1 *******
2 ****************
3 ****************************
4 **********************************************************************
5 *******************************************************
6 ********************************************************
7 ****************************************************************
@athos
athos / reader_test.clj
Created December 12, 2012 12:47
an example of *default-data-reader-fn*, a feature of Clojure 1.5
(ns reader-test)
(alter-var-root #'*default-data-reader-fn* (fn [_] cons))
#defn(fibs [n]
#letfn([#f([a b] #lazy-seq(#cons(a, #f(b, #+(a, b)))))]
#take(n, #f(0, 1))))
; #fibs(10) => (0 1 1 2 3 5 8 13 21 34)