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 Problem 2 | |
| ;;; http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%202 | |
| ;;; | |
| ;;; フィボナッチ数列の項は前の2つの項の和である。 最初の2項を 1, 2 とすれば、最初の10項は以下の通りである。 | |
| ;;; 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... | |
| ;;; 数列の項の値が400万を超えない範囲で、偶数値の項の総和を求めよ。 | |
| ;;; フィボナッチ数列を、シーケンスとして求める。 | |
| (defn fib1 [[a b]] [b (+' a b)]) | |
| (defn fib [] (map second (iterate fib1 [1 1]))) |
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 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 is-3-or-5-mul? [n] (or (= (mod n 3) 0) (= (mod n 5) 0))) | |
| ;;; テスト |
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
| #!/usr/bin/env ruby | |
| # -*- coding: utf-8 -*- | |
| module ActsAsCsv | |
| class CsvRow | |
| def initialize(headers, arr) | |
| @headers = headers | |
| @arr = arr | |
| end |
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
| ;;; | |
| ;;; 簡単 grep。のつもりが、あんまりかっこ良くないorz | |
| ;;; | |
| (use '[clojure.java.io :as io]) | |
| (defn indexed [coll] | |
| (map vector (iterate inc 1) coll)) | |
| (defn grep [re file] | |
| (with-open [rdr (io/reader file)] |
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
| #!/usr/bin/env ruby | |
| arr = (1..16).to_a | |
| tmp = [] | |
| arr.each do |val| | |
| tmp.push(val) | |
| if tmp.length == 4 | |
| puts tmp.join(",") | |
| tmp = [] | |
| end | |
| end |
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
| irb(main):008:0> (1..10).each { |i| puts "This is sentence number #{i}" } | |
| This is sentence number 1 | |
| This is sentence number 2 | |
| This is sentence number 3 | |
| This is sentence number 4 | |
| This is sentence number 5 | |
| This is sentence number 6 | |
| This is sentence number 7 | |
| This is sentence number 8 | |
| This is sentence number 9 |
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
| ;; mod を普通に使う | |
| (defn gcd [m n] | |
| (if (zero? n) m | |
| (gcd n (mod m n)))) | |
| (gcd 1763 1927) | |
| ; => 41 |
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
| # groovy client with no arguments (startup server) | |
| bash$ groovyclient | |
| Invoking server: 'groovyserver' -p 1961 | |
| Groovy home directory: (none) | |
| Groovy command path: /opt/local/bin/groovy (found at PATH) | |
| GroovyServ home directory: /opt/local/share/devel/groovyserv | |
| GroovyServ work directory: /Users/masao/.groovy/groovyserv | |
| Original classpath: (none) | |
| GroovyServ default classpath: /opt/local/share/devel/groovyserv/lib/* | |
| Starting..... |
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
| ;; 何のひねりもない fibo | |
| (defn fibo [x] | |
| (if (or (= 0 x) (= 1 x)) | |
| 1 | |
| (+ (fibo (- x 1)) (fibo (- x 2))))) | |
| ;; | |
| (map #(fibo %) (take 10 (iterate inc 1))) | |
| ;; => (1 2 3 5 8 13 21 34 55 89) |
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
| ;; FizzBuzz in Clojure | |
| (defn fizzbuzz [max] | |
| (map (fn [n] | |
| (cond | |
| (= (mod n 15) 0) "FizzBuzz" | |
| (= (mod n 3) 0) "Fizz" | |
| (= (mod n 5) 0) "Buzz" | |
| :else n)) | |
| (take max (iterate inc 1)))) |