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
| user> (defn fib [n] | |
| (cond | |
| (= n 0) 0 | |
| (= n 1) 1 | |
| :else (+ (fib (- n 1)) | |
| (fib (- n 2))))) | |
| #'user/fib | |
| user> (map fib (range 10)) | |
| (0 1 1 2 3 5 8 13 21 34) | |
| user> (require '[clojure.spec.alpha :as s]) |
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
| {:deps {org.clojure/clojure {:mvn/version "1.9.0"} | |
| org.clojure/tools.cli {:mvn/version "0.3.5"}} | |
| :paths ["."]} |
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
| user=> (alembic.still/distill '[org.clojure/algo.generic "0.1.2"]) | |
| WARN: org.clojure/clojure version 1.4.0 requested, but 1.9.0-beta1 already on classpath. | |
| Loaded dependencies: | |
| [[org.clojure/algo.generic "0.1.2"]] | |
| Dependencies not loaded due to conflict with previous jars : | |
| [[org.clojure/clojure "1.4.0"]] | |
| nil | |
| user=> (require '[clojure.algo.generic.functor :as functor]) | |
| nil | |
| user=> (functor/fmap inc {:a 1 :b 2 :c 3}) |
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
| ;; cf. https://github.com/ctford/traversy | |
| user> (require '[traversy.lens :as lens]) | |
| nil | |
| user> (def data {:a [{:aa 1 :bb 2} | |
| {:cc 3}] | |
| :b [{:dd 4}]}) | |
| #'user/data | |
| user> (-> data | |
| (lens/update (lens/*> lens/all-values lens/each lens/all-values) | |
| #(if (even? %) (inc %) %))) |
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
| λ> import Data.Ratio ((%)) | |
| -- y = e^xの区間[0,1]の面積の近似 | |
| λ> sum $ zipWith (*) (repeat 0.001) (map (\x -> exp . realToFrac $ x) [0, 1%1000..999%1000]) | |
| 1.7174228307349653 | |
| -- y = x^2の区間[0,1]の面積の近似 | |
| λ> realToFrac . sum $ zipWith (*) (repeat $ 1%1000) (map (\x -> x * x) [0, 1%1000..999%1000]) | |
| 0.3328335 | |
| λ> |
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
| user> (defn enum | |
| ([fst] | |
| (iterate inc fst)) | |
| ([fst snd] | |
| (iterate #(+ % (- snd fst)) fst)) | |
| ([fst snd lst] | |
| (range fst (inc lst) (- snd fst)))) | |
| #'user/enum | |
| user> (take 10 (enum 1)) ; take 10 [1..] | |
| (1 2 3 4 5 6 7 8 9 10) |
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
| ;; 0 <= x <= 23 | |
| ;; 0 <= y <= 23 | |
| ;; x + y = 23 | |
| ;; 2x + 4y = 62 | |
| user> (for [x (range (inc 23)) | |
| y (range (inc 23)) | |
| :when (and (= (+ x y) 23) | |
| (= (+ (* 2 x) (* 4 y)) 62))] | |
| [x y]) | |
| ([15 8]) |
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
| ;; cf. https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 | |
| user> (require '[clojure.math.numeric-tower :as math]) | |
| nil | |
| user> (def leibniz (map / (iterate - 4) (iterate #(+ % 2) 1))) | |
| #'user/leibniz | |
| user> (->> leibniz | |
| (take-while #(> (math/abs %) 1/1000)) | |
| (apply +) | |
| double) | |
| 3.141092653621043 |
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
| ;; cf. https://clojure.org/guides/spec | |
| user=> (require '[clojure.spec.alpha :as s]) | |
| nil | |
| user=> (s/def ::side-a number?) | |
| :user/side-a | |
| user=> (s/def ::side-b number?) | |
| :user/side-b | |
| user=> (s/def ::side-c number?) | |
| :user/side-c | |
| user=> (s/fdef cuboid-volume |