Update multiple elements of a Clojure atom - clojure

I have a atom:
(def data (atom[ {:orderid 0 :productid 0 :description "A" :amount 2} {:orderid 1 :productid 1 :description "A" :amount 2}]))
and my swap function:
(defn edit-order [params]
(filter
#(and (= (:orderid %) (:orderid params)))
#data
(swap! data (fn [old new] (merge old new)) params))
The result I got is:
(println (edit-order {:orderid 0 :description "edited" :amount 3}))
;=> [{:orderid 0, :productid 0, :description A, :amount 2} {:orderid 1, :productid 1, :description A, :amount 2} {:orderid 0, :description edited, :amount 3}]
What I trying to do is update the new value to old value not just add it as a new one. How should I do that?
;=> [{:orderid 0, :productid 0, :description edited, :amount 3} {:orderid 1, :productid 1, :description A, :amount 2}]
Thanks for helping!

The way to go about this is:
(def data (atom
[{:orderid 0 :productid 0 :description "A" :amount 2}
{:orderid 1 :productid 1 :description "A" :amount 2}]))
(defn edit-order
[params]
(swap! data
(fn [old-orders]
(mapv (fn [order]
(if (= (:orderid order)
(:orderid params))
(merge order params)
order))
old-orders))))
(comment
(edit-order {:orderid 0 :description "edited" :amount 3})
#_ [{:orderid 0, :productid 0, :description "edited", :amount 3}
{:orderid 1, :productid 1, :description "A", :amount 2}]
)
Notice that you cannot mutate just one map inside a vector. You are creating an entirely new vector based on the old one, because the entire data structure inside the atom is (should be) immutable.

