Skip to content

Instantly share code, notes, and snippets.

@ponkore
ponkore / factorial.clj
Created December 7, 2012 13:24
階乗の計算 in Clojure
;;; 普通の再帰版
(defn fact-1 [n]
(if (= n 0) 1
(*' n (fact-1 (dec n)))))
;;; 末尾再帰版
(defn fact-2
([n] (fact-2 n 1))
([n acc]
(if (= n 0) acc
@ponkore
ponkore / problem-1-1.clj
Created December 7, 2012 13:29
Project Euler Problem 1
;;; Project Euler Problem 1
;;; http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%201
;;;
;;; 10未満の自然数のうち、3 もしくは 5 の倍数になっているものは 3, 5, 6, 9 の4つがあり、 これらの合計は 23 になる。
;;; 同じようにして、1,000 未満の 3 か 5 の倍数になっている数字の合計を求めよ。
;;; 3 もしくは 5の倍数の場合 true、そうでない場合 false を返す filter 判定用関数
(defn fizz-or-buzz?
"3か5で割り切れるならtrue、そうでないならfalseを返す。"
[n]
@ponkore
ponkore / fizzbuzz-multimethod.clj
Created December 7, 2012 14:12
fizzbuzz in Clojure (multimethod版)
;; ディスパッチ関数
(defn fizzbuzz-dispatch-fn [n] [(zero? (mod n 3)) (zero? (mod n 5))])
;; マルチメソッドの定義(マッチングめちゃ強力!)
(defmulti fizzbuzz fizzbuzz-dispatch-fn)
(defmethod fizzbuzz [true false] [n] "fizz")
(defmethod fizzbuzz [false true] [n] "buzz")
(defmethod fizzbuzz [true true] [n] "fizzbuzz")
(defmethod fizzbuzz :default [n] n)
@ponkore
ponkore / sqlkorma-tutrial.md
Created December 10, 2012 18:16
sqlkorma 使ってみた (Lisp アドベントカレンダー 2012の 11日目の記事です)

sqlkorma 使ってみた

この記事は、Lispアドベントカレンダー2012 の 11日目 の記事です。前日は、nitro_idiot さんがライブコーディングで凄まじい勢いでWebサイトを作る 記事 でした。

今日のネタは、いろいろ考えた挙句 sqlkorma にしました。

動機

@ponkore
ponkore / compiler.clj
Created December 13, 2012 13:15
hiccup の魔改造。hiccup の jar には手を入れずに、hiccup 内プライベート関数を書き換えて、改行とインデントをサポート。 だいたいできたけど、a とか span とか h1〜h6 とかのように、改行をいちいち入れないほうが見栄えが良いタグをちゃんと サポートしたい。
(ns hiccup-mod.compiler
(:require [hiccup.compiler :as c]))
;;;
;;; CAUTION!!! hiccup.compiler の end-tag、render-element を書き換える。
;;;
;;; public vars
(def ^{:dynamic true :doc " default is two white space."}
*html-indent-characters* " ")
@ponkore
ponkore / problem-4-1.clj
Created December 14, 2012 13:35
Project Euler Problem 4
;;;A palindromic number reads the same both ways.
;;;The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
;;;Find the largest palindrome made from the product of two 3-digit numbers.
;;;左右どちらから読んでも同じ値になる数を回文数という。 2桁の数の積で表される回文数のうち、
;;;最大のものは 9009 = 91 × 99 である。
;;;では、3桁の数の積で表される回文数のうち最大のものはいくらになるか。
(ns projecteuler.problem-4
(:require [clojure.string :as str])
@ponkore
ponkore / problem_11.clj
Created December 23, 2012 13:47
Project Euler Problem 11
;;; In the 20x20 grid below, four numbers along a diagonal line have been marked in red.
;;; :
;;; (snip)
;;; :
;;; The product of these numbers is 26 * 63 * 78 * 14 = 1788696.
;;; What is the greatest product of four adjacent numbers
;;; in any direction (up, down, left, right, or diagonally) in the 20x20 grid?
(ns projecteuler.problem-11
(:use clojure.test))
@ponkore
ponkore / problem_18.clj
Created January 5, 2013 14:55
Project Euler Problem 18
;;; By starting at the top of the triangle below and moving to adjacent numbers
;;; on the row below, the maximum total from top to bottom is 23.
;;;
;;; 3
;;; 7 4
;;; 2 4 6
;;; 8 5 9 3
;;;
;;; That is, 3 + 7 + 4 + 9 = 23.
;;;
@ponkore
ponkore / problem_25.clj
Created January 9, 2013 14:18
Project Euler Problem 25
(ns projecteuler.problem-25
(:require [clojure.string :as str]
[clojure.math.numeric-tower :as math])
(:use clojure.test))
(defn fib-gen
"フィボナッチ数を generate する関数。初期値[1 1]から iterate する前提。"
[[a b]] [b (+' a b)])
(def fib-seq
@ponkore
ponkore / camel-to-dash.clj
Last active December 13, 2015 17:09
CamelCase を dash-connected な string に変換する(日本語の取扱が今一つとか他にもバグあり)
(defn camel-to-dash
"convert `CamelCase` string to dash-connected string."
[s]
(->> (re-seq #"[A-Z]+[^A-Z]+|[a-z]+[^a-z]+" s)
(map str/lower-case)
(interpose "-")
str/join))