This file contains 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
(defmacro hash-map-by-names [names] | |
(zipmap (map keyword names) names)) | |
(def x 100) | |
(def y 200) | |
(let [y 2 | |
z 3] | |
(hash-map-by-names [x y])) ; {:y 2, :x 100} |
This file contains 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=> (def freq (frequencies (map #(count (str %)) (keys (ns-publics 'clojure.core))))) | |
#'user/freq | |
user=> (doseq [[l n] freq] (printf "%2d %s\n" l (apply str (repeat n \*)))) | |
1 ******* | |
2 **************** | |
3 **************************** | |
4 ********************************************************************** | |
5 ******************************************************* | |
6 ******************************************************** | |
7 **************************************************************** |
This file contains 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
(ns reader-test) | |
(alter-var-root #'*default-data-reader-fn* (fn [_] cons)) | |
#defn(fibs [n] | |
#letfn([#f([a b] #lazy-seq(#cons(a, #f(b, #+(a, b)))))] | |
#take(n, #f(0, 1)))) | |
; #fibs(10) => (0 1 1 2 3 5 8 13 21 34) |
This file contains 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
(declare odd?) | |
(defn even? [x] | |
(if (== 0 x) | |
true | |
#(odd? (- x 1)))) | |
(defn odd? [x] | |
(if (== 1 x) | |
true |
This file contains 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
(ns send-more-money | |
(:require [clojure.math.combinatorics :refer [combinations permutations]])) | |
(defn solve [n cond] | |
(for [ds (combinations (range 10) n) | |
ds' (permutations ds) | |
:when (cond ds')] | |
ds')) | |
(defn ->n [ds] |
This file contains 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=> (def ^:dynamic x 0) | |
#'user/x | |
user=> (dotimes [_ 5] (time (dotimes [_ 10000000] x))) | |
"Elapsed time: 665.329 msecs" | |
"Elapsed time: 755.922 msecs" | |
"Elapsed time: 735.566 msecs" | |
"Elapsed time: 780.893 msecs" | |
"Elapsed time: 766.333 msecs" | |
nil | |
user=> (binding [x 100] x) ; 一度値を動的に束縛すると、それ以降の参照は遅くなる |
This file contains 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
let fib n = | |
(let rec iter n a b = | |
(if ((=) n 0) | |
then a | |
else (iter ((-) n 1) b ((+) a b))) | |
in (iter n 0 1)) |
This file contains 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
5 | 0 | 0 | 0 | |
---|---|---|---|---|
10 | 0 | 0 | 0 | |
15 | 0 | 0 | 0 | |
20 | 0 | 0 | 0 | |
25 | 0 | 0 | 0 | |
30 | 0 | 0 | 0 | |
35 | 0 | 0 | 0 | |
40 | 133 | 36 | 97 | |
45 | 52 | 0 | 52 | |
50 | 66 | 23 | 43 |
This file contains 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
nREPL server started on port 55753 on host 127.0.0.1 | |
REPL-y 0.3.0 | |
Clojure 1.5.1 | |
Docs: (doc function-name-here) | |
(find-doc "part-of-name-here") | |
Source: (source function-name-here) | |
Javadoc: (javadoc java-object-or-class-here) | |
Exit: Control+D or (exit) or (quit) | |
Results: Stored in vars *1, *2, *3, an exception in *e |
This file contains 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
(defn- compound-rule [r1 r2] | |
(fn [type] | |
(fn [part] | |
(fn [x v] | |
(((r2 type) part) x (((r1 type) part) x v)))))) | |
(defn- apply-rule [r x] | |
(if-let [f (r (:type x))] | |
(assoc x :contents | |
(mapcat (fn [[p v]] [p (or ((f p) x v) v)]) |