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
(defn a&b [n]
(loop [n n, a (/ 1.0 (* 2 (Math/pow 3 0.5))), b (/ 1.0 3)]
(if (= n 0)
[a b]
(let [a' (/ (+ a b) 2)]
(recur (dec n) a' (Math/pow (* a' b) 0.5))))))
;; => (time (/ 1 (first (a&b 100))))
;; "Elapsed time: 0.04547 msecs"
;; 3.141592653589795
(require '[clojure.core.async :as a]
'[kitchen-async.promise :as p]
'[kitchen-async.promise.from-channel])
(comment
;; cf. https://gist.github.com/swannodette/5888989
(defn debounce
([c ms] (debounce (a/chan) c ms))
(ns repeating-decimal)
(defn repeating-decimal [n]
(loop [rem 1 rems #{} divs [] i 0]
(let [div (int (/ (* rem 10) n))
rem (mod (* rem 10) n)]
(if (and (not= rem 0) (contains? rems rem))
[n divs]
(when (not= rem 0)
(recur rem (conj rems rem) (conj divs div) (inc i)))))))
@athos
athos / core.clj
Last active January 26, 2017 00:43
「数字6桁パスワードのハッシュ値の総当たり、PHPなら約0.25秒で終わるよ(http://z.tokumaru.org/2014/02/6php025.html )」で、PHPに比べてClojureがやたら遅いという話だったので、Clojureのコードをいろいろいじってみた。
(ns six-length.core
(:require [clojure.core.reducers :as r]
[clojure.string :as s])
(:import [java.security MessageDigest]
[javax.xml.bind DatatypeConverter]))
(set! *warn-on-reflection* true)
(def hexdigest
(let [digester (MessageDigest/getInstance "MD5")]
@athos
athos / gist:1826678
Created February 14, 2012 13:11
Sudoku(9x9) solver in Alloy
abstract sig Digit {}
one sig One, Two, Three, Four, Five, Six, Seven, Eight, Nine extends Digit {}
sig Cell {content: Digit}
abstract sig Group {
cells: set Cell
} {
no disj c, c': cells | c.content = c'.content
}
(use gauche.partcont)
(use util.queue)
(define-class <channel> (buf takes puts))
(define (chan)
(make <channel> (make-queue) (make-queue) (make-queue)))
(define-syntax go
(syntax-rules ()

Clojure製ボットフレームワークを作る

リポジトリ on GitHub

前提

  • Hubot: GitHub製ボットフレームワーク(CoffeeScript)
    • アダプター: ボットを動かすサービスの差を吸収するレイヤー
    • スクリプト: コマンドに対するアクションを規定
(defn with-retry [n f]
(letfn [(try-once [i]
(try
(f)
(catch Exception e
(if (pos? i)
#(try-once (dec i))
(throw e)))))]
(trampoline try-once n)))
(ns apply-ctor
(import [java.lang.reflect Constructor]))
(defn- acceptable-types? [ptypes atypes]
(and (= (count ptypes) (count atypes))
(every? (fn [[ptype atype]]
(or (= ptype atype)
((ancestors atype) ptype)))
(map vector ptypes atypes))))
(def actions
(repeatedly (read) (fn [] [(read) (read)])))
(defn step [[[[t op] & as' :as as] temp temps] time]
(let [temp' (+ temp (if (= time t) (get '{out 3 in 5} op) 0))]
[(if (= time t) as' as), (max (dec temp') 0), (conj temps temp')]))
(defn calc [actions]
(let [zs (count (filter zero? (last (reduce step [actions 0 []] (range 24)))))]
(+ zs (* 2 (- 24 zs)))))