Sunday, June 29, 2014

Exercism: "Nucleotide count in Clojure"

This is my solution to the Nucleotide count problem in Clojure:


The code is very similar to the one for the Word count exercise. The main difference is that here I used a set to validate that the given nucleotide is valid.

I eliminated the duplication between the set and the map contents in this second version:


where I defined a set containing only DNA nucleotides, dna-nucleotides, that I used to define the nucletides set using conj. This dna-nucleotides set served to generate the counted-nucleotides map using the zipmap function.

Trying to avoid duplication I discovered several new things about Clojure.

You can nitpick my solution here or see all the exercises I've done so far in this repository.

--------------------------------

Update:

After learning some new stuff, I've been able to simplify the code a bit more:


It turned out that the frequencies function already did the counting out of the box, I jut needed to merge it with the result for an empty strand to make frequencies output conform with what the tests were expecting.

I also made the dna-nucleotides and nucleotides sets private.

You can nitpick this new version here.

Saturday, June 28, 2014

Exercism: "Beer song in Clojure"

This is my solution to the Beer song problem in Clojure:

I wrote a first version using just regular functions:


and a second one using multimethods:


I enjoyed refactoring the code once all tests were passing.
I also practised how to use multimethods and how to pass parameters with default values.

You can nitpick my solution here or see all the exercises I've done so far in this repository.

Friday, June 27, 2014

Exercism: "Anagram in Clojure"

This is my solution to the Anagram problem in Clojure:


This time I decided not to make the helpers local because they might be useful by themselves.
I also decided to separate in different functions the case-sensitive and case-insensitive versions of the anagram detection function.

You can nitpick my solution here or see all the exercises I've done so far in this repository.

Exercism: "Rna Transcription in Clojure"

This is my solution to the Rna Transcription problem in Clojure:


I used a map to look-up the transcription of each nucleotide.

The AssertionError exception was necessary to pass the provided tests. I would have rather used a different exception.

You can nitpick my solution here or see all the exercises I've done so far in this repository.

--------------------------------

Update:

After learning some new stuff, I've been able to simplify the code a bit more:


I used the if-let form, def ^:private and the defn- macro in order to improve readability

You can nitpick this new version here.

Thursday, June 26, 2014

Exercism: "Word count in Clojure"

This is my solution to the Word count problem in Clojure:


I created some local helpers and played with partial and comp to make the code more readable.
I also reduced the number of parameters of some local helpers by using some free variables.

I really liked how the reduce function can be applied to a map data-structure in Clojure.

You can nitpick my code here.

--------------------------------

Update:

After learning some new stuff, I've been able to simplify the code a bit more:


I used the frequencies function to count each word frequency and the defn- special macro to make extract-words-from function private.

You can nitpick this new version here.

Wednesday, June 25, 2014

Exercism: "Hamming distance in Ruby"

This is my solution to the Hamming problem in Ruby:

I tried to improve readability by creating the helper class methods base_distance and both_exists?. I also wanted to play a bit with map and reduce.

You can see two other versions here. I used zip in all of them.

This exercise served me to practice a bit with some Ruby collection functions.

Exercism: "Bob in Clojure"

This is my solution to the Bob problem in Clojure:


I just used cons and created some local helpers to make the code more readable.

You can see two other versions here.

This exercise served me to practice a bit with regular expressions and to discover some new useful Clojure functions.

Sunday, June 22, 2014

Interesting Talk: "467 tests, 0 failures, 0 confidence"

I've just watched this wonderful talk by Katrina Owen: She talks about a practical application of Sandi Metz's "magic" testing tricks and what those heuristics can tell you about your design.

Exercism.io

This weekend I've started working through the Exercism exercises.

First of all, I'd like to thank Katrina Owen for starting this wonderful project.

Exercism is a great open source project that she started in order to "provide a space to think deeply about simple, expressive, and readable code, and experiment and discuss what good looks like".

There are exercises in many languages but I'm only working on the Clojure and Ruby ones.

Even though the exercises have been very simple so far, they've helped me to practise and learn more about Clojure's and Ruby's powerful collection and string functions.

I've also learned some tricks and functions I didn't know by looking at the solutions to the same exercises posted by other people. As soon as I gain enough confidence in Clojure and Ruby I will start giving them feedback.

I hope to start receiving some feedback about my solutions in order to improve them.

Here you can find the few exercises I've done so far.

Interesting Talk: "Steve Freeman On TDD: How Do We Know When We’re Done?"

I've just watched this great talk by Steve Freeman:

Sunday, June 15, 2014

Saturday, June 14, 2014

Interesting Talk: "Testable Javascript"

