File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Clojure and the fly likes Taking the Next Step Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Languages » Clojure
Bookmark "Taking the Next Step" Watch "Taking the Next Step" New topic
Author

Taking the Next Step

Dan King
Ranch Hand

Joined: Mar 18, 2009
Posts: 84
At this point, I'm comfortable with most Clojure concepts and syntax. To further my understanding I want to put together code that can be used for simple (file) data analysis. However, I've been unable to find any resources that explain how to read, write, and perform regex either 'natively' or via java. Can anyone suggest resources -- i.e. books, websites, examples -- that can help? Thanks.
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

I think you mean that you are comfortable with Clojure lack of syntax
Clojure FAQ page contains a couple of books but you can have a taste of RegEx in Clojure right now:
http://clojure.org/other_functions

On general those functions are what Clojure has for RegEx:
#"pattern" re-pattern re-matcher re-find re-matches re-groups re-seq

If what is blocking you now is how to do RegExp in Clojure, then don't get any book, just check the previous functions.
Hope this helps.
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 231
    
    3

Dan,

If you can provide a bit more background on what you're thinking of trying, we can give you better pointers. Is it just RegEx? If so, John's answer should help. Is is also how to read lines from a file? Jump on clojuredocs.org and search for reader, with-open, line-seq and see if that helps.

For books: Clojure Programming (O'Reilly Rough Cuts) has a lot of practical examples, as does Clojure in Action (Manning). Joy of Clojure (Manning) has some specific file reading examples but is more about the why / idiom of Clojure.

HTH,
Sean
Dan King
Ranch Hand

Joined: Mar 18, 2009
Posts: 84
@John: Thanks for the link on regex and the list of regex functions; the information was very helpful.

Sean Corfield wrote:Dan,
If you can provide a bit more background on what you're thinking of trying, we can give you better pointers. Is it just RegEx? If so, John's answer should help. Is is also how to read lines from a file? Jump on clojuredocs.org and search for reader, with-open, line-seq and see if that helps.

Firstly, thanks for the link to clojuredocs.org and the 'reader', 'with-open', 'line-seq' search terms.

No, it's not just RegEx, but rather reading from a file, regex, and writing to a file.

I think it's best, as you suggested, for me to elaborate on what I'm trying to do.

I have a log file, in xml format, that I want to parse for 'author' data. I want to then write this parsed data to another file. For example, assuming the following xml log file:



I want to generate the following file (the email domain is the same for all authors):


Thanks for the help.
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

I learnt to break my code into functions when coding in Clojure, after all functions are the building blocks in Clojure.
That is true for many language but it is extremely true for languages like Clojure and Haskell.
It is tempting to code the whole logic in one function like the imperative style, but I think Clojure will try to block you if you tried to do so.
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

Have a look at this thread: http://www.coderanch.com/t/544295/clojure/Incubator/Analyzing-massive-data-Clojure
It might be useful.
Dan King
Ranch Hand

Joined: Mar 18, 2009
Posts: 84
John Todd wrote:Have a look at this thread: http://www.coderanch.com/t/544295/clojure/Incubator/Analyzing-massive-data-Clojure
It might be useful.


Thanks for the link. There's a quite a lot to absorb; certainly seems useful.

As an aside, below is what I hacked together to solve the problem:

Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 231
    
    3

Since Clojure has some powerful functions to process XML, you might want to try something like this:

This probably looks a lot more complicated but...

The first line parses the XML and turns it into a walked sequence of nodes, then filters out just those nodes that have the tag author, then the map returns the first element of the content (the author name). So authors is the sequence ("dking" "jdoe").

The second line maps each author name to the string author = author@emailaddress.com, then interposes a newline string between each element, and finally apply str (sequence of strings) turns it into a single string which spit then writes to a file.

I'd definitely break that up into multiple functions so that they're easier to reuse but it should give you an idea of the power of sequences.

Here's the code broken up some more:
Dan King
Ranch Hand

Joined: Mar 18, 2009
Posts: 84
Sean Corfield wrote:Since Clojure has some powerful functions to process XML, you might want to try something like this...



Thanks for the code example; it was very helpful in understanding how to parse xml data. Definitely seems clojure's sweet-spot is data analysis.

If and when you have the time, could you share an example demonstrating the creation of xml data with clojure?

Thanks.

Best,
Dan

Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 231
    
    3

Dan King wrote:If and when you have the time, could you share an example demonstrating the creation of xml data with clojure?

If you build a nested map that represents the structure of the XML, you can use (clojure.xml/emit my-map) to write the XML to *out* (stdout) and therefore wrap it in (with-out-str ...) to get back a string representation, instead of writing to *out* - or use (binding [*out* (writer "myoutput.xml")] ...)

For example:

You'll get the following in myoutput.xml:
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

Clojure continues to impress me.
I have a large XML file to parse and analyze and insert the result into a data base.
Yes the task isn't difficult but I'm really amazed by the elegant, succinct and dense Clojure code. Mission done by only 35 ~ 40 line of code !
Dan King
Ranch Hand

Joined: Mar 18, 2009
Posts: 84
@Sean: Thanks for the code examples, they were very helpful.

John Todd wrote:Clojure continues to impress me.

Agreed. It's a vicious cycle: the more I learn about clojure, the more I like it...
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

Please inform us once you got the symptom: "I don't want to code anything without Clojure"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Taking the Next Step
 
Similar Threads
xlink question
IllegalStateException error
Could not reserve enough space for object heap
passed with a 77%
WA #1.....word association