I am parsing a CSV file, and since the CSV does not have type information, all values ββ(float, ints, date, etc.) become strings. To fix the types, I created a map that defines each type of field. Now I need to convert the fields to the appropriate types.
Given a map where the values ββare strings containing integers and floats and possibly other types, I need to return a map with the values ββthat were converted to their corresponding types by referencing the type definition map. Below is a sample code that I came up with, but I believe there should be a better way to do this.
(mapv #(case ({"one" :int, "point-two" :float} (key %)) :int {(key %) (Integer/parseInt (val %))} :float {(key %) (Float/parseFloat (val %))} {(key %) (val %)}) ; If there no type defined, just return the original {"one" "1", "point-two" ".2", "three" "three"})
By re-creating the map in each case, the result is needed, it seems that there should be a way to simply change the values ββwithout touching the keys inside the case . Re-creating a map entry using {(key %) (val %)} for the default test seems even more inconvenient.
source share