Saliya's Blogs

Mostly technical stuff with some interesting moments of life

Recursion is Natural

 

frontbend

“Yay! I can see my bu** !! … wait, it’s not my bu**, it’s myself !”

If you can come to the last realization that you are seeing yourself then you already understand recursion is natural. If not, the following few examples may help.

Example 1: Is Even?

Zero is even. Any integer N > 0 is even if N-1 is not even.

Example 2: Factorial

Factorial of zero is 1. Factorial of any integer N > 0 is N times factorial of N-1.

Example 2: Length of a List

An empty list has a length zero. Any other list has one head element and a sub list called the tail. So length is 1 more than the length of the tail.

Example 3: Map f() to List S

If S is empty then nothing to do just return an empty list. If not map f() to the tail and get a mapped list. Then add f(head) to the front of that list.

 

P.S. Many thanks to Dan Friedman (https://www.cs.indiana.edu/~dfried/) for his great class of B521 (cs.indiana.edu/classes/b521) at IU, 2009.

----------

About the image; it’s an InkScape sketching of the image I found at http://contortionistsunite.ning.com/profiles/blogs/day-1-3

Taming Wild Horses: Chapel Asynchronous Tasks

Chapel supports nesting data parallel and task parallel code arbitrary as desired. This allows you, for example, to spawn asynchronous tasks inside a forall loop. The code snippet below shows code for a case like this where a forall is run on an array of 3 elements. The work to be done for second element is time consuming, hence a new task is spawned to run the timeeater(). Seems straightforward isn’t it? What if timeeater() takes more time than the forall loop? You’d expect forall to wait till all the dynamically spawned tasks to complete, but unfortunately it’s not the case. So if you want everything to be done when you exit forall loop use the construct sync to synchronize.

Try running the code with and without sync and observe the value of result, which should be 500500 if forall exit only after all the tasks have completed.

var d : domain(1) = [1..3];
var a : [d] int = d;
var result : int;
sync forall i in a{
    writeln(i);
    if (i == 2) then {
       begin result = timeeater();
    }
}
writeln("end of forall");
writeln("result ", result);

proc timeeater(){
    var tmp : int = 0;
    for i in 1 .. 1000{
        tmp = tmp + i;
        if (i%25 == 0) then {
            writeln("eating time ", i);
        }
    }
    return tmp;
}

Chapel is Sweet

It has been a little while since I started playing around Chapel (http://chapel.cray.com/) language, but could not run anything fun and large until recently. As part of the B524 – Parallelism in Programming Languages and Systems class from Prof. Lumsdaine (http://osl.iu.edu/~lums/), we had to implement Single Source Shortest Path (SSSP) of Graph500 (http://www.cc.gatech.edu/~jriedy/tmp/graph500/) specification. Only then I could realize the easiness of many of the high-level abstractions provided in Chapel compared to other parallel languages or paradigms. Honestly, I did not expect it to work in the first run across a set of machines, but surprisingly it did!

Download a Set of URLs with GNU Wget

I had a list of URLs that I wanted to download and it was a pain to do it manually. So end up writing a simple shell script and downloading all of them using GNU Wget. Here’s the shell script (modified the one at http://www.linuxquestions.org/questions/programming-9/shell-script-that-read-each-line-separatly-364259/).

#!/bin/bash
# Set the field seperator to a newline
IFS="
"
# Loop through the file
for line in `cat file.txt`;do
wget $line
done

Blogging: Images in Comments

Finally, an awesome solution to a problem that I’ve been searching for quite a while: how to add an image in a comment to blog post?

Look no further, just visit Spice Up Your Blog on this at http://www.spiceupyourblog.com/2010/12/images-colored-text-blogger-comments.html

Apparently it has just more than adding images like colored text and scrolling text.


See the test comments I made for fun

Windows Live Writer: Life Made Easy for Blogging

I've been using Blogger for quite some time and over the years they have improved their Web based blog editor a lot, yet there was some uneasiness always when thinking about editing or adding a post. Anyway, after I got to know about Windows Live Writer I wanted to get it setup with Blogger but it wasn’t successful simply for some reason I couldn't understand “Username or password is incorrect”! I remembered only today that I was using two step verification with Google and that I have to create an application specific password to connect to Blogger. Wish it came to my mind sooner! Anyway, it’s now working fine and if you can read this online that means I made a post successfully with Live Writer.
Few places to note if you are having trouble connecting to Blogger with Live Writer as I did.
  1. Blog URL: Don’t forget to use https instead of http
  2. Username: Remember to add @gmail.com to your user id
  3. Password: As mentioned above, if you are using a two step verification with Google you need to generate application specific password to connect (see http://support.google.com/accounts/bin/answer.py?hl=en&answer=185833)

A Small Contribution: Substitution in The World of Lambda

Feeling a humble joy for being able to make a small contribution to a great resource: Semantics Engineering with PLT Redex, errata on substitution

Thank you Professor Matthias Felleisen for posting it and Professor Amr Sabry at Indiana University for the inspiring course on B522 - Foundations in Programming Languages, which made this possible.

SugarSync Public Link: Direct Download

SugarSync public links are great, but unfortunately they've added an intermediate download page instead of the direct download that used to be there (see more on this at http://www.sugarsync.com/blog/2012/01/10/if-you-love-your-public-links-set-them-free/).

May be it was done with good intentions, but it broke all the image links we had in our Web site. Anyway, it seems there's a way around to get images working back in your Web pages without much hassle.

The solution is just add the following to the end of each image link (I know it's bit work too, but way better than having copy images to a local folder and linking them again manually).

old-link?directDownload=true

Update 3/27/2012:

I tried the same trick to put an image to a blog post using a SugarSync public link, but it wasn’t successful. As it seems Blogger’s image retrieving service couldn’t handle the directDownload=true.

Either "either", "or","and" or "and/or"?

http://en.wikipedia.org/wiki/And/or

Copy Path to Clipboard : Another Life Saver

Want to copy the path of a file to clipboard in Windows without having to open properties window? Just try Copy Path to Clipboard v 1.00 (http://www.softpedia.com/get/System/OS-Enhancements/Copy-Path-To-Clipboard-JA.shtml). It's a pretty neat tool that saves quite a bit of human I/O time :D

Note. It works fine with Windows 7 as well.

ReSharper: Life Saver for Visual Studio Users

If you are using Visual Studio for application development then most probably you may have noticed the lack of built-in refactoring support. At least for me it was so obvious, probably because I was used to the rich environment provided by IntelliJIDEA (http://www.jetbrains.com/idea/) for Java development. So finally did some search to see if there's any third party plug-in for Visual Studio on this aspect and guess what! The same guys who build IntelliJIDEA has a plug-in called ReSharper (http://www.jetbrains.com/resharper/). I had no second thoughts to use it based on my experience and it sure changed my clumsy feeling towards Visual Studio :)

Unfortunately it's not free, but if you are doing open source development they give you it for free. Anyway, the cost isn't sky-high to purchase as well.

It's Cold Again

à¶…à¶³ුà¶»ු වළා à¶½ං à·€ුà¶±ා ද
à¶‘à¶½ිය à¶±ුà¶¹ේ à¶±ැà¶­ි à·€ුà¶±ා ද
à·ƒීà¶­ à¶…à·„à·ƒෙ රජ à·€ුà¶±ා ද
à¶±ුà¶¹ ආයෙà¶­් à·ƒැà¶Ÿ à·€ුà¶±ා ද

2/22/11

(C) Saliya Ekanayake

Subscript and Superscript with MS Word 2007 Equations

If you happen to use MS Word 2007 for equations and wonders why the heck there isn't a shortcut to subscript and superscript then you will like what I just found couple of minutes ago.

If you want to type e1 with 1 as subscript just type e_1. The moment you hit space it will become what you want. To make 1 superscript type e^1 and space. Enjoy!

Open Cubicles and Work

I wanted to blog about this for a long time and finally it's out today. If you are in the computer science world working in some company or doing research, cubicles will not be a new thing for you. A large room partitioned into blocks of small cubicles and two or more people working nearby are the typical nature of these work areas.

Honestly, I really really don't like this setting. I wonder who came up with this idea of having people sit nearby in the open and work. May be people are thinking that having open cubicles give more freedom to people because they are not physically constrained by walls or doors. But have they ever thought the effect on mind? Does physical boundaries affect the same way to mind? In fact, I think it's totally the other way around. You cannot think effectively when you are in open with others. Essentially what happens is that you are physically free, but mentally constrained.

The truth with everyone, no matter how much they don't like to show it, is that they have unique ways of working optimally. This may include things like clapping and rubbing the hands when your code works and say "Oh! Sh*t" when it doesn't. Not to mention the luxury of thinking silently. How much of these can you do when you are in a professional setting surrounded by others? Also, how much actions of others can you tolerate. Here's one of my personal experience. A person who sat next to me used to sip his coffee so loud and end each sip with the sound "aah". I understand that it's how he likes to enjoy his coffee. That's perfectly fine, but for me that sipping was annoying and disturbing.

So in my view, if you want to work effectively specially when you have to think, open cubicles are nothing but jail to your mind. If you think I am crazy, think of theses (http://www.guardian.co.uk/books/2009/sep/19/books-written-in-prison). These guys were in prison physically, but they had all the "space" in mind to think. No I am not suggesting to go to prison to work :D.

Anyway, another good video on "Why work doesn't happen at work", by Jason Fried from one the TED talks can be found from here (http://www.ted.com/talks/jason_fried_why_work_doesn_t_happen_at_work.html).

Great Feedback: MapReduce In Simple Terms

My presentation on MapReduce has received some great feedback via Pragmatic Integration. Isn't it really nice to see how somebody who you don't even know benefits from something you have?

WCF Hosting with IIS7

I started working with services in Windows world recently and got stuck pretty badly when I tried to host a WCF service in IIS 7. I was receiving errors no matter what solution I tried with Googling. All of them mentioned that I may not have installed ASP.NET properly, but I have installed it properly.

So I was clueless for a while, but luckily found this great article in WCF Tools team's blog, which mentioned that it may be because I installed Visual Studio prior to installing IIS. I did the simple command mentioned there and wow! it worked.

I will post a step-by-step guide in a later post on how to deploy your WCF Service in IIS7.

Hadoop: Writing Byte Output

Recently, I wanted to write the set of value bytes (only them; no key) from the Reduce task of a Hadoop MapReduce program. The signature of the reduce function looked like this.

public void reduce(Text text, Iterator<byteswritable> itr, OutputCollector<text,> output, Reporter reporter)


In the main method I used SequenceFileOutputFormat as the output format. But it turned out that this way I get output as a SequenceFile, which I cannot later read by a separate Java program to extract out the values. May be I am wrong here, but as far as my searching went on, I couldn't find a way to easily do this.

After being fed up with searching I thought of writing a custom FileOutputFormat just to suit my job. So I wrote this ByteOutputFormat class, which simply writes the value bytes as a binary file. So later I can read it using a normal (non Hadoop aware) Java program to extract the bytes.


import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.DefaultCodec;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.RecordWriter;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.util.Progressable;
import org.apache.hadoop.util.ReflectionUtils;

import java.io.DataOutputStream;
import java.io.IOException;

/**
* @author Saliya Ekanayake
*/
public class ByteOutputFormat<K, V> extends FileOutputFormat {
protected static class ByteRecordWriter<K, V> implements RecordWriter<K, V> {
private DataOutputStream out;

public ByteRecordWriter(DataOutputStream out) {
this.out = out;
}

public void write(K key, V value) throws IOException {
boolean nullValue = value == null || value instanceof NullWritable;
if (!nullValue) {
BytesWritable bw = (BytesWritable) value;
out.write(bw.get(), 0, bw.getSize());
}
}

public synchronized void close(Reporter reporter) throws IOException {
out.close();
}
}

@Override
public RecordWriter<K, V> getRecordWriter(FileSystem ignored, JobConf job, String name, Progressable progress)
throws IOException {
if (!getCompressOutput(job)) {
Path file = FileOutputFormat.getTaskOutputPath(job, name);
FileSystem fs = file.getFileSystem(job);
FSDataOutputStream fileOut = fs.create(file, progress);
return new ByteRecordWriter<K, V>(fileOut);
} else {
Class codecClass = getOutputCompressorClass(job, DefaultCodec.class);
CompressionCodec codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, job);
Path file = FileOutputFormat.getTaskOutputPath(job, name + codec.getDefaultExtension());
FileSystem fs = file.getFileSystem(job);
FSDataOutputStream fileOut = fs.create(file, progress);
return new ByteRecordWriter<K, V>(new DataOutputStream(codec.createOutputStream(fileOut)));

}

}
}


Hope this would be helpful for you as well.

MapReduce: Explained Simply as The Story of Sam

Couple of days back I made a presentation I made to explain the concept of MapReduce simply. I have attached the slides here for anyone interested. Please note, this doesn't include animations. If you want to get a better feeling feel free to download the original version of this MapReduce presentation.

.NET Naming Conventions

A good place to learn about .NET naming conventions.

Command Line: Relaxing Colors

I have been using a color theme for the command line in both Windows and Ubuntu for sometime now and it has been really comfortable for the eyes. So if you feel tired or bored using the white on black try these.

Background Color: #3A4237 (in RGB this is 58,66,55)
Text Color: White

It will give you this feeling of a good old chalk board. Here's a screen capture of how it looks.