Recursion is Natural
“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
Few places to note if you are having trouble connecting to Blogger with Live Writer as I did.
- Blog URL: Don’t forget to use https instead of http
- Username: Remember to add @gmail.com to your user id
- 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
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
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.
Copy Path to Clipboard : Another Life Saver
Note. It works fine with Windows 7 as well.
ReSharper: Life Saver for Visual Studio Users
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 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
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
WCF Hosting with IIS7
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
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
Command Line: Relaxing Colors
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.