Last week I was discussing with @romandreg about how sort algorithms could be so intuitive when you are thinking in a functional style. They become very succinct and expressive. After some thinking I implemented a version of insertion sort and quicksort in ruby following a functional style.
First let’s see an example of insertion sort in a classic imperative style:
1.upto(length - 1) do |i|
value = self[i]
j = i - 1
while j >= 0 and self[j] > value
self[j+1] = self[j]
j -= 1
self[j+1] = value
n a functional style, you could think that there are two functions involved when you are doing insertion sort. One that inserts a given value where it belongs and another that reduces the original array using this function:
# Function to insert a value where it belongs
# in an ordered array.
insert_in_place = -> (array, new_elem) do
head, *tail = array
return [new_elem] unless head
if head > new_elem
[new_elem] + array
[head] + insert_in_place.call(tail, new_elem)
Lets see how this same of kind of thinking applies to quicksort:
“You should also be careful to not have addresses which are valid
alternate syntaxes to the inet_ntoa() library call. For example 0xe
is a valid name, but if you were to type “telnet 0xe”, it would try
to connect to IP address 0.0.0.14. It is also rumored that there
exists some broken inet_ntoa() routines that treat an address like
x400 as an IP address.”
RFC 1912, Common DNS Operational and Configuration Errors
Today I found myself writing an integration test where an input field was using token-input auto complete plugin. After a considerable amount of pain and without finding an out-of-the-box solution on google, here is a possible approach to solve the problem. It’s worth saying that is basically a merge between the approaches proposed here and here.
1) Add the following module in spec/support:
def fill_token_input(locator, options)
raise "Must pass a hash containing 'with'" unless options.is_a?(Hash) && options.has_key?(:with)
2) Include the module in your spec helper:
RSpec.configure do |config|
config.include TokenInputHelper, :type => :feature
3) Finally, in your test you can do something like this:
Last Sunday I stumble upon 6th Street in Downtown L.A. For those of you who had never been in this part of town it looks like a Brazilian fabela in the heart of a city but with less dignity. Maybe is not going to be as dangerous as a fabela, but it definitely looks very sad.
I don’t want to describe it myself, because I wont make justice to the harshness of the scene. To give a better a idea here are two pictures:
These two streets are less than two blocks away. The gap between the two of them is beyond my understanding.
For me it is always very shocking to find this kind of contrast in countries like U.S.A where you could think that the resources to solve this kind of social problems are or should be there. In my humble opinion, the problem here is about political will. This could be better and it shouldn’t be like this. America has a lot of good things, but there are other that are really bad. The people of this country (and the world in general) needs to be shaken and get more committed to each other (me included). We do not live in islands.
I would like to close this post with a song that has the same allure that those streets…