Skip to content

Instantly share code, notes, and snippets.

Created June 7, 2013 07:02

Revisions

  1. @invalid-email-address Anonymous created this gist Jun 7, 2013.
    29 changes: 29 additions & 0 deletions foo.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    (defn to-float [x]
    (when x (Float/parseFloat x)))

    (defn to-int [x]
    (when x (Integer/parseInt x)))

    (defn process-data [data]
    (let [field-format (array-map :id to-int
    :name identity
    :score to-float)]
    (->> data
    (map (fn [fields] ;; Create record
    (->> fields
    (map #(get {"-" nil "" nil} % %)) ;; Normalize field values, handle nil
    (map #(%1 %2) (vals field-format)) ;; Convert fields into proper types
    (zipmap (keys field-format)) ;; Convert record into map
    ))))))

    (comment
    (process-data [["1" "one" "0.1"]
    ["2" "two" "0.2"]
    ["3" "three" "0.3"]
    ["4" "four" "-"]])
    ;; returns
    ({:score 0.1, :name "one", :id 1}
    {:score 0.2, :name "two", :id 2}
    {:score 0.3, :name "three", :id 3}
    {:score nil, :name "four", :id 4})
    )