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.

No comments:

Post a Comment