Skip to content

Instantly share code, notes, and snippets.

@robinkraft
Created February 13, 2012 03:10
Show Gist options
  • Save robinkraft/1813110 to your computer and use it in GitHub Desktop.
Save robinkraft/1813110 to your computer and use it in GitHub Desktop.
Explorations with Cascalog
;; Sam, I got up to 21, some of which now reside forma-clj
(def nums
[1
2
3
4
5
6])
(def fires ["lat,lon,bright,conf" "1.23,34.2,330,75" "14.3,49.2,329,72"])
(fact
"Get sum of nums"
(<- [?sum]
(nums ?nums)
(c/sum ?nums :> ?sum)) => (produces [[21]]))
(fact
"Count nums"
(<- [?count]
(nums ?nums)
(c/count ?count)) => (produces [[6]]))
(fact
"Get average of nums"
(<- [?avg]
(nums ?nums)
(c/count ?count)
(c/sum ?nums :> ?sum)
(div ?sum ?count :> ?avg)) => (produces [[(float 7/2)]]))
(deffilterop evens?
[num]
(cond even? num) num nil)
(fact
"Sum up even numbers"
(<- [?sum]
(nums ?nums)
(even? ?nums)
(c/sum ?nums :> ?sum)) => (produces [[12]]))
(defn int-sqrt? [n]
(== (int (Math/sqrt n)) (Math/sqrt n)))
(fact
"Checks whether a number's sqrt is an integer"
(<- [?nums]
(nums ?nums)
(int-sqrt? ?nums)) => (produces [[4] [1]]))
(defn squared [n]
(* n n))
(fact
"Check whether n^2 < 10"
(<- [?nums]
(nums ?nums)
(squared ?nums :> ?squared-nums)
(> 10 ?squared-nums)) => (produces [[1] [2] [3]]))
(fact
"Check whether I'm in the list"
(let [names ["robin" "dan" "sam"]]
(<- [?truth]
(names ?names)
(= "robin" ?names :> ?truth)
(truthy ?truth))) => (produces [[true]]))
(fact
"parsing strings"
(let [stuff ["asdf" "dlkfj" "ekfjdka"]]
(<- [?strings]
(stuff ?strings)
(re-find #"a" ?strings :> ?has-a)
(= "a" ?has-a))) => (produces [["asdf"] ["ekfjdka"]]))
(fact
"fires-style skipping header"
(<- [?first-three]
(fires ?fires)
(not= ?first-three "lat")
(subs ?fires 0 3 :> ?first-three)) => (produces [["1.2"] ["14."]]))
(fact
"count of valid fires"
(<- [?count]
(fires ?fires)
(not= ?first-three "lat")
(subs ?fires 0 3 :> ?first-three)
(c/count ?count)) => (produces [[2]]))
(defn strings->floats
"Accepts any number of string representations of floats, and
returns the corresponding sequence of floats."
[& strings]
(map #(Float. %) strings))
(fact
"mean lat/lon of valid fires"
(<- [?avg-lat ?avg-lon]
(fires ?fire)
(not= ?first-three "lat")
(subs ?fire 0 3 :> ?first-three)
;;(c/count ?count)
(clojure.string/split ?fire #"," :> ?s-lat ?s-lon _ _)
(c/avg ?lat :> ?avg-lat)
(c/avg ?lon :> ?avg-lon)
(strings->floats ?s-lat ?s-lon :> ?lat ?lon)) => (produces
[[7.765000104904175
41.70000076293945]]))
(fact
"mean brightness and confidence"
(<- [?avg-bright ?avg-conf]
(fires ?fire)
(not= ?first-three "lat")
(subs ?fire 0 3 :> ?first-three)
(clojure.string/split ?fire #"," :> _ _ ?s-bright ?s-conf)
(c/avg ?bright :> ?avg-bright)
(c/avg ?conf :> ?avg-conf)
(strings->floats ?s-bright ?s-conf :> ?bright ?conf)) => (produces [[329.5
73.5]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment