Functional programming with Clojure

Clojure

I’ve been studying the new language called Clojure (all the cool kids are talking about Clojure). It is a functional language created by Rich Hickey around 2007. This is a(nother) dialect of Lisp. It is a dynamic language as Ruby, JavaScript and others. As said before Clojure (pronounced as closure) it’s a impure functional language in contrast with Haskell, a pure functional language. It runs over the JVM, so it’s fast, interoperable with Java among a lots of good stuffs that JVM give us. To put hands-on and try code something you can use the try Clojure online or you can download the clojure.jar file and run it. Surprisingly Clojure it’s easy to learn.


java -jar clojure-x.x.x.jar

What it a functional language? (concepts)

first-order functions -> functions are treated as values. You can store a function on a variable, you can pass one function to another or you can return a function from another function.

var sum = function(a,b){
  return a + b;
};

var obj = function(sum){
  return {
    hello: "hello",
    sum: sum
  };
}();

obj.sum(3,5);

functions constructs -> the language constructs are function instead of keyword. Constructions for conditions (if), for iterations (for, while), catch exceptions (try, catch) and others.


(if condition do-it else-do-it)

stateless -> it’s functional in the sense of math, you have functions which defines values input and output and doesn’t rely on outside global state. In such pure function you won’t produce any side-effect (read, write outside resource). Obviously we will produce programs which causes side-effects, clojure helps you build “mutable” data . On other pure languages like Haskell side-effects are treated as expections so you have concepts like actors and monad.

immutable data -> collections and local variable, in clojure, are immutable. The immutability, helps us in parallelism, since the “values” are immutable you can shared then without worry about locks.

currying -> is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application).

memoization -> is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously processed inputs.

Resources

Put the bricks together on gnu linux

Why Linux?

It is easy, flexible, very secure, updated, “free”, open source… and the list goes on however what makes me more happy on this little world *unix it is its flexibility.

The power

Let start with a simple command, like ps, which can show you a list of current process and its details.

ps -aux

Now, I would like to just shows the output lines which has usr on it. To do that I will use the output of ps -aux command as input to grep filter the data. On linux you can pass the output of a command to another by using a pipe. So we can redirect the last output as input to another one.

ps -aux | grep usr

Now we have the output from ps filtered by only lines which contains ‘usr’. But what if want only the process id’s. There is a program for that too, the cut.

ps -aux | grep usr | cut -d " " -f 8

This program cut is simple creating fields (-f) delimited (-d) by a space (” “) and I ask the program to pick the field 8, which is the process ids. But if you notice there is some lines without number. The next step is remove this lines without values. We can do it by using sed program.

ps -aux | grep usr | cut -d " " -f 8 | sed '/^$/d'

Now our output has only lines with numbers, sed program receives a simple regex and says delete this pattern. Nice but I just need those numbers ending with 2. We can grep again.

ps -aux | grep usr | cut -d " " -f 8 | sed '/^$/d' | grep $2

I also want to sum all this numbers and show on the screen the result. To achieve that we are gonna use awk program.

ps -aux | grep usr | cut -d " " -f 8 | sed '/^$/d' | grep $2 | awk '{ sum += $1; print "+" $1} END {print "_____" ; print sum}'

The first code (surrounded by brackets) will create a variable called sum and it also prints plus and each first argument passed to it. This first block will be called for every line we pass (processed by all the programs we did it) and it follows by END that will print a line and finally prints the sum of all this stuffs.

+1692
+1712
+1732
+1752
+1762
+1962
+2062
_____
12674

You can use programs connect them produce output that only one program can not do. PS: I know I could too all this with less programs/commands but the intend here was not teach linux/GNU programs it was show you how powerful can be linux.

Bonus round

You can put this output to a file just using the redirect to a file. ( > or >> to append)

ps -aux | grep usr | cut -d " " -f 8 | sed '/^$/d' | grep $2 | awk '{ sum += $1; print "+" $1} END {print "_____" ; print sum}' > file.txt

UX – Teach your users how to use properly your webthing

Very inspired by Modelo mental and this amazing video here.

How Google taught me use it properly!

I always use Google.com as a tool for an uncountable things! And I always use the advanced search, mainly for search only over the last week. This feature (I mean Advanced Search) used to be at the home page.

However right now, there is no such link on google home page. (or at least in my browser for the last weeks). But the google guys also used to put this feature link on the top’s search page, as the bellow image.

And now there is no this link there too, this feature link was moved to the lower part of result list page. That’s okay, at the first sigh I thought they could facing performance issues with more and more users using this kind of search. But man they are the Google.com they face and win perforamance challenges.

What was really interesting about this place changing feature links, was I “discovery” that google already provide a side bar links to search only on last X age. I learn that looking for the advanced search. This is what I call the masterpiece of UX, I learn a new feature just by using it naturally.

Testing JavaScript with Jasmine and Jessie and node.js

Testing
Testing JS, can you imagine?

Javascript

JavaScript is a prototype-based, object-oriented scripting language that is dynamic, weakly typed and has first-class functions. It is also considered a functional programming language like Scheme and OCaml because it has closures and supports higher-order functions. Cool language isn’t? A new feeling inside me tells me that everything (related to I.T.) I would like to learn I should start writing a tests. However I thought that JavaScript test was stucked on Alert windows and then I found out Jasmine framework.

How could we write tests to JavaScript?

Using Jasmine in a BDD style.

describe("Jasmine", function() {
  it("makes testing JavaScript awesome!", function() {
    expect(yourCode).toBeLotsBetter();
  });
});

To run it and get feedback you have some options whose I take two: seeing the html file on browser and seeing the terminal result as rspec way. We will do the second way. For that we will use: node.js, npm and jessie.

node.jsevented i/o server (and also you can use it as an interpreter) for javascript vm (specifically v8)
Installing node.js

git clone --depth 1 git://github.com/joyent/node.git
cd node
git checkout v0.4.11 #opt, note that master is unstable.
export JOBS=2 #opt, sets number of parallel commands.
mkdir ~/local
./configure --prefix=$HOME/local/node
make
make install
echo 'export PATH=$HOME/local/node/bin:$PATH' >> ~/.profile
echo 'export NODE_PATH=$HOME/local/node:$HOME/local/node/lib/node_modules' >> ~/.profile
source ~/.profile

npm – node package manager, as its own name suggest. You can use it to install and publish your node programs. It manages dependencies and does other cool stuff.
Installing npm.

curl http://npmjs.org/install.sh | sh

jessie – Jessie is a Node runner for Jasmine. It was created to provide better reporting on failures, more modular design, easier creation of formatters and optional syntactic sugar.
Installing jessie.

npm install jessie

With all these binaries on your path you can just run your tests from terminal typing:

jessie folder_with_specs/ -f nested

In fact I’ve been using the gitub and jessie to learn and apply javascript.

RSpec and Watir to test web applications

Testing is cool

Software testing

Software testing is an investigation conducted to provide stakeholders with information about the quality of the product or service under test. Software testing can also provide an objective, independent view of the software to allow the business to appreciate and understand the risks of software implementation. Test techniques include, but are not limited to, the process of executing a program or application with the intent of finding software bugs (errors or other defects) – Wikipedia

The main intend of this post is, introduce you to UI tests over some ruby toys. In fact you could create an entire project (new) in ruby just to test your legacy web project. It’s cool, you can learn new language and work for the improvement of your legacy product. If you are totally new for ruby maybe a ruby overview can help you. (or might confuse you more)

Installing ruby, watir and rspec

Instead of installing the ruby directly, we are going to install the RVM (Ruby Version Manager) to then install any ruby we need. The steps described here were made on Ubuntu 11.04. On your terminal do the magic to install RVM.

bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
echo ‘[[ -s “$HOME/.rvm/scripts/rvm” ]] && . “$HOME/.rvm/scripts/rvm” # Load RVM function’ >> ~/.bash_profile
source .bash_profile

And from now on, your life will be better on ruby interpreters versions. Let’s install the ruby 1.9.2. (terminal again)

rvm install 1.9.2

And if we want to see the rubies installed on our machine?

rvm list

And now, how can we chose one ruby to work on the terminal session?

rvm use 1.9.2

For the test purpose we will use Watir and RSpec, great tools for testing, make fun with BDD and the best thing is install them it’s very easy.

gem install watir-webdriver
gem install rspec

Hands-on

Since we have all things installed, we can move for the example. The feature I want to test is the search system of  Amazon. Being more precise, I want to search for ‘Brazil’ and see if the ‘Brazil on the Rise’ is within the results as I want to be sure when I search for ‘semnocao‘ the Amazon doesn’t provide any result. Now, we can write the spec.

require 'amazon_page'

describe AmazonPage do
 before(:each) do
   @page = AmazonPage.new
 end
 after(:each) do
   @page.close
 end
 it "should show 'Brazil on the Rise' when I query for [Brazil]" do
  @page.query 'Brazil'
  @page.has_text('Brazil on the Rise').should == true
 end
 it "should bring no result when I search for [semnocao]" do
  @page.query 'semnocao'
  @page.results_count.should == 0
 end
end

The specification is very simple, it will create a page before each test calling and close the page after each test calling. There is only two tests: test when you search for Brazil  and  when you search for semnocao. We will design the tests using page object pattern. The class bellow is the page which represents the Amazon page and all testable behaviors should be inside of it.

require 'watir-webdriver'

class AmazonPage
 def initialize
  @page = Watir::Browser::new :firefox
  @page.goto 'http://www.amazon.com'
 end
 def close
  @page.quit
 end
 def query(parameter)
  @page.text_field(:id=>'twotabsearchtextbox').set parameter
  @page.send_keys :enter
 end
 def has_text(text)
  @page.text.include? text
 end
 def results_count
    if @page.text.include? 'did not match'
     0
    else
     @page.div(:id=>'resultCount').text.split(' ')[5].gsub(',','').to_i
    end
 end
end

To run this you just need to type on your terminal.

rspec spec/

The final code can be downloaded or viewed at github.

Additions

  • We could improve our story readibility  with Cucumber.
  • We could send the browser execution to an Xvfb server. (A.K.A. running headless) The browser pops up really bothers me.
  • We could integrate it with our CI.
  • We could design a base Page class for provide common operations as mixin or something

ps: the post was very inspired by KK post and Saush.