Skip to content

Instantly share code, notes, and snippets.

View syou6162's full-sized avatar

Yasuhisa Yoshida syou6162

View GitHub Profile
@syou6162
syou6162 / simple_continuation_example_in_clojure.clj
Created February 18, 2012 14:57
Clojureでの継続の簡単な例
;; プログラミングGaucheの継続の例をClojureで書いてみる
;; 継続がそもそもどういう概念なのか理解できると...いいな
;; まずCall-Return方式
(defn find-fold [pred? proc seed lis]
(cond (empty? lis) seed
(pred? (first lis)) (let [seed2 (proc (first lis) seed)]
(find-fold pred? proc seed2 (rest lis)))
:else (find-fold pred? proc seed (rest lis))))
@syou6162
syou6162 / example_of_character_unicode_block.clj
Created February 21, 2012 11:56
ClojureでJavaのUnicodeBlockを使って文字種を判定する方法について
;; see also http://java.sun.com/j2se/1.5.0/ja/docs/ja/api/java/lang/Character.UnicodeBlock.html
(import 'java.lang.Character$UnicodeBlock)
;; java.lang.Character$UnicodeBlock
(Character$UnicodeBlock/of \u3042)
;; #<UnicodeBlock HIRAGANA>
(map #(Character$UnicodeBlock/of %) "日本だ")
;; (#<UnicodeBlock CJK_UNIFIED_IDEOGRAPHS> #<UnicodeBlock CJK_UNIFIED_IDEOGRAPHS> #<UnicodeBlock HIRAGANA>)
@syou6162
syou6162 / gist:2241779
Created March 29, 2012 18:39 — forked from AlexBaranosky/gist:2233328
state monad as a way to hold 'world' state
(defmacro defworld [name bindings & body]
`(defn ~name ~bindings
(fn [~'world]
(println ~'world)
~@body)))
(defmacro with-world [& body]
`(domonad state-m
~@body))
@syou6162
syou6162 / conditions_filter.clj
Created May 22, 2012 12:36
条件をどんどん付けてフィルタリングするのを簡単にするための何か
;; macroで書く必要がなかった...
;; (defmacro gen-bool-fns
;; [key-value-pairs]
;; `(loop [fns# []
;; pairs# (partition 2 ~key-value-pairs)]
;; (if (empty? pairs#)
;; fns#
;; (recur
;; (conj fns# (let [[k# v#] (first pairs#)]
;; (fn [x#] (= (k# x#) v#))))
@syou6162
syou6162 / simple_seesaw.clj
Created May 23, 2012 12:55
seesawで線を描画するための最小構成(と思われるもの)。lineをどう引くかだけ見てもらって、実際は100とかの数字をSICPの中の関数とかで計算していくのがメインとなっています
(use 'seesaw.core)
(use 'seesaw.graphics)
(def my-line-style (style :foreground :black))
(def f (frame :title "My first example of Picture Language!!!"
:content
(canvas :id :canvas
:background :white
:size [640 :by 480]
;; {:x x, :y y}みたいなhash-mapを毎回作るのがダルくなってきたので...
(defmacro hash-map-by-names [& names]
`(let [pairs# (map (fn [name#]
(list (keyword name#) (deref (resolve name#))))
'~names)]
(apply hash-map (flatten pairs#))))
(def x 1)
(def y 2)
(defprotocol Contains?
(contains? [this item]))
(extend-type clojure.lang.PersistentList
Contains?
(contains? [this item]
(some #(= % item) this)))
(contains? '(1 2 3) 1) ; true
(defmacro hash-map-by-names [names]
`(let [ks# (map keyword '~(keys &env))
vs# (list ~@(keys &env))
names# (map keyword '~names)]
(select-keys (zipmap ks# vs#) names#)))
(let [x 1
y 2
z 3]
(hash-map-by-names [x y])) ; {:y 2, :x 1}