Skip to content

Instantly share code, notes, and snippets.

@jkugiya
jkugiya / gist_rtc_initialize
Last active August 29, 2015 14:03
WebRTC系のオブジェクトの初期化(クロスブラウザ対応)
window.RTCPeerConnection = (window.webkitRTCPeerConnection || // Chrome
window.mozRTCPeerConnection); // Firefox
window.RTCSessionDescription = (window.RTCSessionDescription ||
window.mozRTCSessionDescription);
window.RTCIceCandidate = (window.RTCIceCandidate ||
window.mozRTCIceCandidate);
; 2章データ型
; コレクション
;; リスト
;;; 以下は同じ
'(1 2 3)
(list 1 2 3)
; 'はリーダーマクロで(list ...) に展開される
(first '(1 2 3 4))
(rest '(1 2 3 4))
(last '(1 2 3 4))
@jkugiya
jkugiya / yhpg-Tick-Tack-Toe.clj
Last active August 29, 2015 14:04
yhpg - Tick-Tack-Toe
(def won-pattern [2r111000000 2r000111000 2r000000111 2r100100100 2r010010010 2r001001001 2r100010001 2r001010100])
(defn win? [prots]
(if (> 2 (count prots)) false
(let [current-bit (apply bit-or (map #(.intValue (Math/pow 2 (- % 1))) prots))]
(some (fn [x] (= x (bit-and current-bit x))) won-pattern))))
(defn judge [a-prots b-prots f?]
(let [target (if f? a-prots b-prots)
enemy (if f? b-prots a-prots)
@jkugiya
jkugiya / yhpg-poker.clj
Last active August 29, 2015 14:05
yhpg-poker
(ns poker.core)
(defn to-pairs [coll]
(loop [pairs []
ranks (set (map second coll))]
(let [t (first ranks)]
(if (nil? t) pairs
(recur (conj pairs (filter #(= t (second %)) coll)) (rest ranks))))))
(defn is-4K? [pairs]
(not (empty? (filter #(= 4 (count %)) pairs))))
@jkugiya
jkugiya / yhpg-poker2.clj
Created August 8, 2014 06:13
ポーカー/カンニングバージョン
(ns poker2.core)
; http://qiita.com/Nabetani/items/cbc3af152ee3f50a822f
; http://qiita.com/cielavenir/items/d96889cbb7fd246c3ccb
(defn to-pairs [coll]
(loop [pairs []
ranks (set (map second coll))]
(let [t (first ranks)]
(if (nil? t) pairs
(recur (conj pairs (filter #(= t (second %)) coll)) (rest ranks))))))
@jkugiya
jkugiya / yhpg-image-rotation.clj
Created August 8, 2014 12:31
二値画像の回転
(ns image-rotation.core
(:require [clojure.string :as string]))
; http://qiita.com/Nabetani/items/9d80de41903775296ca6
(defn take-by-nth [coll n step]
(reverse (take-nth n (concat (nthrest coll step) (take step coll)))))
(defn flip [f]
(fn [& args] (apply f (reverse args))))
@jkugiya
jkugiya / recur
Created September 18, 2014 13:50
末尾再帰
// こういうメソッドがあったとして
int someMethod(int n) {
if (n < 1) {
return 1;
} else {
return n * someMethod(n - 1);
}
}
// コンパイラレベルでこういうコードや
@jkugiya
jkugiya / study_0921.clj
Last active August 29, 2015 14:06
はじめてのClojure勉強会#4 7つの言語 7つの世界 2日目
; 再帰について
; Clojureの再帰
;; loopとrecur
(defn size [v]
(if (empty? v)
0
(inc (size (rest v)))))
; (size [1 2 3])
@jkugiya
jkugiya / lazy-seq.clj
Created October 3, 2014 07:16
はじめてのClojure勉強会#5 遅延シーケンス
(defn take
"Returns a lazy sequence of the first n items in coll, or all items if
there are fewer than n."
{:added "1.0"
:static true}
[n coll]
(lazy-seq
(when (pos? n)
(when-let [s (seq coll)]
(cons (first s) (take (dec n) (rest s)))))))
@jkugiya
jkugiya / thread_macro.clj
Created October 3, 2014 07:18
はじめてのClojure勉強会#5 スレッドマクロの実装を調べてみた
; (sourcec ->)
(defmacro ->
"Threads the expr through the forms. Inserts x as the
second item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
second item in second form, etc."
{:added "1.0"}
([x] x)
([x form] (if (seq? form)