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 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." |
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 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 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 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 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 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 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 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
;; the initial namespace in REPL is 'user' | |
(def my-var "var in the user namespace") | |
(ns new-namespace) | |
my-var | |
;; -> CompilerException java.lang.RuntimeException: | |
;; Unable to resolve symbol: my-var in this context |
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
def spicy = { first, second, third -> | |
println "I like food with $first, $second and $third." | |
} | |
def curry = spicy.curry("curry") | |
def moreCurry = curry.curry("curry") | |
def curryAndChilli = moreCurry.curry("chilli") | |
curryAndChilli.call() | |
// prints: |
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 spicy [first second third] | |
(println "I like food with" first "," second "and" third ".")) | |
(def curry (partial spicy "curry")) | |
(def more-curry (partial curry "curry")) | |
(def curry-and-chilli (partial more-curry "chilli")) | |
(curry-and-chilli) |
OlderNewer