Skip to content

Instantly share code, notes, and snippets.

@aibrahim
Created November 11, 2018 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aibrahim/fd392d3efb3f31b091a8470d9e791d8a to your computer and use it in GitHub Desktop.
Save aibrahim/fd392d3efb3f31b091a8470d9e791d8a to your computer and use it in GitHub Desktop.
(:require [clojure.core.async :as a
:refer [>! <! >!! <!! go chan buffer close! thread alts! alts!! timeout]])
(def URLS ["https://www.haskell.org"
"https://www.amazon.com"
"https://www.youtube.com"])
(defn time-now []
(. System (nanoTime)))
(defn elapsed [start end]
(/ (double (- end start)) 1000000000.0))
(defn fetch [url ch]
(go
(let [start (time-now)
resp (slurp url)
n (count resp)]
(>! ch (format "%.2fs %7d %s" (elapsed start (time-now)) n url)))))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(let [start (time-now)
ch (chan (count URLS))]
(doseq [url URLS]
(fetch url ch))
(doseq [url URLS]
(println (<!! ch)))
(println (elapsed start (time-now)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment