EzDevInfo.com

clojure-contrib

NOTE - the contrib libraries have moved to individual repos under Clojure: Clojure · GitHub github is where people build software. more than 11 million people use github to discover, fork, and contribute to over 29 million projects.

How do I extend clojure.contribs json writer to serialize other classes

I need to create JSON objects from clojure maps that store things like clojure vars. The base implementation throws this kind of error when it sees them:

java.lang.Exception: Don't know how to write JSON of class clojure.lang.Var

Can anybody point me to sample code on how to extend the capabilities of the JSON writer?

Thanks.


Source: (StackOverflow)

Find where clojure-contrib libs have moved to

Given how with future version of clojure are discouraging use of clojure-contrib as a single item - how do you find where things have moved to.

For example - this utility: http://richhickey.github.com/clojure-contrib/javadoc.browse-api.html#clojure.contrib.javadoc.browse/open-url-in-browser handy - but where has it moved to under the new scheme?


Source: (StackOverflow)

Advertisements

Tips for Html parsing and web driving with clojure?

I want to automate filling in data on a website using clojure.

For this I want to query elements of webpages and create http requests. I have been looking at using HttpUnit and contrib.clojure.zip-filter.xml. So far neither approach feels right.

Are there alternative libraries to aid with this task?

thanks


Source: (StackOverflow)

How to find import-static in clojure 1.3

I am reading "Programming Clojure", and this book is based on clojure 1.1.

I want to use the latest stable version, 1.3, but it does not have old clojure-contrib.

My problem is that I cannot find import-static in clojure 1.3. I could copy the macro definition from old clojure-contrib and run.

Is there import-static or some substitute in clojure 1.3? Are there knowhows to find out such libraries and functions in old clojure-contrib for 1.3?


Source: (StackOverflow)

Why does my Clojure import fail?

I'm running Clojure 1.3 with contrib 1.1 in IntelliJ. My program consists of a single line

(use 'clojure.contrib.prxml)

I get the following error upon running

Exception in thread "main" java.lang.RuntimeException: java.lang.NoSuchMethodError: clojure.lang.RestFn.<init>(I)V

Source: (StackOverflow)

Building a Clojure app with a command-line interface?

I just started w/ Clojure (coming from Ruby) and I would like to build an small app with a command-line interface. How do I handle input/output to a CL?

I noticed that there is a clojure.contrib.command-line, but documentation is slim.

http://github.com/richhickey/clojure-contrib/blob/ffa868411cda6c617105b52b4f6f9e0f37ee8c24/src/clojure/contrib/command%5Fline.clj


Source: (StackOverflow)

Clojure XML Parsing

I can not find any info on how to parse xml documents and access elements.

I have found two ways to parse the xml document

(clojure.zip/xml-zip (clojure.xml/parse file))

and

(parse-seq file)

but i can seem to find any info on how to process the resulting structure?

Source file's refers to zip-query.clj on how to query the result but that seems to missing too.


Source: (StackOverflow)

How does ClojureQL compare to clojure.contrib.sql?

It looks like each one covers the basic cases like selecting certain columns and filtering by predicate pretty well, but I'm wondering how each compares for more advanced cases. Is it easier to express complex queries in one vis-à-vis the other? Is one library missing any functionality that the other covers?


Source: (StackOverflow)

How do I dynamically find metadata for a Clojure function?

Say I have the following code:

(defn ^{:graph-title "Function 1"} func-1
  [x]
  (do-something-with x))

(defn get-graph-title 
  [func]
  (str
    ((meta func) :graph-title))) 

I expect this to return "Function 1", but it returns nil. I think this stems from the following difference, which I don't totally comprehend:

(meta func-1)
=>  {:ns some-ns-info, :name func-1}
(meta #'func-1)
=>  {:ns some-ns-info, :name func-1, :graph-title "Function 1"}

Can someone explain this to me?


Source: (StackOverflow)

Create a list from a string in Clojure

I'm looking to create a list of characters using a string as my source. I did a bit of googling and came up with nothing so then I wrote a function that did what I wanted:

(defn list-from-string [char-string]
  (loop [source char-string result ()]
    (def result-char (string/take 1 source))
    (cond
     (empty? source) result
     :else (recur (string/drop 1 source) (conj result result-char)))))

But looking at this makes me feel like I must be missing a trick. So:

  1. Is there a core or contrib function that does this for me? Surely I'm just being dumb right?
  2. If not is there a way to improve this code?
  3. Would the same thing work for numbers too?

Thanks in advance. Robert


Source: (StackOverflow)

Use for the identity monad in Clojure

I've been reading an excellent introduction to monads for Clojure programmers. The article illustrates that the Identity monad is functionally equivalent to Clojure's let and that the Sequence/List monad is equivalent to for.

When the article gets to monad transformers, it shows an example combining the Maybe and Sequence monads. Ok, so one reason for using a Sequence monad instead of a for is that I can transform it. However, transforming an Identity monad doesn't make sense to me - wouldn't that always be equivalent to just building up whatever the transforming monad is? For example, if I transformed Maybe with Identity - doesn't that just give me a Maybe, which would have been easier to declare directly?

Can someone clear up whether there's a practical use in Clojure for choosing an Identity monad over a let (perhaps I'm not thinking all the way through the implications of transformers?), or is it just there for theoretical completeness?


Source: (StackOverflow)

Insertions into Zipper trees on XML files in Clojure

I'm confused as how to idiomatically change a xml tree accessed through clojure.contrib's zip-filter.xml. Should be trying to do this at all, or is there a better way?

Say that I have some dummy xml file "itemdb.xml" like this:

<itemlist> 
  <item id="1">
    <name>John</name>
    <desc>Works near here.</desc>
  </item>
  <item id="2">
    <name>Sally</name>
    <desc>Owner of pet store.</desc>
  </item>
</itemlist>

And I have some code:

(require '[clojure.zip :as zip]
  '[clojure.contrib.duck-streams :as ds]
  '[clojure.contrib.lazy-xml :as lxml]
  '[clojure.contrib.zip-filter.xml :as zf]) 

(def db (ref (zip/xml-zip (lxml/parse-trim (java.io.File. "itemdb.xml")))))

;; Test that we can traverse and parse.
(doall (map #(print (format "%10s: %s\n"
       (apply str (zf/xml-> % :name zf/text))
       (apply str (zf/xml-> % :desc zf/text))))
     (zf/xml-> @db :item)))

