Skip to content

Instantly share code, notes, and snippets.

@ithayer
Last active December 13, 2015 20:08
Show Gist options
  • Save ithayer/4967560 to your computer and use it in GitHub Desktop.
Save ithayer/4967560 to your computer and use it in GitHub Desktop.
;; Create a function that given visitors, computes
;; the spend for each - here, just a random number.
user> (defgraphfn spends [raw-visitors]
(map (fn [_] (rand-int 10))
raw-visitors))
#'user/spends
;; Create a function that given visitors, computes the
;; browser for each - here, just randomly assigned.
user> (defgraphfn browsers [raw-visitors]
(map (fn [_] (rand-nth [:chrome :firefox :ie-9]) )
raw-visitors))
#'user/browsers
;; Create a function that given visitors, browsers,
;; and spends, compiles all the info.
user> (defgraphfn compile-all [raw-visitors browsers spends]
(map #(assoc %1 :browser %2 :spend %3)
raw-visitors
browsers
spends))
#'user/compile-all
;; Execute the graph defining "raw-visitors" as input to
;; make sure it works.
user> (run-graph {:raw-visitors [{:id 1} {:id 2} {:id 3}]}
compile-all browsers spends)
{:all-visitors ({:spend 4, :browser :ie-9, :id 1}
{:spend 1, :browser :firefox, :id 2}
{:spend 9, :browser :firefox, :id 3}),
:spends (4 1 9),
:browsers (:ie-9 :firefox :firefox),
:raw-visitors [{:id 1} {:id 2} {:id 3}]}
;; And make a function with no args that executes the graph.
user> (defn all-visitors []
(run-graph {:raw-visitors [{:id 1} {:id 2} {:id 3}]}
compile-all
browsers
spends))
#'user/all-visitors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment