I’ve known about tries for sometime. They’re a pretty neat data-structure for storing and looking-up strings.I decided to try and implement one in Java so that I can learn more about them. I’ll post another article later that goes into some more detail about this implementation, but for now you can check out the source here. It’s not production-ready by any means; it’s just me playing around.
The master was meditating when a priest respectfully entered his chamber. The master opened his eyes. The priest bowed respectfully and said, “Master, I would like you to look at the code of a young disciple of mine”. The master nodded and followed the priest to a computer. On the screen, was a code listing. The priest pointed to the code and said:
“My disciple created an abstract class and another class that extends the abstract class. However, he has a method that should be of use to all future derivations of the abstract class.”
The master furrowed his brow and looked at the code. “Indeed.”, he said. The priest continued, “I pointed this fact out to him and mentioned that it would be better to define it in the abstract class”. “I see; what did he say?”, asked the master.
The priest sighed and said, “He said ‘You Ain’t Gonna Need It’ and that he could simply copy the method into each new derivation when the time comes”. The master then asked the priest to bring the disciple to him at once. The priest bowed and went away to fetch the young disciple.
A few minutes later, the young disciple respectfully entered the master’s chamber. “You sent for me, master?”, he said. “Yes. I have a task for you”, said the master. The disciple bowed, indicating that he was ready to perform whatever task the master required. The master looked at the disciple and said: “Tomorrow, we will have monks visiting from a neighboring monastery. In their honor, our monastery is providing a feast for them. I need you to report to the dining hall tomorrow. The cook will give you further instructions”. The disciple bowed and left the master’s chamber.
The next morning the disciple arrived at the dining hall as he was asked. He looked around and noticed a large number of seats. He assumed, correctly, that these seats were for the visiting monks. He then noticed the monastery’s cook approaching him. The cook was holding a bowl that was filled with a white substance. As the cook got closer, the disciple realized that it was salt. Once the cook reached the disciple, he held out the bowl to him and said, “Master has asked you to give salt to any of the monks who desire it”. The disciple took the bowl and the cook left. The disciple was puzzled, but smiled thinking that this was going to be an easy task. After all, how many monks would require more salt in their food? He estimated only a few; not much more than that.
In a few minutes, the visiting monks arrived and sat at their places. Other monks from the hosting monastery brought out steaming bowls of soup for the visitors. The head visiting-monk took a spoonful of soup, sipped it and wrinkled his nose. “This soup does not have any salt!” he said, rather loudly. The disciple quickly ran up to the head visiting-monk and bowed and said “Master, I apologize that there is not enough salt in your soup, please allow me to offer you some!” The head monk nodded and the disciple quickly added salt until the monk motioned him to stop. He had barely finished when he heard another monk complaining that there was no salt in his soup either, then another, and another. Soon he was running around the hall at full speed, bringing salt to each of the visiting monks, trying his best to make sure that everyone was happy. The hall was big, and the number of visiting monks was many. When he was done, he sat down exhausted, in the corner of the hall.
He closed his eyes and tried to catch his breath. When he opened his eyes, he noticed that the next course was being brought in. “Surely the cook wouldn’t have forgotten to add salt this time!”, he thought. Unfortunately, it was not so! “There is no salt in my meal!”, thundered the head visiting-monk. The disciple got up and ran to the head monk to add salt his meal. Soon other monks started complaining and the disciple was running around the hall as he had done before, offering salt to all the visiting monks. The disciple hoped that this would be the last time he would have to do this, but alas, there were three more courses! The rest of the courses passed by in a blur for the disciple. All he could remember was that he was running around the entire hall, bringing salt to the visiting monks. The monks didn’t all finish their meals at the same time and so the later courses were continuously being brought out. Hence, he was always on his feet and didn’t get a chance to rest.
Finally, the monks stopped asking for salt and the disciple wearily went back to his corner. Dessert would be next, and there would be no need for salt then. He had barely sat down when he saw the cook approaching him. This time he had another bowl, also filled with a white substance. When the cook got close, he offered the bowl to the disciple and said a single word: “Sugar”. The disciple was almost in tears. He knew was was coming and prepared himself for the endless rushing around that he would have to do. Luckily there was only one course of dessert. When the feast was done, the disciple collapsed in the corner. He opened his eyes and noticed the cook walking towards him again. “What more will he want me to do?”, thought the disciple frantically. He noticed with some relief however, that the cook’s hands were empty. “Master will see you now”, said the cook as he got closer to the disciple. The disciple wearily got up to his feet and walked to the master’s chambers.
After a few minutes, the disciple arrived at the master’s chambers. He walked in and bowed in front of the master, his legs burning with fatigue. “How was your task?” asked the master with his eyes still closed. “It was exceedingly difficult master! There was no salt in the soup or the meals, and no sugar in the desserts! I had to run around the whole hall bringing salt and sugar to the visiting monks!”, said the disciple. “It must have been exceedingly tiring…”, said the master. “Yes, master! It was!”, said the disciple nodding his head. The master opened his eyes and said, “One could say that your task would have been much easier had the salt and sugar been added to the meals at the source, and thus before they were brought out to our honored visitors.”
In that moment, the disciple was enlightened.
A novice monk had just started learning assembly programming when he was troubled by doubt. He approached his master and asked:
“Master, how do I know which is code and which is data?”
The master who was meditating, opened his eyes, smiled, and said:
“Each is the other, yet neither is either.”
“Master, I do not understand.”, said the disciple.
The master then brought out two identical pots and said. “Take these. Fill one with the water from the lake, and fill the other with water from the stream that flows into the lake. Then bring them to me.”
The monk bowed and took the pots. He walked to the lake, which was some distance away and filled it with water from the lake. Then he walked around the side of the lake until he found the stream that fed water into the lake. He used this water to fill the second pot and then brought both pots back to his master and set them at his feet. The master looked at the pots, and then back to his disciple and said “Now go. You may come back tomorrow morning.”
The disciple came back the next morning to find his master standing with the two pots. He held up both the pots and then threw them to the opposite sides of the room. The pots smashed, and the water from both pots flowed towards the center of the room, forming a puddle.
The master then said, “Which pot contained water from the stream? Which pot contained water from the lake?” He then pointed to the growing puddle that was forming in the middle of the room. “Which part of the puddle contains water from the stream? Which part contains water from the lake?”
In that moment, the novice was enlightened.
vim has an awesome feature, using which you can pipe a range through an external command. This is pretty useful if you’re opening up an un-indented or poorly-indented XML or HTML file. If you want to indent your entire file, simply do the following:
:%!tidy -i -xml -q
The -i option tells tidy that it needs to indent the content, -xml tells tidy that the content is well-formed XML, and -q puts tidy into “quiet mode” where extraneous information is suppressed. You can also specify ranges like so:
:40, 74!tidy -i -xml -q
This indents content between lines 40 and 74 (both lines inclusive). You can also do:
:., .+50!tidy -i -xml -q
This indents the current line and the next 50 lines. You can also do the same for HTML:
:%!tidy -i -xml -q
You can of course, supply additional parameters to tidy to customize the indenting.
:%!tidy -i -q
I was trying to get pinch-zoom and panning working on an Android view today. Basically I was trying to implement the same behavior you see when you use Google Maps (for example). You can zoom in and pan around until the edge of the image, but no further. Also, if the image is fully zoomed out, you can’t pan the image. Implementing the pinch-zoom functionality was pretty easy. I found an example on StackOverflow. I then wanted to implement panning (or dragging) as well. However, I wasn’t able to easily find examples and tutorials for this functionality. I started with this example that comes from the third edition of the Hello, Android! book but I didn’t get too far. So I started playing around a little bit with the events and started writing some code from scratch (using the example from Hello, Android!) so that I could have a better idea of what was happening.
As I mentioned before, getting zoom to work was pretty easy. Implementing panning/dragging was the hard part. The major issues I encountered and subsequently fixed were the following:
- Panning continues indefinitely in all directions.
- When you zoom and then pan, stop, and then start again, the view jerks to a new position instead of panning from the existing position.
- Excessive panning towards the left and top can be constrained, but panning towards the right and bottom is not so easily constrained.
Once I fixed all the problems, I figured that it would be nice to document it for future reference, and I also think it would be a useful resource for others who have the same problem. Now a little disclaimer before I go any further: I’m not an Android expert and I’m really not that great with graphics; I just started it learning to program for Android this semester for one of my Masters electives. So there might be a better way of doing all this, and if there is, please let me know! Also, if you want to skip all the explanations and just see the code, you can skip to the last page.
I’ve always been interested in Neural Networks (ever since I first found out about them around 10 years ago). However, I never got a chance to learn about them or write one of my own; this is something I’ve wanted to do for some time. I got the opportunity this semester when my professor in my advanced data-structures class told us that we could pick any topic we liked, for a semester project. I thought that this would be the perfect time for me to learn about neural networks and create one of my own.
The end result was Akṣi, a neural network that recognizes hand-written digits. I’ve hosted it using Google’s App Engine. Please check it out!
I was using a jQuery plugin called a ajaxfileupload to upload a file through AJAX. Technically what the plugin does isn’t AJAX. It creates a hidden form and an iframe, and then submits the form using the iframe as the target. The iframe will then end up with the response from the server. This response is then read-in by the plugin and handled appropriately. In my case I was using a controller action that would return JSON (using the .action extension). The action uses Spring’s MappingJacksonJSONView that returns JSON with a content type of application/json (as it should). This works perfectly in Chrome, however in both Firefox and IE, the user is presented with a dialog box that asks them to download the JSON response. This is obviously not what I wanted. The reason this is happening is because the response is being directly submitted to the iframe (and therefore, the page). That is, it’s not coming through via the XMLHttpRequest object. So IE and FF don’t know what to do with it and assume that it is something the user would want to download. The solution to this problem is to set the content-type to text/plain. This wasn’t as straightforward as I thought it would be.
Initially I was going to call the render(…) method of MappingJacksonJsonView but that didn’t work because the content-type had already been set to application/json. The solution I came up with was to duplicate some of the code (ugh) inside MappingJacksonJsonView to get the JSON as a string and to then write that to the response:
@RequestMapping
public void processFileUpload(HttpServletResponse response, Model model, ...) {
...
//Set the content-type and charset of the response
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
//I need to use another OutputStream here; I cannot use the response's OutputStream because that will cause errors
//later on when the JSP needs to render its content (recall that getOutputStream() can only be called exactly once
//on a response). Therefore I'm writing the data to a ByteArrayOutputStream and then writing the byte array from
//the ByteArrayOutputStream to the response manually.
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectMapper objectMapper = new ObjectMapper();
JsonGenerator generator = objectMapper.getJsonFactory().createJsonGenerator(byteArrayOutputStream, JsonEncoding.UTF8);
//Before I can convert the data into JSON, I will need to filter some attributes out of the model (namely BindingResult)
Map<String, Object> result = new HashMap<String, Object>();
for(Map.Entry<String, Object> entry : model.asMap().entrySet()) {
if(!(entry.getValue() instanceof BindingResult)) {
result.put(entry.getKey(), entry.getValue());
}
}
objectMapper.writeValue(generator, result);
response.getWriter().write(new String(byteArrayOutputStream.toByteArray(), "UTF8"));
}
This still seems a little hacky to me. A possible improvement is to annotate the action with @ResponseBody and return the JSON as a string without involving the response at all. If anyone has a better solution, I’m all ears!
I’m using a somewhat old plugin (it hasn’t been updated since ’09) called Popularity Contest to show the popularity of my posts. However, I noticed that it was breaking my RSS feed. This is due to the fact that the plugin doesn’t properly escape the ampersand character inside an image URL. Instead of & it uses just &. You can fix this by changind line number 2272 to:
$str .= '<img src="'.site_url('?ak_action=api_record_view&id='.$post->ID.'&type=feed').'" alt="" />';
I decided to change the theme on my blog. I went for a wider theme this time because on my older theme the real estate for post content was rather narrow. This made the posts excessively long. This new theme has a lot more room for posts and so it doesn’t look like I’m posting a wall of text… well, mostly… I still tend to write a lot sometimes.

