Clojure Map with # literal - clojure

What is the difference between
#:user{:profile{:name "Sally Clojurian"
:address {:city "Austin" :state "TX"}}}
and
{:user {:profile {:name "Sally Clojurian"
:address {:city "Austin" :state "TX"}}}}
I know that for the last on if I want to get name I can just do :
(get-in [:profile :name] map)
How would I get name for the first map?

The syntax of printing a map beginning with #:qualifer{ ... } is an abbreviated form of printing when all of the keys of the map are keywords with the same qualifier, or namespace. You can cause it to print without that abbreviation as shown below:
$ clojure
Clojure 1.10.1
user=> (def m1 #:user{:profile{:name "Sally Clojurian"
:address {:city "Austin" :state "TX"}}})
#'user/m1
user=> (pr m1)
#:user{:profile {:name "Sally Clojurian", :address {:city "Austin", :state "TX"}}}nil
user=> (doc *print-namespace-maps*)
-------------------------
clojure.core/*print-namespace-maps*
*print-namespace-maps* controls whether the printer will print
namespace map literal syntax. It defaults to false, but the REPL binds
to true.
nil
user=> (binding [*print-namespace-maps* false] (pr m1))
{:user/profile {:name "Sally Clojurian", :address {:city "Austin", :state "TX"}}}nil

If a map is def'ed as:
(def myMap #:user{:profile{:name "Sally Clojurian"
:address {:city "Austin" :state "TX"}}})
than to get to profile name you can do something like :
(get-in map [:user/profile :name])

Related

Recursive map query using specter

Is there a simple way in specter to collect all the structure satisfying a predicate ?
(./pull '[com.rpl/specter "1.0.0"])
(use 'com.rpl.specter)
(def data {:items [{:name "Washing machine"
:subparts [{:name "Ballast" :weight 1}
{:name "Hull" :weight 2}]}]})
(reduce + (select [(walker :weight) :weight] data))
;=> 3
(select [(walker :name) :name] data)
;=> ["Washing machine"]
How can we get all the value for :name, including ["Ballast" "Hull"] ?
Here's one way, using recursive-path and stay-then-continue to do the real work. (If you omit the final :name from the path argument to select, you'll get the full “item / part maps” rather than just the :name strings.)
(def data
{:items [{:name "Washing machine"
:subparts [{:name "Ballast" :weight 1}
{:name "Hull" :weight 2}]}]})
(specter/select
[(specter/recursive-path [] p
[(specter/walker :name) (specter/stay-then-continue [:subparts p])])
:name]
data)
;= ["Washing machine" "Ballast" "Hull"]
Update: In answer to the comment below, here's a version of the above the descends into arbitrary branches of the tree, as opposed to only descending into the :subparts branch of any given node, excluding :name (which is the key whose values in the tree we want to extract and should not itself be viewed as a branching off point):
(specter/select
[(specter/recursive-path [] p
[(specter/walker :name)
(specter/stay-then-continue
[(specter/filterer #(not= :name (key %)))
(specter/walker :name)
p])])
:name]
;; adding the key `:subparts` with the value [{:name "Foo"}]
;; to the "Washing machine" map to exercise the new descent strategy
(assoc-in data [:items 0 :subparts2] [{:name "Foo"}]))
;= ["Washing machine" "Ballast" "Hull" "Foo"]
The selected? selector can be used to collect structures for which another selector matches something within the structure
From the examples at https://github.com/nathanmarz/specter/wiki/List-of-Navigators#selected
=> (select [ALL (selected? [(must :a) even?])] [{:a 0} {:a 1} {:a 2} {:a 3}])
[{:a 0} {:a 2}]
I think you could iterate on map recursively using clojure.walk package. On each step, you may check the current value for a predicate and push it into an atom to collect the result.

How best to update this tree?

I've got the following tree:
{:start_date "2014-12-07"
:data {
:people [
{:id 1
:projects [{:id 1} {:id 2}]}
{:id 2
:projects [{:id 1} {:id 3}]}
]
}
}
I want to update the people and projects subtrees by adding a :name key-value pair.
Assuming I have these maps to perform the lookup:
(def people {1 "Susan" 2 "John")
(def projects {1 "Foo" 2 "Bar" 3 "Qux")
How could I update the original tree so that I end up with the following?
{:start_date "2014-12-07"
:data {
:people [
{:id 1
:name "Susan"
:projects [{:id 1 :name "Foo"} {:id 2 :name "Bar"}]}
{:id 2
:name "John"
:projects [{:id 1 :name "Foo"} {:id 3 :name "Qux"}]}
]
}
}
I've tried multiple combinations of assoc-in, update-in, get-in and map calls, but haven't been able to figure this out.
I have used letfn to break down the update into easier to understand units.
user> (def tree {:start_date "2014-12-07"
:data {:people [{:id 1
:projects [{:id 1} {:id 2}]}
{:id 2
:projects [{:id 1} {:id 3}]}]}})
#'user/tree
user> (def people {1 "Susan" 2 "John"})
#'user/people
user> (def projects {1 "Foo" 2 "Bar" 3 "Qux"})
#'user/projects
user>
(defn integrate-tree
[tree people projects]
;; letfn is like let, but it creates fn, and allows forward references
(letfn [(update-person [person]
;; -> is the "thread first" macro, the result of each expression
;; becomes the first arg to the next
(-> person
(assoc :name (people (:id person)))
(update-in [:projects] update-projects)))
(update-projects [all-projects]
(mapv
#(assoc % :name (projects (:id %)))
all-projects))]
(update-in tree [:data :people] #(mapv update-person %))))
#'user/integrate-tree
user> (pprint (integrate-tree tree people projects))
{:start_date "2014-12-07",
:data
{:people
[{:projects [{:name "Foo", :id 1} {:name "Bar", :id 2}],
:name "Susan",
:id 1}
{:projects [{:name "Foo", :id 1} {:name "Qux", :id 3}],
:name "John",
:id 2}]}}
nil
Not sure if entirely the best approach:
(defn update-names
[tree people projects]
(reduce
(fn [t [id name]]
(let [person-idx (ffirst (filter #(= (:id (second %)) id)
(map-indexed vector (:people (:data t)))))
temp (assoc-in t [:data :people person-idx :name] name)]
(reduce
(fn [t [id name]]
(let [project-idx (ffirst (filter #(= (:id (second %)) id)
(map-indexed vector (get-in t [:data :people person-idx :projects]))))]
(if project-idx
(assoc-in t [:data :people person-idx :projects project-idx :name] name)
t)))
temp
projects)))
tree
people))
Just call it with your parameters:
(clojure.pprint/pprint (update-names tree people projects))
{:start_date "2014-12-07",
:data
{:people
[{:projects [{:name "Foo", :id 1} {:name "Bar", :id 2}],
:name "Susan",
:id 1}
{:projects [{:name "Foo", :id 1} {:name "Qux", :id 3}],
:name "John",
:id 2}]}}
With nested reduces
Reduce over the people to update corresponding names
For each people, reduce over projects to update corresponding names
The noisesmith solution looks better since doesn't need to find person index or project index for each step.
Naturally you tried to assoc-in or update-in but the problem lies in your tree structure, since the key path to update John name is [:data :people 1 :name], so your assoc-in code would look like:
(assoc-in tree [:data :people 1 :name] "John")
But you need to find John's index in the people vector before you can update it, same things happens with projects inside.

Why can't clojure.repl/print-doc binding be changed in Clojure REPL?

This works as expected:
java -jar clojure-1.4.0.jar -e "(do (require 'clojure.repl) (.setDynamic #'clojure.repl/print-doc) (with-bindings {#'clojure.repl/print-doc str} (eval '(clojure.repl/doc println))))"
Output:
"{:ns #<Namespace clojure.core>, :name println, :arglists ([& more]), :added \"1.0\", :static true, :doc \"Same as print followed by (newline)\", :line 3325, :file \"clojure/core.clj\"}"
But the same does not work in the REPL:
java -jar clojure-1.4.0.jar -e "(do (require 'clojure.repl) (.setDynamic #'clojure.repl/print-doc) (clojure.main/repl :init (fn [] {#'clojure.repl/print-doc str}))))"
Output of (doc println):
user=> (doc println)
-------------------------
clojure.core/println
([& more])
Same as print followed by (newline)
nil
user=>
I don't know what I'm doing wrong.
Found the answer after diving into the counterclockwise and nrepl code:
java -jar clojure-1.4.0.jar -e "(do (require 'clojure.repl) (.setDynamic #'clojure.repl/print-doc) (with-bindings {#'clojure.repl/print-doc str} (clojure.main/repl)))))"
The output is the same as above:
"{:ns #<Namespace clojure.core>, :name println, :arglists ([& more]), :added \"1.0\", :static true, :doc \"Same as print followed by (newline)\", :line 3325, :file \"clojure/core.clj\"}"
The trick is to use with-bindings before calling repl:
(with-bindings {#'clojure.repl/print-doc str}
(repl))

NOT - EXISTS / NOT - IN type query in Clojure

I have 2 data structures like the ones below
(ns test)
(def l
[{:name "Sean" :age 27}
{:name "Ross" :age 27}
{:name "Brian" :age 22}])
(def r
[{:owner "Sean" :item "Beer" }
{:owner "Sean" :item "Pizza"}
{:owner "Ross" :item "Computer"}
{:owner "Matt" :item "Bike"}])
I want to have get persons who dont own any item . (Brian in this case so [ {:name "Brian" :age 22}]
If this was SQL I would do left outer join or not exists but I not sure how to do this in clojure in more performant way.
While Chuck's solution is certainly the most sensible one, I find it interesting that it is possible to write a solution in terms of relational algebraic operators using clojure.set:
(require '[clojure.set :as set])
(set/difference (set l)
(set/project (set/join r l {:owner :name})
#{:name :age}))
; => #{{:name "Brian", :age 22}}
You basically want to do a filter on l, but negative. We could just not the condition, but the remove function already does this for us. So something like:
(let [owner-names (set (map :owner r))]
(remove #(owner-names (% :name)) l))
(I think it reads more nicely with the set, but if you want to avoid allocating the set, you can just do (remove (fn [person] (some #(= (% :owner) (person :name)) r)) l).)

Construct a List of Maps

First of all I'm a Clojure beginner and sry for my bad English.
Lets say you have a function that is suppossed to return a List of Maps containing various Information (in this example Systeminformation).
I've come up with the following example but it just seems wrong and overcomplicated to me.
My Problem is that 'for [disk (File/listRoots)' already returns a list and I have to merge the 2 Lists in order to get the desired Output, for which must exist a better Solution.
I hope that somebody of you can enlighten me of how to do that in a more 'Clojure way'.
(import
[java.lang Runtime System]
[java.io File])
(defn get-sysinfo []
(let [basic-info (list
{:name "Processor Count:", :value (. (Runtime/getRuntime) availableProcessors)}
{:name "OS Name:", :value (System/getProperty "os.name")}
{:name "OS Arch:", :value (System/getProperty "os.arch")}
{:name "User Name:", :value (System/getProperty "user.name")}
{:name "Java Version:", :value (System/getProperty "java.version")})]
(concat basic-info (for [disk (File/listRoots)]
{:name (str "Disk " (. disk getAbsolutePath)), :value (str "Free Space " (float (/ (. disk getFreeSpace) (* 1024 1024 1024))) " GB")}))))
Thanks in advance
Markus
You can also use the little list* helper. It creates a list from the given elements. The last of which is taken as a tail list. So the "merging" happens implicitly.
(defn get-sysinfo
[]
(list*
{:name "Processor Count:" :value (.availableProcessors (Runtime/getRuntime))}
{:name "OS Name:", :value (System/getProperty "os.name")}
{:name "OS Arch:", :value (System/getProperty "os.arch")}
{:name "User Name:", :value (System/getProperty "user.name")}
{:name "Java Version:", :value (System/getProperty "java.version")}
(for [disk (File/listRoots)]
{:name (str "Disk " (.getAbsolutePath disk))
:value (str "Free Space " (float (/ (.getFreeSpace disk)
(* 1024 1024 1024)))
" GB")})))
However, maybe you really want to return a map?
(defn get-sysinfo-map
[]
(into {"Processor Count" (.availableProcessors (Runtime/getRuntime))
"OS Name" (System/getProperty "os.name")
"OS Arch" (System/getProperty "os.arch")
"User Name" (System/getProperty "user.name")
"Java Version" (System/getProperty "java.version")}
(for [disk (File/listRoots)]
[(str "Disk " (.getAbsolutePath disk))
(float (/ (.getFreeSpace disk) (* 1024 1024 1024)))])))
Since you're using (for ...) to transform each element, map seems a more logical choice. You can't really avoid merging two lists since one is "fixed" and the other is generated later from the file listings.
It's also more idiomatic to use (.methodName object) instead of (. object methodName) - though that's purely a style issue.
I'd do it something like:
(defn get-sysinfo []
(concat (list
{:name "Processor Count:", :value (. (Runtime/getRuntime) availableProcessors)}
{:name "OS Name:", :value (System/getProperty "os.name")}
{:name "OS Arch:", :value (System/getProperty "os.arch")}
{:name "User Name:", :value (System/getProperty "user.name")}
{:name "Java Version:", :value (System/getProperty "java.version")})
(map #(hash-map :name (str "Disk " (.getAbsolutePath %)),
:value (str "Free Space "
(float (/ (.getFreeSpace %) (* 1024 1024 1024)))
" GB"))
(File/listRoots))))