Skip to content

Commit

Permalink
Merge pull request incanter#238 from Gerrrr/feature/sel_string
Browse files Browse the repository at this point in the history
get-column-id returns keyword version of column-key if convenient
  • Loading branch information
alexott committed Apr 9, 2014
2 parents 8de1ccf + d4eb04b commit ffcde29
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
34 changes: 21 additions & 13 deletions modules/incanter-core/src/incanter/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1211,19 +1211,27 @@


(defn- get-column-id [dataset column-key]
(let [headers (:column-names dataset)
col-key (if (and
(keyword? column-key) ;; if the given column name is a keyword, and
(not (some #{column-key} headers))) ; a keyword column name wasn't used in the dataset
(name column-key) ;; convert the keyword to a string
column-key) ;; otherwise use the given column key
id (if (number? col-key)
(if (some #(= col-key %) headers)
col-key
(nth headers col-key))
col-key)]
id))

(let [headers (:column-names dataset)]
(cond
(and (keyword? column-key) ;; if the given column name is a keyword, and
;; a keyword column name wasn't used in the dataset
(not (some #{column-key} headers)))
(name column-key) ;; convert the keyword to a string

(and (string? column-key) ;; if the given column is a string, and
;; this column was't used in the dataset, and
(not (some #{column-key} headers))
;; a keyword column name was used in the dataset
(some #{(keyword column-key)} headers))
;; convert string to keyword
(keyword column-key)

(and (number? column-key) ;; if the given column name is a number
;; and this number is not in headers
(not (some #(= column-key %) headers)))
(nth headers column-key) ;; get nth column from headers

:else column-key)))

(defn- map-get
([m k]
Expand Down
3 changes: 3 additions & 0 deletions modules/incanter-core/test/incanter/core_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@

(deftest dataset-tests
(is (= (sel dataset1 :cols :a) [1 4]))
(is (= (sel dataset1 :cols "a") [1 4]))
(is (= (sel dataset1 :all 1) [2 5]))
(is (= (sel dataset1 :all :b) [2 5]))
(is (= (sel dataset1 :all [:a :c]) (dataset [:a :c] [[1 3] [4 6]])))
(is (= (sel dataset1 :all ["a" "c"]) (dataset [:a :c] [[1 3] [4 6]])))
(is (= (sel dataset1 :all [:a]) (dataset [:a] [[1] [4]])))
(is (= (sel dataset1 :all ["a"]) (dataset [:a] [[1] [4]])))
(is (= (sel dataset1 :all :all) dataset1))
(is (= (sel dataset2 :cols :b) [2 5]))
(is (= (sel dataset2 :cols "c") [3 6]))
Expand Down

0 comments on commit ffcde29

Please sign in to comment.