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
| ------------------------- | |
| ;clojure.core/fnil | |
| ;([f x] [f x y] [f x y z]) | |
| ; Takes a function f, and returns a function that calls f, replacing | |
| ; a nil first argument to f with the supplied value x. Higher arity | |
| ; versions can replace arguments in the second and third | |
| ; positions (y, z). Note that the function f can take any number of | |
| ; arguments, not just the one(s) being nil-patched. | |
| (+ nil 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
| ;------------------------- | |
| ;clojure.core/merge-with | |
| ;([f & maps]) | |
| ; Returns a map that consists of the rest of the maps conj-ed onto | |
| ; the first. If a key occurs in more than one map, the mapping(s) | |
| ; from the latter (left-to-right) will be combined with the mapping in | |
| ; the result by calling (f val-in-result val-in-latter). | |
| ; key/value pairs representing order-id and order-quantity | |
| (def map1 {1 44 2 33}) |
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
| (use '[clojure.contrib.map-utils | |
| :only [deep-merge-with]]) | |
| (defn add-path [h path] | |
| (let [dir (butlast path) | |
| entry (last path)] | |
| (update-in h dir (fn [x] (if x (conj x entry) [entry]))))) | |
| (defn restore-hierarchy [paths] | |
| (reduce add-path {} 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
| (defn fib [n] | |
| (if (= n 0) 0 | |
| (if (= n 1) 1 | |
| (+ (fib (- n 1)) (fib (- n 2)))))) | |
| (defn lazy-seq-fibo | |
| ([] | |
| (concat [0 1] (lazy-seq-fibo 0 1))) | |
| ([a b] | |
| (let [n (+ a b)] |
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 pas.tree | |
| (:use [incanter core io charts datasets])) | |
| (def reg-cntry-list | |
| {"America" ["USA" "Canada" "Mexico" "Venezuala" "Brazil" "Argentina" "Cuba"] | |
| "Asia" ["India" "Pakistan" "Singapore" "China" "Japan" "Sri Lanka" "Malaysia"] | |
| "Europe" ["UK" "Germany" "France" "Italy" "Belgium" "Turkey" "Finland"] | |
| "Middle East" ["Saudi Arabia" "Bahrain" "UAE" "Kuwait" "Yemen" "Qatar" "Iraq"] | |
| "Africa" ["Libya" "Tanzania" "South Africa" "Kenya" "Ethiopia" "Morocco" "Zimbabwe"]}) |
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
| (declare my-odd?) | |
| (defn my-even? [n] | |
| (if (zero? n) | |
| true | |
| (my-odd? (dec n)))) | |
| (defn my-odd? [n] | |
| (if (zero? n) | |
| false |
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 pas.perf) | |
| (require 'pas.perf :reload) | |
| ;time and load perf data | |
| (time (def p (load-perf-data))) | |
| ;show key | |
| (.toString (first (keys p)) |
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 pos-add [& args] | |
| (apply + args)) | |
| (defn pos-add [& args] | |
| {:pre [(not-any? neg? args)] | |
| :post [(<= 0 %)]} | |
| (apply + args)) |
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 xors [max-x max-y] | |
| (for [x (range max-x) y (range max-y)] | |
| [x y (bit-xor x y)])) | |
| (xors 2 2) | |
| (def frame (java.awt.Frame.)) | |
| (for [method (seq (.getMethods java.awt.Frame)) | |
| :let [method-name (.getName method)] |
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
| (+ 1 2 3) | |
| (defn average [x y] (/ (+ x y) 2)) | |
| (map (addx 5) [1 2 3 4 5]) | |
| (map + [1 2 3] [4 5 6]) | |
| (map average [1 2 3] [4 5 6]) |