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
(future (def future-var "future var")) | |
future-var | |
;; -> "future var" |
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
(def ^:dynamic *dynamic-var* "dynamic var") | |
*dynamic-var* | |
;; -> "dynamic var" | |
(def re-binded-var | |
(binding [*dynamic-var* "re-binded var"] *dynamic-var*)) | |
re-binded-var | |
;; -> "re-binded var" |
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
(defn cube [x] (Math/pow x 3)) | |
(defn cube-root [x] (Math/pow x 1/3)) | |
(defn ^:dynamic prettify [x] (Math/round x)) | |
(defn compute [] (prettify (cube-root (cube 42)))) | |
(compute) | |
;; -> 42 |
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
(def static-var "static var") | |
static-var | |
;; -> "static var" | |
(binding [static-var "re-binded var"] static-var) | |
;; -> IllegalStateException Can't dynamically bind non-dynamic var: | |
;; user/static-var clojure.lang.Var.pushThreadBindings (Var.java:320) |
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
(defn factorial [n] | |
"Counts a factorial." | |
(reduce *' (range 1 (inc n)))) | |
(defn binomial [n k] | |
"Counts a binomial coefficient." | |
(/ (factorial n) | |
(*' (factorial k) | |
(factorial (- n k))))) |
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
(defn catalan [n] | |
"Counts a Catalan number by recursion." | |
(loop [cnt n acc 1] | |
(if (zero? cnt) | |
acc | |
(recur (dec cnt) | |
(* acc (- 4 (/ 6 (inc cnt)))))))) |
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
(ns blog-map-reduce.core | |
(:import java.security.MessageDigest)) | |
(defn digest [string] | |
"Returns a SHA-1 digest of the given string." | |
(.digest (MessageDigest/getInstance "SHA-1") | |
(.getBytes string))) | |
(defn hex [bt] | |
"Returns a hexadecimal value of the given byte." |
NewerOlder