<?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; Functional Programming</title>
	<atom:link href="http://blog.m.artins.net/tag/functional-programming/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>Hamcrest Out Of Test Code!</title>
		<link>http://blog.m.artins.net/hamcrest-out-of-test-code/</link>
		<comments>http://blog.m.artins.net/hamcrest-out-of-test-code/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 12:38:30 +0000</pubDate>
		<dc:creator>Alexandre Martins</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Behaviour-Driven Development]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Domain-Driven Design]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://blog.m.artins.net/?p=168</guid>
		<description><![CDATA[It's been a while since I read some interesting posts showing creative uses of Hamcrest library out of test code. Since then I've been proscrastinating to implement my own version, trying strongly typed java delegates. Thankfully this week I came across a nice API called hamcrest-collections. It uses Hamcrest to implement features such as select, [...]]]></description>
			<content:encoded><![CDATA[<p>It's been a while since I read <a href="http://joe.truemesh.com/blog/000705.html">some</a> <a href="http://jroller.com/ghettoJedi/entry/using_hamcrest_for_iterators">interesting</a> <a href="http://cromwellian.blogspot.com/2006/07/fun-with-generics-and-cglib-functional.html">posts</a> showing creative uses of <a href="http://code.google.com/p/hamcrest/">Hamcrest</a> library out of test code. Since then I've been proscrastinating to implement my own version, trying <a href="http://weblogs.java.net/blog/alexwinston/archive/2005/04/strongly_types_1.html">strongly typed java delegates</a>. </p>
<p>Thankfully this week I came across a nice API called <a href="http://code.google.com/p/hamcrest-collections/">hamcrest-collections</a>. It uses Hamcrest to implement features such as <strong><code>select</code></strong>, <strong><code>reject</code></strong>, <strong><code>map</code></strong>, <strong><code>reduce</code></strong> and <strong><code>zip</code></strong> familiar from languages like Ruby and Python. </p>
<h2>Selectors</h2>
<p>Selectors can be used to select or reject items that matches a given Matcher, from any iterable object. It reminds me the <a href="http://martinfowler.com/apsupp/spec.pdf">Specification Pattern</a> from <a href="http://domaindrivendesign.org/">Domain-Driven Design</a>, which is also used for querying objects that satisfies defined specifications.</p>
<pre style="border: solid gray 1px; width: 90%; padding: 10px;">
public static final Person john = new Person("John", 28);
public static final Person nicole = new Person("Nicole", 12);
public static final Person ryan = new Person("Ryan", 23);
public static final Person nathan = new Person("Nathan", 18);

public static final List<Person> list() {
    return Arrays.asList(john, nicole, ryan, nathan);
}</pre>
<p><br/><br />
The code below selects from the list of users defined above, the ones that are under twenty.</p>
<pre style="border: solid gray 1px; width: 90%; padding: 10px;">
@Test
public void should_select_only_people_under_twenty_years_old() {
    List<Person> users = Person.list();
    Iterable<Person> underTwentyList = select(users, underAge(20));
    assertThat(underTwentyList, hasItems(nicole, nathan));
    assertThat(underTwentyList, not(hasItems(john, ryan)));
}
</pre>
<p><br/><br />
The code below rejects all the users that are under twenty.</p>
<pre style="border: solid gray 1px; width: 90%; padding: 10px;">
@Test
public void should_reject_every_people_under_twenty_years_old() {
    List<Person> users = Person.list();
    Iterable<Person> aboveTwentyList = reject(users, underAge(20));
    assertThat(aboveTwentyList, hasItems(john, ryan));
    assertThat(aboveTwentyList, not(hasItems(nicole, nathan)));
}</pre>
<p><br/></p>
<h2>Map and Reduce</h2>
<p>Map is used to apply a function onto each item in any iterable object, whereas Reduce combines all these elements, applying a Reducer implementation. In our example, we map the timesTwo function, that doubles each element in the list, and then we reduce it by adding up all of them.</p>
<pre style="border: solid gray 1px; width: 90%; padding: 10px;">
@Test
public void should_double_each_number_in_the_list_then_sum_all_of_them() {
    List<Integer> numbers = Arrays.asList(1, 2, 3);
    MultiplyBy timesTwo = new MultiplyBy(2);

    Iterable<Integer> result = map(numbers, timesTwo);
    assertThat(result, hasItems(2, 4, 6));

    Integer sum = reduce(result, new Sum());
    assertThat(sum, equalTo(12));
}</pre>
<p><br/></p>
<pre style="border: solid gray 1px; width: 90%; padding: 10px;">
public class MultiplyBy implements Function<Integer, Integer> {
    private Integer factor;

    public MultiplyBy(Integer factor) {
        this.factor = factor;
    }

    public Integer apply(Integer number) {
        return (int)number * factor;
    }
}</pre>
<p><br/></p>
<pre style="border: solid gray 1px; width: 90%; padding: 10px;">
public class Sum implements Reducer<Integer> {
    public Integer apply(Integer first, Integer second) {
        return first + second;
    }
}</pre>
<p><br/></p>
<p>Despite the bias created by some developers, that Hamcrest should not be used anywhere else but test code, specially after JUnit has defined it as its new matcher library, just ignore it and add these features to your runtime library, so that you can let your creativity drive you when developing. Get rid of "for" loops from your life! <img src='http://blog.m.artins.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.m.artins.net/hamcrest-out-of-test-code/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<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>
