This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;;; Project Euler #15 | |
| ;;; http://projecteuler.net/problem=15 | |
| ;;; http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2015 | |
| (require '[clojure.test :refer [deftest is]]) | |
| (defn fact | |
| [n] | |
| (loop [n n r 1] | |
| (if (zero? n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var providerName = "Npgsql"; | |
| var connectionString = "Server=db.bouzuya.jp;Port=5432;User Id=postgres;Password=postgres;Database=mydb"; | |
| var sql = "INSERT INTO users(username,password) VALUES(:username, :password)"; | |
| var parameters = new Dictionary<string, object>() { {"username", "bouzuya"}, {"password", "password"} }; | |
| var factory = DbProviderFactories.GetFactory(providerName); | |
| using (var connection = factory.CreateConnection ()) { | |
| connection.ConnectionString = connectionString; | |
| connection.Open (); | |
| using (var transaction = connection.BeginTransaction ()) { | |
| try { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; Clojure でのリソースの取得 | |
| ; Thread.currentThread().getContextClassLoader().getResource("my.resource") 相当 | |
| (clojure.java.io/resource "my.resource") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; Clojure での java.util.Properties の取得 | |
| (let [u (clojure.java.io/resource "my.resource")] | |
| (with-open [r (clojure.java.io/reader u)] | |
| (doto | |
| (java.util.Properties.) | |
| (.load r)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; java.util.Properties から Clojure の map への変換 | |
| ; java.util.Properties -> Clojure map | |
| ; e.g. key1=val1 -> {:key1 val1} | |
| (defn props->map | |
| [^java.util.Properties props] | |
| (let [names (enumeration-seq (.propertyNames props)) | |
| pairs (map (juxt keyword #(.getProperty props %)) names)] | |
| (into {} pairs))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;;; Project Euler #22 | |
| ;;; http://projecteuler.net/problem=22 | |
| ;;; http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2022 | |
| ; [org.clojure/data.csv "0.1.2"] | |
| (require '[clojure.test :refer [deftest is]] | |
| '[clojure.data.csv :as csv]) | |
| (defn char-score | |
| [c] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (require '[clojure.java.jdbc :as sql]) | |
| (def db | |
| "postgresql://postgres:postgres@localhost:5432/shouter") | |
| (sql/with-connection db | |
| (try (sql/drop-table :testing) (catch Exception _))) | |
| (sql/with-connection db | |
| (sql/create-table :testing [:data :text])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (require '[clojure.java.jdbc :as sql] | |
| '[clojure.java.io :as jio]) | |
| (def dbpath | |
| (jio/file (System/getProperty "user.home") "derby.example.db")) | |
| (def db | |
| {:classname "org.apache.derby.jdbc.EmbeddedDriver" ; "derby" も可能 | |
| :subprotocol "derby" | |
| :subname dbpath |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (use 'clojure.test) | |
| (with-test | |
| (defn f | |
| [& {:as options}] | |
| options) | |
| (is (nil? (f))) | |
| (is (thrown? IllegalArgumentException (f nil))) | |
| (is (thrown? IllegalArgumentException (f :a))) | |
| (is (= (f nil nil) {nil nil})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;;; [PostgreSQL Tutorial][postgresql-tutorial] in H2 | |
| ;;; | |
| ;;; [postgresql-tutorial]: http://www.postgresql.jp/document/8.2/html/tutorial-table.html | |
| (ns h2-example.core | |
| (:require [clojure.java.io :as jio] | |
| [clojure.java.jdbc :as jdbc] | |
| [clojure.string :as str])) | |