I've just watched this great talk by Mark Ethan Trostler in which he describes some great principles, techniques and patterns to make your JavaScript code more testable:
He is also the author of the Testable Javascript book.

------------------------

PS: This other version of the talk is also great.

Kata: Berlin Clock in Clojure with Midje

I did the Berlin Clock kata in Clojure using Midje.

These are the tests:


And this is the final version of the code:


I used a mix of TDD and REPL-driven development.

To document the process I commited the code after every passing test and every refactor.
I also commited the .lein-repl-history file so that you can see all the micro-tests I did on the REPL to create some of the features and learn about Clojure including many beginner mistakes :)

You can find the resulting code in this GitHub repository.

Wednesday, June 11, 2014

Protractor: How to click a select option that has a given value

Today I needed to click on a select option that had a specific value using protractor.
After Googling for a while, I did it using the element selection by xpath.
This is the helper function I created:



And this is how it's used from the test:



----------------

PS: Check also my previous posts about Protractor:

Monday, June 9, 2014

Protractor: How to select a dropdown option to avoid ElementNotVisibleError

Another problem we found last week using protractor was when we tried to click on a dropdown option.

This was the problematic code:


which caused the following error:
ElementNotVisibleError: element not visible

This happened because the element we were trying to click on was hidden.

Googling a bit again, we found (here) that to avoid the error we had to click on the dropdown before clicking on the option to open it up, so that it wa visible to the user:


Again we extracted this bit of code into a helper method in out TestTools module:


----------------

PS: Check also my previous post about Protractor:

Protractor: How to avoid UnexpectedAlertOpenError

At work, we've recently started to use protractor to write end-to-end tests for a legacy AngularJs code base.

Last week we found a problem while trying to test that an element from a list was deleted:


The problem was that clicking on the delete icon was displaying an alert box, prompting the user to confirm whether they actually wanted to delete the element or not, that protractor didn't expect and was causing the following error:
UnexpectedAlertOpenError: unexpected alert open

After searching in Google for a while, we found a solution:


that fixed the problem by switching to the alert box and confirming that we wanted to delete the element.

Since it was a bit verbose and we'd probably have to use it again in some other tests, we extracted it to a helper function in our TestTools module:


I hope you might find this useful if you're using protractor.

Sunday, June 8, 2014

Practising Clojure sequences using FizzBuzz and REPL Driven Development

I recently blogged about my version of the FizzBuzz kata in Clojure using Midje comparing it with a version I did some time ago in Racket's Advanced Student Language.

Even though both solutions were very similar, doing the kata this time in Clojure was useful to practise with Midje.

To try to get to a different solution for the kata and use the Clojure sequence library, I set myself a constraint:
Instead of mapping just a function on the sequence of numbers, I had to find several transformations that could be mapped one after another on sequences to get the same result.

Since I had to do a lot of trial and error, I prefered to use REPL Driven Development this time.

This was the REPL session to get to the first alternative version (eliminating a lot of errors..):


From this REPL session I got this alternative version for fizz-buzz:


It's more complicated that the original version but it served me to practise some interesting Clojure features: recur, partial, map and let.

Then I tried a different approach:
Reducing the sequence of transformation functions so that I can map the resulting transformation function on the sequence of numbers.

Again I worked in the REPL first:


From this second REPL session I got this other alternative version for fizz-buzz:


which is again more complicated than the original version but served me to work with other intersting Clojure's features: reduce, comp and reverse.

After these two versions of FizzBuzz I did a web search to find nicer ways to use the Clojure's sequence library to implement FizzBuzz and I found these two in Rosseta Code that I specially like (I modified them a bit so they return the same that the precious versions):



These two last versions will serve me to learn about if-let and cycle.

Ok, that was all I got today.

Just to sum up, I think there is a lot you can get from even a simple kata like FizzBuzz if you set yourself some constraints and try different variations.

--------------------------------------------------

PS: You'll find other variations of FizzBuzz kata in these previous posts: this one and this other one.

Kata: FizzBuzz in Clojure with Midje

I did the FizzBuzz kata in Clojure using Midje.
You can find the resulting code in GitHub.

I love Midje's readability. Look at the tests:


This is the FizzBuzz code in Clojure:


Compare it to this version I did in Racket's Advanced Student Language some time ago:


They are more or less the same (except that Clojure's version returns a string whereas Racket's one returns a list of strings).

On one hand, I prefer Clojure's cond which is more readable because it's more lenient with the parentheses.

On the other hand, I really like how Racket's local allows to define local functions inside another function in the same way they are defined in a module.
In Clojure I also defined a local function is-multiple-of? using let but the result is less readable than using Racket's local.

Update: I keep on working on this kata on the next post, Practising Clojure sequences using FizzBuzz and REPL Driven Development.