list tables, first get the faraday class thing ready. boot.user=> (use '[taoensso.faraday :as dynamo]) with a client-config hash defined in a separate clj file, here one/dynamo.clj is my file. (def client-config (if (:development env) {:access-key "OMGDEVELOPMENT" :secret-key "I_SHOULD_KEEP_THIS_SECRET!" ; Point the configuration at the DynamoDB Local :endpoint "http://localhost:8000"} {:endpoint "http://dynamodb.us-east-1.amazonaws.com"} ) ) and use list-tables from the module/class thing, boot.user=> (use '[one.dynamo :as db]) ; `one/dynamo.clj` boot.user=> (dynamo/list-tables db/client-config) (:primes :projects :times) create table to get this env part to work, I didnt see any way to set the env vars in clojure, so I just set them on my shell, ...
Handies
read (use 'clojure.java.io) (with-open [rdr (reader "/tmp/test.txt")] (doseq [line (line-seq rdr)] (println line))) write (use 'clojure.java.io) (with-open [wrtr (writer "/tmp/test.txt")] (.write wrtr "Line to be written"))
sleeping… (Thread/sleep 4000) simple multithreading , from the Brave clojure book (future (Thread/sleep 4000) (println "I'll print after 4 seconds")) (println "I'll print immediately") hmm this is weird. so dereferencing the future blocks? (defn fight-crime [] (let [] (println "hi") (Thread/sleep 2000) (println "ho") (Thread/sleep 1000) (println "yo") 5 )) (let [result (future (fight-crime))] (println "@: " @result) (println "snore. " ) (println "@: " @result) (Thread/sleep 1000) (println "@: " @result) ) Ah ok, but you can stop waiting.. so here we wait 10ms and then return “hmmf” if future isnt done yet. (let [result (future (fight-crime))] (println "@: " (deref result 10 "hmmf")) (println "snore. " ) (println "@: " (deref result 10 "hmmf")) (Thread/sleep 5000) (println "@: " (deref result 10 "hmmf")) ) delays. cool (def jackson-5-delay (delay (let [message "Just call my name and I'll be there"] (println "First deref:" message) message))) and a colorful example with future, force, delay , from http://www.braveclojure.com/concurrency/ (def gimli-headshots ["serious.jpg" "fun.jpg" "playful.jpg"]) (defn email-user [email-address] (println "Sending headshot notification to" email-address)) (defn upload-document "Needs to be implemented" [headshot] true) (let [notify (delay (email-user "and-my-axe@gmail.com"))] (doseq [headshot gimli-headshots] (future (upload-document headshot) (force notify)))) Blocking thread in action… ; make a future which blocks on a promise. Then deliver promise in main thread and see what happens. (def a-promise (promise)) (def futr (future (def result (+ @a-promise 10)) (Thread/sleep 5000) (println "result " result) result)) ; (println "nothing yet: " (deref futr 10 "nothing.")) ; deliver.. (deliver a-promise 99) (println "right after deliverin " (deref futr 10 "still nothin.")) (Thread/sleep 5500) (println "had some time to think... " (deref futr)) delivering to promise multiple times? hmm, how about the other way around… make some workers do something and have them all try deliver same promise. What will happen? (def another-promise (promise)) (def d1 (delay (Thread/sleep 6000) (deliver another-promise 6000) )) ; (def d2 (delay (Thread/sleep 4000) (deliver another-promise 4000) )) ; (def d3 (delay (Thread/sleep 1000) (deliver another-promise 1000) )) ; nothing there right? (realized? another-promise) ; now run them all... (doseq [a-worker [d1 d2 d3]] (future (force a-worker)) ) (println "Check promise: " (deref another-promise 10 "nothin.")) (println "Check promise: " @another-promise) hmm, for the above, strange that I tried but was not able to deref the delays. Error was ClassCastException clojure.lang.Delay cannot be cast to java.util.concurrent.Future clojure.core/deref-future (core.clj:2206) exercise in http://www.braveclojure.com/concurrency/ given the yak butter search data… (def yak-butter-international {:store "Yak Butter International" :price 90 :smoothness 90}) (def butter-than-nothing {:store "Butter Than Nothing" :price 150 :smoothness 83}) ;; This is the butter that meets our requirements (def baby-got-yak {:store "Baby Got Yak" :price 94 :smoothness 99}) (defn mock-api-call [result] (Thread/sleep 1000) result) (defn satisfactory? "If the butter meets our criteria, return the butter, else return false" [butter] (and (<= (:price butter) 100) (>= (:smoothness butter) 97) butter)) The original non concurrent code was … (time (some (comp satisfactory? mock-api-call) [yak-butter-international butter-than-nothing baby-got-yak])) ; => "Elapsed time: 3002.132 msecs" ; => {:store "Baby Got Yak", :smoothness 99, :price 94} and a concurrent version… (defn blah-func [] (def best-butter-promise (promise)) (time (doseq [some-butter [yak-butter-international butter-than-nothing baby-got-yak]] (future (if ((comp satisfactory? mock-api-call) some-butter) (deliver best-butter-promise some-butter) ))) ) (time (println "result is: " @best-butter-promise)) ) (blah-func) => cloj-multiproc-play.core=> (blah-func) "Elapsed time: 0.615436 msecs" result is: {:store Baby Got Yak, :price 94, :smoothness 99} "Elapsed time: 1001.655823 msecs" nil cloj-multiproc-play.core=> one more example , racing Bing vs Google (def search-result-promise (promise)) (def bingurl "https://www.bing.com/search?q=foobar") (def googleurl "https://www.google.com/?gws_rd=ssl#q=foobar") (doseq [url [bingurl googleurl]] (future (deliver search-result-promise (slurp url) )) ) (def html1 (slurp "https://www.google.com/?gws_rd=ssl#q=foobar")) hot dog machine from http://www.braveclojure.com/core-async/ my version of a hot dog machine that only dispenses max number of hot dogs… (defn hot-dog-machine-v2 [how-many-dogs-init] (let [in (chan) out (chan)] (go (loop [num-dogs how-many-dogs-init] ; if no hot dogs left, then done. (if (= num-dogs 0) true ; Otherwise (do (println "Going to <! block on <in> now.") (<! in) (println "Now we have " num-dogs " dogs.") (println "Going to >! block on <out> now.") (>! out "hot dog") )) (recur (dec num-dogs)) )) [in out] )) ; get channels.. (def machine-channels (hot-dog-machine-v2 5)) (def in (first machine-channels)) (def out (last machine-channels)) ; ... dispensing them (println "one") (>!! in "pocket lint") (<!! out) (println "two") (>!! in "pocket lint") (<!! out) (println "three") (>!! in "pocket lint") (<!! out) (println "four") (>!! in "pocket lint") (<!! out) (println "five") (>!! in "pocket lint") (<!! out) .... ; that was fine except we went into negative hot dogs... playsync.core=> (println "five") five nil playsync.core=> (>!! in "pocket lint") trueNow we have 1 dogs. Going to >! block on <out> now. playsync.core=> (<!! out) Going to <! block on <in> now. "hot dog" playsync.core=> playsync.core=> (>!! in "pocket lint") Now we have -1 dogs. true Going to >! block on <out> now. playsync.core=> (<!! out) "hot dog"Going to <! block on <in> now. .. update (let [results-chan (chan) results-vector (atom [])] ; For several things in stuff-vec , start some async call that will throw results, ; into the results-chan (doseq [x stuff-vec] (do-something-async results-chan x)) ; in this case, we take from the results-chan once they are ready, ; and update (using conj) the atom results-vector (doseq [_ stuff-vec] (swap! results-vector conj (<!! results-chan))) ) swap syntax for updating atoms…
test http-kit timeout.. look at result for a timeout 1ms.. (require ['org.httpkit.client :as 'http]) (let [options {:timeout 1} url "http://yahoo.com" ] (def vout @(http/get url options))) ==> user=> (keys vout) (:opts :error) user=> vout {:opts {:timeout 1, :method :get, :url "http://yahoo.com"}, :error #error { :cause "read timeout: 1ms" :via [{:type org.httpkit.client.TimeoutException :message "read timeout: 1ms" :at [org.httpkit.client.HttpClient clearTimeout "HttpClient.java" 82]}] :trace [[org.httpkit.client.HttpClient clearTimeout "HttpClient.java" 82] [org.httpkit.client.HttpClient run "HttpClient.java" 433] [java.lang.Thread run "Thread.java" 748]]}} user=> (type (:error vout)) org.httpkit.client.TimeoutException Answer the question: does http-kit func use the callback if there is an {:error ...} ? … (let [options {:timeout 1} url "http://yahoo.com" callback-fn #(println "callback yay! ____" % "_____") ] ;(def vout @(http/get url options)) ;(def vout @(http/get url options callback-fn)) (def vout (http/get url options callback-fn)) ) => well looks like the Callback func is still called on an error. callback yay! ____ {:opts {:timeout 1, :method :get, :url http://yahoo.com}, :error #error { :cause read timeout: 1ms :via [{:type org.httpkit.client.TimeoutException :message read timeout: 1ms :at [org.httpkit.client.HttpClient clearTimeout HttpClient.java 82]}] :trace [[org.httpkit.client.HttpClient clearTimeout HttpClient.java 82] [org.httpkit.client.HttpClient run HttpClient.java 433] [java.lang.Thread run Thread.java 748]]}} _____ user=>
class cast exception clojure.lang.LazySeq cannot be cast to clojure.lang.IFn => some code is expecting a function, but is getting a LazySeq.
Amazonica and s3 This was not super obvious, because this example uses a java.io.ByteArrayInputStream with the the :input-stream parameter of the put-object function But in my mind this feels more like an output stream since we’re writing. but maybe this is because we’re reading from the payload . (require ['amazonica.aws.s3 :as 'ss3]) (defn put-s3-obj [bucket-name s3key content] (let [payload (.getBytes content "UTF-8") input-stream (java.io.ByteArrayInputStream. payload)] (ss3/put-object :bucket-name bucket-name :key s3key :input-stream input-stream ; :metadata {:server-side-encryption "AES256"} ;? ;:file content )))
logging, https://github.com/futurice/timbre ; require... [taoensso.timbre :as log] ; i have ended up using it like this, in a let, with fake variables, (let [ var1 (myfunc "blah") fake1 (log/info (str "var1: " var1))] () ; do stuff)
I am used to python’s zip zip([1, 2, 3], ['a', 'b', 'c']) # [(1, 'a'), (2, 'b'), (3, 'c')] Interleaving and partitioning can do the same thing From stackoverflow , below, This is so clever … (partition 2 (interleave '(1 2 3) '(4 5 6))) ; => ((1 4) (2 5) (3 6)) ; or more generally (defn zip [& colls] (partition (count colls) (apply interleave colls))) (zip '( 1 2 3) '(4 5 6)) ;=> ((1 4) (2 5) (3 6)) (zip '( 1 2 3) '(4 5 6) '(2 4 8)) ;=> ((1 4 2) (2 5 4) (3 6 8)) This was also a cool solution From here user=> (map vector [1 2 3] [4 5 6]) ([1 4] [2 5] [3 6]) user=>
? If on a repl, but wanting to simulate a namespace ns in a project file using discussion in https://www.braveclojure.com/organization/#Anchor-3 … (in-ns 'foo.my-test) hmm I thought that would give me access to the names in that namespace, but in my project, that didnt work… My namespace in .. has (ns foo.my-test (:require [org.httpkit.client :as http] [org.httpkit.fake])) and when i tried … user=> (in-ns 'foo.my-test) #object[clojure.lang.Namespace 0x37b2f7ef "foo.my-test"] foo.my-test=> (org.httpkit.fake/with-fake-http ["http://google.com/" "faked" #_=> "http://flickr.com/" 500] #_=> (:body @(http/get "http://google.com/")) ; "faked" #_=> (:status @(http/get "http://flickr.com/"))) ; 500 ClassNotFoundException org.httpkit.fake java.net.URLClassLoader.findClass (URLClassLoader.java:381) Also initially confused with … Starting new repl and cannot use require with raw library strings… user=> (require [org.httpkit.client :as http]) CompilerException java.lang.ClassNotFoundException: org.httpkit.client, compiling:(/private/var/folders/mj/7bwn1wld4pscycn91fpjn1h40000gn/T/form-init5251815666209634293.clj:1:1) but use works. user=> (use '[org.httpkit.client :as http]) WARNING: get already refers to: #'clojure.core/get in namespace: user, being replaced by: #'org.httpkit.client/get nil Hmm even so, I am using :as http, so why is use replacing the clojure.core/get in this namespace? but using a fresh repl, this worked: user=> (require ['org.httpkit.client :as 'http]) nil user=> user=> (def v1 @(http/get "https://www.braveclojure.com/organization/")) #'user/v1 user=> (keys v1) (:opts :body :headers :status) So here, 'org.httpkit.client is a symbol and not a variable like org.httpkit.client .
from clojure for machine learnings.. (defn plot-points "plots sample points of a solution s" [s] (let [X (concat (:hidden s) (:observed s)) Y (concat (:hidden-values s) (:observed-values s))] (view ; NOTE save instead of view can save to a file. (add-points (xy-plot X Y) (:observed s) (:observed-values s))))) ; namespace... ; [incanter "1.5.4"] (ns my-namespace (:use [incanter.charts :only [xy-plot add-points]] [incanter.core :only [view]]) (:require [clojure.core.matrix.operators :as M] [clatrix.core :as cl])) (ns my-namespace (:use clojure.core.matrix) (:require [clatrix.core :as cl])) ; from csv , to matrix.. (with-open [reader (io/reader "in-file.csv")] (doall (csv/read-csv reader)))