;; I assume something like this is needed to make the xml tags
(defn create-item [name desc]
  {:tag :item
   :attrs {:id "3"}
   :contents
   (list {:tag :name :attrs {} :contents (list name)}
         {:tag :desc :attrs {} :contents (list desc)})})

(def fred-item (create-item "Fred" "Green-haired astrophysicist."))

;; This disturbs the structure somehow
(defn append-item [xmldb item]
  (zip/insert-right (-> xmldb zip/down zip/rightmost) item))

;; I want to do something more like this
(defn append-item2 [xmldb item]
  (zip/insert-right (zip/rightmost (zf/xml-> xmldb :item)) item))

(dosync (alter db append-item2 fred-item))

;; Save this simple xml file with some added stuff.
(ds/spit "appended-itemdb.xml"
    (with-out-str (lxml/emit (zip/root @db) :pad true)))

I am unclear about how to use the clojure.zip functions appropriately in this case, and how that interacts with zip-filter.

If you spot anything particularly weird in this small example, please point it out.


Source: (StackOverflow)

Equivalent to clojure.contrib's show?

There used to be this useful utility called show in clojure.contrib. Now, that it's deprecated, is there an equivalent to it?

Thanks!


Source: (StackOverflow)

IllegalStateException Compiling Clojure-Contrib

I am trying to compile my own version of clojure-contrib with Maven I get the following exception:

Exception in thread "main" java.lang.IllegalStateException: Can't dynamically bind non-dynamic var: clojure.contrib.pprint/*format-str*, compiling:(dispatch.clj:90)

I am also using the following command to compile it:

mvn package -Dclojure.jar=/usr/local/share/jars/clojure.jar

clojure.jar is link to the actual jar (that is on the same directory) because I am using a version that I compiled from the Git repository.

Any Ideas?

Thanks in advance for your comments! =)


Source: (StackOverflow)

How to call Clojure Macros from Java?

Is there anyway to call Clojure macros from Java?

Here is what I am trying to do:

RT.var("clojure.core", "require").invoke(Symbol.create("clojure.contrib.prxml"));
Var prxml = RT.var("clojure.contrib.prxml", "prxml");
Var withOutStr = RT.var("clojure.core", "with-out-str");
String stringXML = (String) withOutStr.invoke((prxml.invoke("[:Name \"Bob\"]")));

prxml writes to *out* by default which is why I need to wrap it with the macro with-out-str which returns the string.

I am getting this error:

 [java] java.lang.IllegalArgumentException: Wrong number of args (1) passed to: core$with-out-str
 [java]     at clojure.lang.AFn.throwArity(AFn.java:437)
 [java]     at clojure.lang.RestFn.invoke(RestFn.java:412)
 [java]     at clojure.lang.Var.invoke(Var.java:365)
 [java]     at JavaClojure.xml.main(Unknown Source)

Source: (StackOverflow)