I am new to Clojure and Reagent. Kindly tell how to print the variable first inside the atom variable contacts?
(def app-state
(r/atom
{:contacts [{:first "Ben" :last "Lem" :middle "Ab"}]}))
First of all: the reagent tutorial is a really good place to start. It even gives you examples to solve exactly this problem.
Since reagents atom can be treated just as a regular Clojurescript atom, you can use all your normal sequence operations. Keep in mind that in order to access the current value, you have to dereference the atom via #.If you really just want to access the first :first in your atom:
(:first (first (:contacts #app-state))) or (get (first (get #app-state :contacts)) :first)
Or, if you think it's more readable
(-> #app-state
:contacts
first
:first)
I guess what you might want to do is define a few functions to make the access more easy such as:
(defn get-contacts!
"Returns the current vector of contacts stored in the app-state."
[]
(:contacts #app-state))
(defn get-first-names!
"Return a vector of all first names in the current list of contacts in the
app-state."
[]
(mapv :first (get-contacts!)))
Please keep in mind that in reagent (and in general really) you might want to dereference that atom as fiew times as possible, so look for a good place to dereference it and just use regular functions that operate on a simple sequence instead of an atom.
Still, I would really suggest you go read the aforementioned reagent tutorial.
Here is a concise way to access the value that you are looking for using Clojure's (get-in m ks) function:
(get-in #app-state [:contacts 0 :first])
Just as an extra, you may see this often written as
(->> #app-state
:contacts
(mapv :first)
first
and it's useful to understand what's going on here.
->> is a macro called thread-last which will re-write the code above to be
(first (mapv :first (:contacts #app-state)))
Thread last is a bit weird at first but it makes the code more readable when lots of things are going on. I suggest that on top of the reagent tutorial mentioned in the other comments, you read this.
#app-state will give you whatever is inside the r/atom and (:first (first (:contacts #app-state))) will return the first element and (println (:first (first (:contacts #app-state)))) will print output to the browser console (so you need to have the developer tools console open to see it).
Note that for println to output to the browser developer tools console you need to have this line in your code:
(enable-console-print!)
Related
how can I print list elements using let keyword in clojure language?
(defn build-headline-keywords-item [es-client conf common-item headline]
(let [headline headline]
(println (:headline))
(es/upsert es-client conf (merge common-item{:source ["headline"]
:type ["headline"]
:keywords headline}))))
Alan's answer covers the important parts of how to use Clojure to do this.
I'll take a different path and ask about whether this is an issue with your environment. Are you working in a REPL? Or running things in some other way? What is the actual thing you are running or evaluating?
Maybe you are using an editor where the console output is going someplace you're not seeing? For example, in Emacs the console output may go to a buffer that is not visible.
If I understand you correctly, it should look more like this:
(defn build-headline-keywords-item
[es-client conf common-item headline]
(println headline)
... )
UPDATE:
If you are still having trouble, make a test file and remove bits one-by-one until you get something that works. Then add the bits back in one at a time to build up the whole problem. It will work:
Code:
(defn build-headline [a b c headline]
(println headline))
(build-headline 1 2 3 "Space Aliens Invade!")
Result:
> lein run
Space Aliens Invade!
Please also see the online book Clojure for the Brave & True
for more information.
I'm writing a mini-shell thing for fun, and I'm trying to define a map of commands where one of the commands is help. help should print all available commands, and I (try to) do this by looping through the keys of the commands map, but since help is part of it, no matter which order I define them in, I always get a Use of undeclared variable-warning.
How do I solve this?
(def commands {:help help})
(defn help []
(echo! "The available commands are:")
(doseq [available-command (keys commands)]
(echo! (name available-command))))
Add
(declare help)
at the beginning.
I tried to follow the documentation for clojure.instant/read-instant-timestamp, which reads:
clojure.instant/read-instant-timestamp
To read an instant as a java.sql.Timestamp, bind *data-readers* to a
map with this var as the value for the 'inst key. Timestamp preserves
fractional seconds with nanosecond precision. The timezone offset will
be used to convert into UTC.`
The following result was unexpected:
(do
(require '[clojure.edn :as edn])
(require '[clojure.instant :refer [read-instant-timestamp]])
(let [instant "#inst \"1970-01-01T00:00:09.999-00:00\""
reader-map {'inst #'read-instant-timestamp}]
;; This binding is not appearing to do anything.
(binding [*data-readers* reader-map]
;; prints java.util.Date -- unexpected
(->> instant edn/read-string class println)
;; prints java.sql.Timestamp -- as desired
(->> instant (edn/read-string {:readers reader-map}) class println))))
How can I use the *data-readers* binding? Clojure version 1.5.1.
clojure.edn functions by default only use data readers stored in clojure.core/default-data-readers which, as of Clojure 1.5.1, provides readers for instant and UUID literals. If you want to use custom readers, you can do that by passing in a :readers option; in particular, you can pass in *data-readers*. This is documented in the docstring for clojure.edn/read (the docstring for clojure.edn/read-string refers to that for read).
Here are some examples:
(require '[clojure.edn :as edn])
;; instant literals work out of the box:
(edn/read-string "#inst \"2013-06-08T01:00:00Z\"")
;= #inst "2013-06-08T01:00:00.000-00:00"
;; custom literals can be passed in in the opts map:
(edn/read-string {:readers {'foo identity}} "#foo :asdf")
;= :asdf
;; use current binding of *data-readers*
(edn/read-string {:readers *data-readers*} "...")
(The following section added in response to comments made by Richard Möhn in this GitHub issue's comment thread. The immediate question there is whether it is appropriate for a reader function to call eval on the data passed in. I am not affiliated with the project in question; please see the ticket for details, as well as Richard's comments on the present answer.)
It is worth adding that *data-readers* is implicitly populated from any data_readers.{clj,cljc} files that Clojure finds at the root of the classpath at startup time. This can be convenient (it allows one to use custom tagged literals in Clojure source code and at the REPL), but it does mean that new data readers may appear in there with a change to a single dependency. Using an explicitly constructed reader map with clojure.edn is a simple way to avoid surprises (which could be particularly nasty when dealing with untrusted input).
(Note that the implicit loading process does not result in any code being loaded immediately, or even when a tag mentioned in *data-readers* is first encountered; the process which populates *data-readers* creates empty namespaces with unbound Vars as placeholders, and to actually use those readers one still has to require the relevant namespaces in user code.)
The *data-readers* dynamic var seems to apply to the read-string and read functions from clojure.core only.
(require '[clojure.instant :refer [read-instant-timestamp]])
(let [instant "#inst \"1970-01-01T00:00:09.999-00:00\""
reader-map {'inst #'read-instant-timestamp}]
;; This will read a java.util.Date
(->> instant read-string class println)
;; This will read a java.sql.Timestamp
(binding [*data-readers* reader-map]
(->> instant read-string class println)))
Browsing through the source code for clojure.edn reader here, I couldn't find anything that would indicate that the same *data-readers* var is used at all there.
clojure.core's functions read and read-string use LispReader (which uses the value from *data-readers*), while the functions from clojure.edn use the EdnReader.
This edn library is relatively new in Clojure so that might be the reason why the documentation string is not specific enough regarding edn vs. core reader, which can cause this kind of confusion.
Hope it helps.
I apologize for a second question on the same topic, but I'm confused. Is there a Clojure module that follows lxml, even loosely, or how-to documentation on how to walk through an XML file using Clojure?
In Python, I can open an XML file using the lxml module; parse my way through the data; look for tags like <DeviceID>, <TamperName>, <SecheduledDateTime>, and then peform an action based on the value of one of those tags.
In Clojure, I have been given excellent answers on how to parse using data.xml and then further reduce the data.xml-parsed information by pulling out the :content tag's vals and putting the information in a tree-seq.
However, even that resultant data has other map tags embedded, which obviously do not respond to keys and vals functions.
I could take this data and use regular expression searches, but I feel I'm missing something much simpler.
The data right out of data.xml/parse (calling ret-xml-data) looks like this, using various (first parsed-xml) and other commands at the REPL:
[:tag :TamperExport]
[:attrs {}]
:content
#clojure.data.xml.Element{:tag :Header, :attrs {}, :content
(#clojure.data.xml.Element{:tag :ExportType, :attrs {},
:content ("Tamper Export")}
#clojure.data.xml.Element{:tag :CurrentDateTime,
:attrs {},
:content ("2012-06-26T15:40:22.063")} :attrs {},
:content ("{06643D9B-DCD3-459B-86A6-D21B20A03576}")}
Here is the Clojure code I have so far:
(defn ret-xml-data
"Returns a map of the supplied xml file, as parsed by data.xml/parse."
[xml-fnam]
(let [input-xml (try
(java.io.FileInputStream. xml-fnam)
(catch Exception e))]
(if-not (nil? input-xml)
(xmld/parse input-xml)
nil)))
(defn gen-xml-content-tree
"Returns a tree-seq with :content extracted."
[parsed-xml]
(map :content (first (tree-seq :content :content (:content parsed-xml)))))
I think I may have found a repeatable pattern to the data that will allow me to parse this without creating a hodgepodge:
xml-lib.core=> (first (second cl1))
#clojure.data.xml.Element{:tag :DeviceId, :attrs {}, :content ("80580608")}
xml-lib.core=> (keys (first (second cl1)))
(:tag :attrs :content)
xml-lib.core=> (vals (first (second cl1)))
(:DeviceId {} ("80580608"))
Thank you as always.
Edit:
Add some more testing.
The resulting data, if I ran through the tree-seq structure using a function like doseq, could probably now be parsed with actions taken.
First, it's hard to tell exactly what you're trying to do. When working on a programming problem, it helps both you and others helping you to have a "small case" you can present and solve before working towards a larger one.
From what it sounds like, you're trying to pull the content out of certain elements and perform actions based on that content.
I put together a small XML file with some simple content to try things out on:
<root>
<someele>
<item1>data</item1>
<deeper>
<item2>else</item2>
</deeper>
</someele>
</root>
I designed it to be what I think is representative of some of the core challenges with the problem at hand - in particular, being able to do stuff at arbitrary levels of nesting in the XML.
Looking at the wonderful Clojure Cheatsheet, I found xml-seq, and tried running it on the clojure.data.xml/parsed xml. The sequence went through each of the elements and then their children, making it easy to iterate over the XML.
To pick out and work with particular items in a sequence, I like using for loops with :when. :when makes it easy to just enter the body of the loop when certain conditions are true. I also make use of the "set as a function" semantics, which checks to see if something is in the set.
(for [ele (xml-seq (load-xml))
:when (#{:item1 :item2} (:tag ele))]
[(:tag ele) (first (:content ele))])
This gets back a sequence of ([:item1 "data"] [:item2 "else"]) that can then easily be acted on in other ways.
One of the key things to try and keep in mind about Clojure is that you tend to not need any special API to do stuff - the core language makes it easy to do most, if not all, that you need to do. Records (which are what you see being returned) are also maps for example, so assoc, dissoc, and so on work on them, and it's how they are intended to be worked with.
If this doesn't help you get to what you need, then could you provide a small sample output and a sample result that you want?
The closest Clojure library I can think of for lxml after a (very) brief look is called Enlive. It's listed as an HTML templating tool, but I'm pretty sure the techniques it uses for picking out HTML elements can also be applied to XML.
For example, the prxml function prints XML to *out*. I would like to instead capture this output as a String. Here is the typical usage from a REPL:
user> (prxml [:p "Test"])
<p>Test</p>nil
I'd instead like to do:
(def xml (capture-out (prxml [:p "Test"])))
I made up the capture-out function, but I suspect something like it exists, only I'm having trouble finding it in the API or mailing list.
I just discovered the with-out-str from this great blog post detailing XML processing in Clojure.
So the correct implementation of my example is:
(def xml (with-out-str (prxml [:p "Test"])))
More generally, if you look at the source for with-out-str you can see how to dynamically bind *out* to any stream using binding. This works for dynamically setting the value of any existing var.