Sunday, May 1, 2016

Clojure: conj/merge/assoc-in two maps together where one map is atom and store result to atom map

What I will try to do may be nonlegal in Clojure or at least not so welcome. I still think in terms of state and mutable objects that's why I want to use atomic collections: I try to use something like this:
(ns clojure.examples.maping_examples
 (:gen-class))
(def collection (atom {:ver "0.0.1" })) 
(println
(conj  @collection {:Bank_Of_America 2} {:Land 4 :Bank_Of_America 6})
)
Which give me the nextresult. Please pay attention for using @ before collection, I need not atom object but his content ( :value )
{:ver 0.0.1, :Bank_Of_America 6, :Land 4}    

So now I have a problem , I have what I need. But I can't store it. Or storing again required looping over all collection which not effective. Let's go to google. I found one more wayto do it by using merge Let's modify code:
(ns clojure.examples.maping_examples
 (:gen-class))
(def collection (atom {:ver "0.0.1" })) 
(println
(merge  @collection {:Bank_Of_America 2} {:Land 4 :Bank_Of_America 6})
)
The same result before. What's the difference. Let's check time duration
(ns clojure.examples.maping_examples
 (:gen-class))
(def collection (atom {:ver "0.0.1" })) 
(println
(time (:dowith (merge  @collection {:Bank_Of_America 2} {:Land 4 :Bank_Of_America 6})))
)
(println
(time (:dowith (conj    @collection {:Bank_Of_America 2} {:Land 4 :Bank_Of_America 6})))
)
After googling I found appropriate formula:  assoc-in I couldn't achieve to add collection to atomic map which purpose of this blog, but still usefull to know how this function works:
(println 
(swap! collection assoc-in [] {:Bank_Of_America 2})
)

(println 
(swap! collection assoc-in [":ver" ] {:Bank_Of_America 2})
)

(println 
(swap! collection assoc-in [":ver" ] [:Bank_Of_America 2])
)

(println 
(swap! collection assoc-in [":ver" ] #{:Bank_Of_America 2})
)

(println 
(swap! collection assoc-in [":ver" ] (:Bank_Of_America 2))
)
Result:
{:ver 0.0.1, nil {:Bank_Of_America 2}}                                                                                                                                                                                                                 
{:ver 0.0.1, nil {:Bank_Of_America 2}, :ver {:Bank_Of_America 2}}                                                                                                                                                                                      
{:ver 0.0.1, nil {:Bank_Of_America 2}, :ver [:Bank_Of_America 2]}                                                                                                                                                                                      
{:ver 0.0.1, nil {:Bank_Of_America 2}, :ver #{:Bank_Of_America 2}}                                                                                                                                                                                     
{:ver 0.0.1, nil {:Bank_Of_America 2}, :ver nil}


Conclusion: I couldn't achieve storing one map to another in one function. Initial purpose of this post was to find solution which could do it one operation. From others post "Visualization generic CSV files " I could generate frequency nap for one CSV column . Column name  was "Financial product.In post Using Variance chart with Clojure generated data this hasmap was presented as inline CSV and used as input parameters for cVariant charts.  I tried to accumulate collection of Cartesian pairs    that is collection of all possible unique pairstaken from  all rows of CSV file . The  header of result CSV will be  like this for example [Financial Organization, Financial product], or in Clojure map like this {:Bank_Of_America-Loan: 2345, .... }
After some reseach and googling I finally foud formula and even could apply it for atomic collections here it is:


(-> 
(reduce #(swap! collection assoc %2 (inc (%1 %2 0)))
        {}
        (reduce conj (cartesian-pairs my-coll) (alones my-coll) ))
println
)

(println "Atomic collection: " @collection)
To check how this formula works in details see this blog: CREATE ALL COMBINATIONS [X] AND [X Y] WHERE X,Y SET'S ELEMENTS

No comments: