Skip to main content

Posts

Showing posts from 2013

Tail recursion in Scala

Today I learned that tail recursion optimization can really solve the classic problem of stack overflow in recursive functions that depend on data size this I saw by writing a simple sum function in Scala that sums up a list val x = (1 to 3).toList // simple recursive function def sum(x: List[Int]): Int = {    if (x.isEmpty) 0   else x.head + sum(x.tail) } ok so far so good, this works, and return 6 for the simple list of size 3. But change the 3 to 300000 and you will see a stackOverFlow in the Scala workspace (you save the workspace and Scala will re compute). This is because a new frame is added to the stack for each recursive call and soon that exceeds the stack limit. How do you solve this? Tail recursion comes to the rescue. The term applies to functions whose last call is actually a self call, without any other computations. Scala compiler will internally then convert such functions into a while loop with goto, instead of pure recursion, hence avoiding

Plot csv data using D3.js

I tried my first D3.js data visualization using csv data source My 'graph' would plot circles, where the x position of the circle would represent the value of the second column (file size) in my csv. The larger files would be plotted to the right. I posted my example d3csv.html at  https://github.com/binodpanta/webvisualizer I should be able to do some more interesting things with this, such as realtime monitor files. Enjoy!

Using Sublime Text build system to open your files in your favorite Mac app

A quick hack on Sublime text when working on Macs is to get the editor to invoke the 'open' command so that you can hit command+B to 'open' the file in your favorite app. My favorite use for it is when editing html files, i want it to open in my favorite browser. On a Mac, 'open' is the equivalent of the default file association in Windows, such as when you click on a text file, you can have it open in your favorite editor. In sublime, go to Tools > Build System Select New build system In the file that opens type this in into the pre-filled section and save the file with a name you want, such as 'open.sublime-build': { "cmd": ["open", "$file"] } Then go back to the file you were editing, Tools > Build System. Now select your newly saved build system. Now every time you hit command + B you get the file to open on Chrome (which is what my Mac is setup by default to open html files with)! Obvi

Using "watch" to monitor a command's output in Linux

I recently learned about the 'screen' command on Linux from one of my friends (and now I am hooked). Now I have learned to use another Linux command 'watch' to constantly run a command and watch its output. I was running into an issue when I ran a test session I would keep getting an error about disk space being filled up. So I thought to monitor disk usage as I ran my stuff. First, I setup two screens on my remote terminal (putty in my case) and ran screen ctrl+A S to create a split screen ctrl+A tab to move to the second view ctrl+A c to create a new window now that i have two screens On screen 1, run 'watch -n 2 df /tmp' to run the df command every two seconds against /tmp, and now i have a view that updates every 2 seconds in my screen1 On screen2 (switch using Ctrl+A tab), i run my command There you go, I have not yet found my source of trouble, but the technique still proved its worth since I could see the disk space update as I ran my com

My first encounter with Lambda functions in Python

Lambda functions in Python Here is a very simple  lambda function , this example is one of creating a polynomial function with given coefficients, and storing that as a lambda function in a variable 'f' that can now be used as a function, cool! # This returns a function that represents a polynomial # Input is a dictionary with a,b,c being required keys def polyf(**coeffs): """Returns a lambda function""" return lambda x: coeffs['a']*x*x + coeffs['b']*x+coeffs['c'] # now returns a lambda function that will evaluate 2nd order polynomial with coeffs a, b, c when called f = polyf(a=1,b=2,c=3) print "Help doc for this function = ", polyf.__doc__ # Try using that lambda function now, Done! print f(0.1) print f(0)

Unit testing code that uses environment variables and system properties with fakes

I did not exactly learn this today, but I am sharing it as I have found it extremely useful when unit testing code that depends on environment or system property settings. While I am using Java as an example, the general concepts apply any where. Problem : You have a piece of code you are unit testing that uses settings from env variables or system properties passed to the VM (System.getProperty), but you don't want the tests to be affected by the 'real' environment or system properties in the VM. So, your unit tests should not get different results or fail when the real environment changes. Solution : There are several. But the most straightforward is to use a mocking library to mock out the environment or fake it out, whatever your prefer. You can create a fake using a library like EasyMock, PowerMock etc. This I won't discuss in this post, since there are numerous articles for that. Or you can write a simple class that acts as a proxy, using the proxy pattern