10 February 2010

Alcatraz versus the Evil Librarians

A friend at work told me a bit about Alcatraz versus the Evil Librarians by Brandon Sanderson and he happened to have a copy available to read as we were talking. So, I borrowed it and read it over a weekend. I enjoyed the book immensely. Alcatraz was creative, witty, and fun. The book is written in a first person point of view which I felt was refreshing. It gave new expression to the reading. The book being written in the present day and in the first person, the author was both the character and the author. This allowed for a feeling of really being told a story from an adventurer.

I also thoroughly enjoyed the humor. It was quick witted and fast paced. There were jabs at current literature and entertainment (nothing was called out by name, but you know what he is talking about). There were jabs at the "all knowing" educational institutions and their professors. There was a bit of witty sarcasm about everyday life and events that everyone is dealing with or has dealt with. This allowed the story to bring a bit of humor into some of the drab parts of life.

Now, while I enjoyed the story and had a hard time putting it down, I will not be reading any of the sequels. This book would have been best as a stand alone story. *On Soapbox* Why does every fantasy story have to be a series! *Off Soapbox* The style of writing and the humor became monotonous at the very end of the story. Wit repeated becomes much less witty. The plot was cute but juvenile, which did not really make me want to read more. I read because the humor made me smile (even laugh out loud at times), not because the plot was engaging.

I definitely suggest this book to anyone who would like a quick, fun story to read.

16 December 2009

PHP array_insert

I had the need the other day to create an ordered collection object. I wanted to be able to add elements to the end of the collection as well as at any point in the middle of the collection. Since I was using PHP as the language to create this object, an array was the obvious construct to use as the internal implementation for this collection. Adding elements to the end of an array is trivial, but inserting elements into the middle of the array turned out to be a bit more difficult.

My first thought was that PHP has all kinds of array functions so there must be an array_insert function. This assumption turned out to be wrong. My second thought is that I would have to cut the array into two pieces, insert the new element to the end of the first piece and then glue to two arrays back together. In fact, I coded this before reading the documentation for the array_splice function more closely.

The array_splice function has two optional parameters which are very helpful in creating an array_insert function. The first optional parameter is the length option. This option specifies how many elements to remove from the original array starting at the specified offset. The second optional parameter is the replacement option. This parameter specified what to replace the removed elements with. So, in other words, you can tell array_splice to remove 6 elements from an array starting at offset 2 and to replace the 6 elements with what ever is in the replacement array.

This is perfect for mimicking an array_insert function. If you specify to remove 0 elements and pass in the new element as an array of 1 item, it gets inserted at the specified index. Now for the example:
function array_insert($array, $index, $new_element)
{
    return array_splice($array, $index, 0, array($new_element));
}
There you have it, array_insert nice and clean and optimized in c.