Letter Frequency Across English Words with Clojure

Most Unix systems have a file called 'words'. It is usually located at /usr/share/dict/words. This file is commanly used by spell check programs. The following clojure program divides this word list into four, and in parallel it counts the occurrence of every letter.



Run length Encoding and Decoding in Clojure

Run Length Encoding is a very simple compression algorithm. It simple condenses the number of repeated characters in a file. wwwwyyhse would become 4w2yse. Here is a very simple implementation in Clojure.



Anonymous Recursion in Javascript

Often recursive techniques require helper functions to handle some of the recursive logic for the main method. Most of the time these method are given not so great names like "fooHelper" which make the code less readable, and pollutes the name-space.

Anonymous functions are a great way to have unnamed functions, and are a great tool in many cases. However since they are unnamed it may not be so intuitive how to use an anonymous function recursively.

Thankfully javaScript makes this quite simple.

The arguments object has a property called "callee" which refers to the currently executing function. This can be used to make recursive calls. Of course you could also use named functions as long as they are not in the global scope, and this would not be a problem.

I will show both of these techniques by creating functions to test if two numbers are co-prime (their greatest common divisor is one).



Creating Your Own Ruby Magic

Working with rails, active_record, and ruby can be a very magical experience. If you do not fight against it, sometimes it seems like it knows what you want before you ask. This is because the language, and library designers are very clever at meta programming (altering the state of the program at run time). I want to show a basic and powerful technique, that I have enjoyed playing around with. One caveat is that you must consider if using these techniques is best for your application, as it can be less predictable then other routes.

What I am going to show is how to using method_missing, and respond_to? in your classes and modules, in order to respond to a variety of custom methods at run time. You can find method_missing under basic_object on ruby docs here..

method_missing will act as a fall back in the case that a method requested is not defined. It presents you with up to three arguments. The first argument will represent the name of the method requested. The second will be an array of the arguments given. The third will be a block if any was given. I will show the use of all three.

This is going to be the basic structure of my example.

The first Example I am going to show is writing a method to search for a value in a hash and and return an array of the key value pair. This is not a hard to do with the methods provided to you within ruby. The real difference is in the syntax you can use. For example with this example you can write searches like "Magic.find_where_joe(your_hash)", "Magic.find_where_cat(your_hash), and "Magic.find_where_23". The goal is to find the last term in the method name. The first step I want you take note of in the code below of is that I split the method_name into an array by the underscore.

I then validate the the first term is find, and then send it off to the the appropriate method. You can see how 'respond_to?' checks if the given method is really something I want to respond to. Play around with this with the tests I have provided.

Next lets make a method that counts down from a given number and passes an iterator to a given block.The syntax for calling this method will be like "Magic.down_from_9{|num| do something here}", or "Magic.down_from_183{|num| do something here}". This small method will also show off another powerful technique, which is using blocks in your methods.

Lastly lets write a method to find if an array contains a member of a given type. The syntax for using this method will be like "Magic.contains_string?([1,2,7,'hello',95,3])", "Magic.contains_fixnum?(['a',4,'c','z'])", "Magic.contains_symbol?(['hello', 4, Array.new, :cat])", or any other class.

Here is the full Example.



Queen Attack Problem in Clojure



Exercism.io Koans in CoffeeScript

Find these fun and challenging exercises at exercism.io