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.