Skip to content

Instantly share code, notes, and snippets.

View bouzuya's full-sized avatar

bouzuya bouzuya

View GitHub Profile
;;; 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)
@bouzuya
bouzuya / gist:4374051
Created December 25, 2012 16:23
ADO.NET example
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 {
; Clojure でのリソースの取得
; Thread.currentThread().getContextClassLoader().getResource("my.resource") 相当
(clojure.java.io/resource "my.resource")
; 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))))
; 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)))
;;; 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]
(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]))
(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
(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}))
;;; [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]))