Skip to content

Instantly share code, notes, and snippets.

@namoopsoo
Last active December 16, 2017 00:56
Show Gist options
  • Save namoopsoo/fa903799b958ffc9f279cd293e83e9d9 to your computer and use it in GitHub Desktop.
Save namoopsoo/fa903799b958ffc9f279cd293e83e9d9 to your computer and use it in GitHub Desktop.
more cloj notes

basic callbacks...

(comment 
  "basic callback"
  (let [callback-fn #(+ 5 %)
        ]
    (callback-fn 10))
)
  • ...
user=>   (let [callback-fn #(+ 5 %)
  #_=>         ]
  #_=>     (callback-fn 10))
15

callback w future

(comment
  "future w callback func"
  (defn use-callback-when-done
    [callback-fn]
    (future (callback-fn (+ 4 5))))

  (def output (use-callback-when-done #(println "printings.. " % " .end")))
)
  • =>
user=> (def output (use-callback-when-done #(println "printings.. " % " .end")))
#'user/output
printings..  9  .end
user=> 

callback and core async...

(comment
  "use a go put onto a channel callback..."
  (defn use-callback-when-done
    [callback-fn]
    (future (callback-fn (+ 4 5))))
  
  (require 
    ['clojure.core.async
     :refer ['>! '<! '>!! '<!! 'go 'chan
             'alts! 'alts!! 'timeout]])
  (let [
        my-results-chan (chan)
        callback-fn #(go (>! my-results-chan %))
        output (use-callback-when-done callback-fn)
        ]
    ; (fn [x] (<!! my-results-chan))
    (println "looking at the channel: " (<!! my-results-chan))
    (println "done..")
    )
)
  • ==>
user=>   (let [
  #_=>         my-results-chan (chan)
  #_=>         callback-fn #(go (>! my-results-chan %))
  #_=>         output (use-callback-when-done callback-fn)
  #_=>         ]
  #_=>     ; (fn [x] (<!! my-results-chan))
  #_=>     (println "looking at the channel: " (<!! my-results-chan))
  #_=>     (println "done..")
  #_=>     )
looking at the channel:  9
done..
nil
user=>
@namoopsoo
Copy link
Author

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=> 

@namoopsoo
Copy link
Author

namoopsoo commented Dec 12, 2017

atom vec [] and while loop...

(let [results-atom (atom [])]
    ;
    ; (println "elements: " (count @results-atom))
    ; (swap! results-atom conj "hi")
    ;results-atom
    (while
      (< (count @results-atom) 3)
      (do
        (println "doing")
        ; insert
        (swap! results-atom conj "hi")
        (println "elements: " (count @results-atom))
          ))
    ; done
    (println "Done. Now have elements: " (count @results-atom))
    )
  • =>
doing
elements:  1
doing
elements:  2
doing
elements:  3
Done. Now have elements:  3
nil

@namoopsoo
Copy link
Author

tools.trace debugging exceptions and stack trace

user=> (use 'clojure.tools.trace)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment