<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alexandre Martins &#187; Clojure</title>
	<atom:link href="http://blog.m.artins.net/tag/clojure/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.m.artins.net</link>
	<description>On Agile Software Development</description>
	<lastBuildDate>Thu, 12 Aug 2010 20:09:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Clojure: Integrating With Java</title>
		<link>http://blog.m.artins.net/clojure-integrating-with-java/</link>
		<comments>http://blog.m.artins.net/clojure-integrating-with-java/#comments</comments>
		<pubDate>Thu, 01 Jan 2009 06:44:04 +0000</pubDate>
		<dc:creator>Alexandre Martins</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.m.artins.net/?p=145</guid>
		<description><![CDATA[Currently I am learning Clojure. It is a functional programming language, but not a pure one, since you can both write code that share state (mutable) and also ones that doesn't. Why Clojure? The main reason why I chose Clojure is its easy interoperability with Java, still one of the most used languages, bringing to [...]]]></description>
			<content:encoded><![CDATA[<p>Currently I am learning <a href="http://clojure.org/">Clojure</a>. It is a functional programming language, but not a <a href="http://en.wikipedia.org/wiki/Purely_functional">pure one</a>, since you can both write code that share state (mutable) and also ones that doesn't.</p>
<h2>Why Clojure?</h2>
<p>The main reason why I chose Clojure is its easy interoperability with <a href="http://java.com">Java</a>, still one of the most used languages, bringing to it the power of Lisp. It's fast, since the code is compiled, and it supplements some of Java's weakness, such as the Collections framework and concurrent programming. It is pretty straightforward to write concurrent programs, everything is automatic, no manual lock management!</p>
<h2>Integrating With Java</h2>
<h3>Importing classes</h3>
<p>A single class:</p>
<pre style="border: solid gray 1px; width: 60%; padding: 5px;">
(import java.util.List)
</pre>
</p>
<p>Multiple classes from the same package:</p>
<pre style="border: solid gray 1px; width: 60%; padding: 5px;">
(import '(java.util List Set))
</pre>
</p>
<h3>Creating instances</h3>
<p>Using Java's <span style="font-family: courier;">new</span> keyword:</p>
<pre style="border: solid gray 1px; width: 60%; padding: 5px;">
(new java.util.ArrayList)
(new ArrayList) ; after importing
</pre>
</p>
<p>Assigning a new  <span style="font-family: courier;">List</span> to a Clojure variable:</p>
<pre style="border: solid gray 1px; width: 60%; padding: 5px;">
(def list (new java.util.List))
-> #'user/list
</pre>
</p>
<p>Syntactic Sugar:</p>
<pre style="border: solid gray 1px; width: 60%; padding: 5px;">
(ArrayList.)
</pre>
</p>
<h3>Accessing ﬁelds</h3>
<p>Static fields:</p>
<pre style="border: solid gray 1px; width: 60%; padding: 5px;">
(. Math PI)
</pre>
</p>
<p>Syntactic Sugar:</p>
<pre style="border: solid gray 1px; width: 60%; padding: 5px;">
Math/PI
</pre>
</p>
<h3>Invoking methods</h3>
<p>
<h4>Static Methods</h4>
<pre style="border: solid gray 1px; width: 60%; padding: 5px;">
(.currentTimeMillis System)
</pre>
</p>
<p>Syntactic Sugar:</p>
<pre style="border: solid gray 1px; width: 60%; padding: 5px;">
(System/currentTimeMillis)
</pre>
</p>
<p>
<h4>Non-static Methods</h4>
<pre style="border: solid gray 1px; width: 60%; padding: 5px;">
(. list size)
(. list get 0) ; returns the object stored at index 0
</pre>
</p>
<p>Syntactic Sugar:</p>
<pre style="border: solid gray 1px; width: 60%; padding: 5px;">
(.size list)
</pre>
</p>
<h3>Mixing Them All</h3>
<p>Clojure provides a macro called  <span style="font-family: courier;">memfn</span> that makes possible execute Java methods as functions. So, for a list of String objects, if I want to make all of them upper-case, all I have to do is:</p>
<pre style="border: solid gray 1px; width: 60%; padding: 5px;">
(map (memfn toUpperCase) ["a" "short" "message"])
</pre>
<p>The  <span style="font-family: courier;">map</span> function applies the function/method  <span style="font-family: courier;"> toUpperCase</span> to each element in  <span style="font-family: courier;">["a" "short" "message"]</span></p>
<p>You can also use the  <span style="font-family: courier;">bean</span> function to wrap a Java bean in an immutable Clojure map.</p>
<pre style="border: solid gray 1px; width: 60%; padding: 5px;">
(bean (new Person "Alexandre" "Martins"))
-> {:firstName "Alexandre", :lastName "Martins"}
</pre>
<p>Once converted, you can manipulate the new map using any of Clojure’s map functions, like:</p>
<pre style="border: solid gray 1px; width: 60%; padding: 5px;">
(:firstName (bean (new Person "Alexandre" "Martins")))
-> Alexandre
</pre>
<p>The code above extracts the <span style="font-family: courier;">firstName</span> key, originally from the  <span style="font-family: courier;">Person</span> object.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.m.artins.net/clojure-integrating-with-java/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