Another option, if you're doing a lot of nested structure manipulation, is to use https://github.com/nathanmarz/specter, in this case the "transform" operation.
(ns specterplay.core
(:require [com.rpl.specter :refer :all]))
(def data (atom[ {:orderid 0 :productid 0 :description "A" :amount 2} {:orderid 1 :productid 1 :description "A" :amount 2}]))
(defn edit-order!
[params data]
(swap! data
(fn [a] (transform [ALL #(= (:orderid params) (:orderid %))] #(merge % params) a))))
(edit-order! {:description "edited" :amount 3} data)
#data
;; [{:orderid 0, :productid 0, :description "edited", :amount 3} {:orderid 1, :productid 1, :description "A", :amount 2}]

Related

How do I filter a list of vectors in Clojure?

I am new to Clojure and learning the properties of various data structures in Clojure. Here, I have a list of vectors as follows:
(["1" "Christiano Ronaldo" "Portugal" "35"]
["2" "Lionel Messi" "Argentina" "32"]
["3" "Zinedine Zidane" "France" "47"])
where the first element of each vector is the id. How do I filter out single vectors from the list based on the id? For eg., id = 1 should return
["1" "Christiano Ronaldo" "Portugal" "35"]
I tried doing the same on a nested-map:
(def footballers
[
{:id 1 :name "Christiano Ronaldo" :country "Portugal" :age 35}
{:id 2 :name "Lionel Messi" :country "Argentina" :age 32}
{:id 3 :name "Zinedine Zidane" :country "France" :age 47}
]
)
and was successful using the filter function
(filter #(= (:id %) 1) footballers)
Result:
({:id 1, :name "Christiano Ronaldo", :country "Portugal", :age 35})
How do I do the same in a list of vectors using filter function?
(filterv #(= "1" (first %)) footballers) ; or `filter`
;=> [["1" "Christiano Ronaldo" "Portugal" "35"]] ; vector containing 1 vector
Please see this list of documentation.

Clojure. Update double nested value

I'm kind of newbie with Clojure, all is new but pretty fun too. So I have this data:
{:test {:title "Some Title"}, :questions [
{:id 1, :full-question {:question "Foo question", :id 1, :answers [{:id 7, :question_id 1, :answer "Foobar answer"}, {:id 8, :question_id 1, :answer "Foobar answer two"}]}},
{:id 5, :full-question {:question "Foo question", :id 5, :answers [{:id 12, :question_id 5, :answer "Foobar answer"}]}},
{:id 9, :full-question {:question "Foo question", :id 9, :answers [{:id 14, :question_id 9, :answer "Foobar answer"}, {:id 20, :question_id 9, :answer "Foobar answer two"}]}}
]}
A "classic" Test->Question->Answer kind of data structure. And I have this new info:
(def new-answer {:id 33, :answer "Another foobar answer", :question-id 9 })
I need to update the first structure to add "new-answer" into the "answers" for the :id number 9 in the :questions vector.
I tried with the update-in function but I don't know what to tell the correspondent :id in the maps inside the two vectors. I mean, I don't know how to build the "path" where I want to make the change.
also, there is a nice library for that kind of structural editing, called specter
your case could be solved like this:
(require '[com.rpl.specter :refer [ALL AFTER-ELEM setval]])
(defn add-answer [data {question-id :question-id :as new-answer}]
(setval [:questions ALL #(== question-id (:id %)) :full-question :answers AFTER-ELEM]
new-answer data))
user> (add-answer data {:id 33, :answer "Another foobar answer", :question-id 9 })
;;=> {:test {:title "Some Title"},
;; :questions
;; [
;; ;; ... all other ids
;; {:id 9,
;; :full-question
;; {:question "Foo question",
;; :id 9,
;; :answers
;; [{:id 14, :question_id 9, :answer "Foobar answer"}
;; {:id 20, :question_id 9, :answer "Foobar answer two"}
;; {:id 33, :answer "Another foobar answer", :question-id 9}]}}]}
You have the right idea with update-in. You can first calculate the index in your questions vector, then create the path :questions, "question-index", :full-question, :answers. Then you may conj in your new answer:
(def data {...})
(defn index-by-id
[v id]
(first (filter #(= (:id (v %)) id) (range (count v)))))
(defn add-answer
[answer]
(let [q-index (index-by-id (:questions data) (:question-id answer))]
(update-in data [:questions q-index :full-question :answers]
conj answer)))
Using clojure, you have the update-in function found here and the assoc found here. You can use the code suggested by Alex for update-in. assoc is fairly similar,
(defn change [ma a-map id]
(assoc (:questions ma)
(if-let [xq (first (filter int? (map-indexed (fn [idx mp] (if (= (:id mp) id) idx nil)) (:questions ma))))]
xq
(inc (count ma)))
a-map))
You can update your map as
(change o-map n-map idx) ;;param-1 is map to change,
;;param-2 is new-answer,
;;idx is the :id to change.
You can also refer to assoc-in found here which also associates a value in a nested associative structure.
Hope this helps.

Clojure specter merge navigated value with other map

I try to use Clojure specter to edit my orders information from "database". This application is base on Rest API.
My API call:
(PUT "/orders" []
:return :specs.models.order/orderSpec
:body-params [orderid :- :specs.models.order/orderid
{amount :- :specs.models.order/amount (orderController/getAmount orderid)}
{description :- :specs.models.order/description (orderController/getDescription orderid)}
{productid :- :specs.models.order/productid (orderController/getProductid orderid)}]
:summary "Edits the description and/or amount and/or productid of an order"
(getResponseFromContent (orderController/editOrder orderid amount description productid))
)
This is my "database":
[{:orderid 0 :productid 0 :description "A" :amount 2 :state "active"}
{:orderid 1 :productid 1 :description "A" :amount 2 :state "active"}]
It's in the different file and I use this to call it in model
(def filename "resources/mockorderDatabase.dat")
(def database (atom (try
(clojure.edn/read-string (slurp filename))
(catch Exception e []))))
This is my controller:
(defn editOrder
"Edit order's description and/or amount and/or productid by orderid"
[orderid amount description productid]
(if-let [editedorder (order/edit-order db/config {:orderid orderid :amount amount :description description :productid productid})]
(first editedorder)
nil))
and I am stuck with edit-order. How should I make it works?
So far what I came up with the edit-order:
(defn edit-order
[db orderid amount description productid]
(->> (transform [ALL (comp (partial = orderid) :orderid)]
#(assoc % :productid productid :amount amount :description description)
#database
)
)
)
the response i got is:
{
"type": "unknown-exception",
"class": "java.lang.IllegalArgumentException"
}
Would be better if I use setval instead of transfrom?
Thanks for helping!
You really don't need Specter for this:
(def data
[{:orderid 0 :productid 0 :description "A" :amount 2 :state "active"}
{:orderid 1 :productid 1 :description "A" :amount 2 :state "active"}])
(defn update-by-orderid [orders orderid description amount productid]
(vec
(for [order orders]
(if (= orderid (:orderid order))
(assoc order ; change if match
:description description
:amount amount
:productid productid)
order)))) ; return unchanged if no match
(update-by-orderid data 0 "DESC" 99 123) =>
[{:orderid 0,
:productid 123,
:description "DESC",
:amount 99,
:state "active"}
{:orderid 1,
:productid 1,
:description "A",
:amount 2,
:state "active"}]
(def filename "mockorderDatabase.dat")
(def database (atom (try
(clojure.edn/read-string (slurp filename))
(catch Exception e []))))
(require '[com.rpl.specter :refer :all])
(defn change-order [*database id productid amount desc]
(transform [ATOM ALL (comp (partial = id) :orderid)]
#(assoc % :productid productid :amount amount :description desc)
*database))
(prn (change-order database 0 111 222 "XXXX-YYYY"))

Accessing elements of a Clojure map in a vector of maps

I have:
(def moo (my-func))
which returns:
[{:id 1 :name "Bob"}
{:id 2 :name "Jane"}
{:id 3 :name "Greg"}]
How do I now access moo to get the :name where :id=3? Thanks.
I would rather prefer using some (since it is more logical than using filter i guess, because it is designed to find exactly one value):
(def data
[{:id 1 :name "Bob"}
{:id 2 :name "Jane"}
{:id 3 :name "Greg"}])
(defn name-by-id [id data]
(some #(when (= (:id %) id) (:name %)) data))
user>
(name-by-id 3 data)
"Greg"
user>
(name-by-id 100 data)
nil
One way would be to use a filter
(def moos
[{:id 1 :name "Bob"}
{:id 2 :name "Jane"}
{:id 3 :name "Greg"}])
(defn name-for-id
[id]
(:name (first (filter #(= (:id %) id) moos))))
(name-for-id 3) ; => "Greg"
(def names
[{:id 1 :name "Bob"}
{:id 2 :name "Jane"}
{:id 3 :name "Greg"}])
;;get the :name where :id=3
(defn answer []
(:name (first (filter (fn [e] (= 3 (:id e))) names))))
In the above rather than names you could have moo.

Merging Arrays in Clojure

I need to merge a collection of arrays based on id.
Example data:
EDIT: (changed to match Clojure data structures)
[{:id 1, :region :NA, :name :Test1, :OS :W}
{:id 1, :region :EU, :name :Test2, :OS :W}
{:id 2, :region :AS, :name :test3, :OS :L}
{:id 2, :region :AS, :name :test4, :OS :M}]
Becomes:
EDIT: (changed to match Clojure data structures)
[{:id 1, :region [:NA :EU], :name [:Test1 :Test2] ,:OS [:W]}
{:id 2, :region [:AS] :name [:test3 :Test4], :OS [:L :M]}]
| is the delimiter (changeable)
If possible, also would like alphabetical order as well.
(def data
[{:id 1, :region :NA, :name :Test1, :OS :W}
{:id 1, :region :EU, :name :Test2, :OS :W}
{:id 2, :region :AS, :name :test3, :OS :L}
{:id 2, :region :AS, :name :test4, :OS :M}])
(defn key-join
"join of map by key , value is distinct."
[map-list]
(let [keys (keys (first map-list))]
(into {} (for [k keys] [k (vec (set (map #(% k) map-list)))]))))
(defn group-reduce [key map-list]
(let [gdata (group-by key map-list)]
(into [] (for [[k m] gdata] (let [m (key-join m)](assoc m key ((key m) 0)))))))
user=> (group-reduce :id data)
[{:name [:Test2 :Test1], :OS [:W], :region [:EU :NA], :id 1} {:name [:test3 :test4], :OS [:L :M], :region [:AS], :id 2}]
You can use some combination of functions from clojure.set (if you change the outermost vector to set). Specifically clojure.set/index looks promising.
You can use the merge-with function as shown below in the example.
Firstly, we define some helper functions
(defn collect [& xs]
(apply vector (-> xs distinct sort)))
The collect function makes sure that the items in xs are unique and sorted and finally returns them in a vector.
(defn merge-keys [k xs]
(map #(apply merge-with collect %) (vals (group-by k xs))))
merge-keys first groups the hash-maps in xs by a primary key (in your case :id), takes each list of grouped items and merges the values of the keys using the collect function from above.
(def xs [{:id 1, :region :NA, :name :Test1, :OS :W}
{:id 1, :region :EU, :name :Test2, :OS :W}
{:id 2, :region :AS, :name :test3, :OS :L}
{:id 2, :region :AS, :name :test4, :OS :M}])
(merge-keys :id xs)
=> ({:id [1],
:region [:EU :NA],
:name [:Test1 :Test2],
:OS [:W]}
{:id [2],
:region [:AS],
:name [:test3 :test4],
:OS [:L :M]})
Note however that even the :id key now has vector associated with it. You can easily un-vector it by either introducing an if statement in collect which associates a single value with the key instead of a vector...
(defn collect [& xs]
(let [cs (apply vector (-> xs distinct sort))]
(if (= 1 (count cs)) (first cs) cs)))
...or take the result from merge-keys and do
(map #(update-in % [:id] first) result)
which will only un-vector the :id map entry