1.2.3-SNAPSHOT Arrow_down_16x16
  • ($fn col-bindings body)
A simple macro used as syntactic sugar for defining predicate functions to be used
in the $where function. The supplied arguments should be column names of a dataset.
This macro performs map destructuring on the arguments.

For instance,
($fn [speed] (< speed 10)) => (fn [{:keys [speed]}] (< speed 10))

Examples:
(use '(incanter core datasets))
(view ($where ($fn [speed dist] (or (> speed 20) (< dist 10))) (get-dataset :cars)))

(view ($where ($fn [speed dist] (< (/ dist speed) 2)) (get-dataset :cars)))

(use '(incanter core datasets charts))
(with-data (get-dataset :cars)
(doto (scatter-plot :speed :dist :data ($where ($fn [speed dist] (< (/ dist speed) 2))))
(add-points :speed :dist :data ($where ($fn [speed dist] (>= (/ dist speed) 2))))
(add-lines ($ :speed) (mult 2 ($ :speed)))
view))


(let [passed? ($fn [speed dist] (< (/ dist speed) 2))
failed? (complement passed?)]
(with-data (get-dataset :cars)
(doto (scatter-plot :speed :dist :data ($where passed?))
(add-points :speed :dist :data ($where failed?))
(add-lines ($ :speed) (mult 2 ($ :speed)))
view)))


(use '(incanter core stats charts))
(let [above-sine? ($fn [col-0 col-1] (> col-1 (sin col-0)))
below-sine? (complement above-sine?)]
(with-data (conj-cols (sample-uniform 1000 :min -5 :max 5)
(sample-uniform 1000 :min -1 :max 1))
(doto (function-plot sin -5 5)
(add-points :col-0 :col-1 :data ($where above-sine?))
(add-points :col-0 :col-1 :data ($where below-sine?))
view)))


(view ($where ($fn [] (> (rand) 0.9)) (get-dataset :cars)))

(view ($where ($fn [Species] ($in Species #{"virginica" "setosa"})) (get-dataset :iris)))

0 Examples top

Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source incanter/core.clj:1751 top

(defmacro $fn
" A simple macro used as syntactic sugar for defining predicate functions to be used
  in the $where function. The supplied arguments should be column names of a dataset. 
  This macro performs map destructuring on the arguments.
  
  For instance, 
  ($fn [speed] (< speed 10)) => (fn [{:keys [speed]}] (< speed 10))

  Examples:
    (use '(incanter core datasets))
    (view ($where ($fn [speed dist] (or (> speed 20) (< dist 10))) (get-dataset :cars)))

    (view ($where ($fn [speed dist] (< (/ dist speed) 2)) (get-dataset :cars)))

    (use '(incanter core datasets charts))
    (with-data (get-dataset :cars)
      (doto (scatter-plot :speed :dist :data ($where ($fn [speed dist] (< (/ dist speed) 2))))
        (add-points :speed :dist :data ($where ($fn [speed dist] (>= (/ dist speed) 2))))
        (add-lines ($ :speed) (mult 2 ($ :speed)))
        view))


    (let [passed? ($fn [speed dist] (< (/ dist speed) 2))
          failed? (complement passed?)]
      (with-data (get-dataset :cars)
        (doto (scatter-plot :speed :dist :data ($where passed?))
          (add-points :speed :dist :data ($where failed?))
          (add-lines ($ :speed) (mult 2 ($ :speed)))
          view)))


    (use '(incanter core stats charts))
    (let [above-sine? ($fn [col-0 col-1] (> col-1 (sin col-0)))
          below-sine? (complement above-sine?)]
      (with-data (conj-cols (sample-uniform 1000 :min -5 :max 5) 
                            (sample-uniform 1000 :min -1 :max 1))
        (doto (function-plot sin -5 5)
          (add-points :col-0 :col-1 :data ($where above-sine?))
          (add-points :col-0 :col-1 :data ($where below-sine?))
          view)))


    (view ($where ($fn [] (> (rand) 0.9)) (get-dataset :cars)))

    (view ($where ($fn [Species] ($in Species #{\"virginica\" \"setosa\"})) (get-dataset :iris)))

"
  ([col-bindings body]
     `(fn [{:keys ~col-bindings}] ~body)))
Vars in incanter.core/$fn: defmacro apply concat fn hash-map list seq vector
Used in 0 other vars

Comments top

No comments for $fn. Log in to add a comment.