Posts

Showing posts from 2014

Understanding Code Execution in Javascript

If you are doing any form of programming in Javascript then understanding its eventloop is very important. Any javascript engine has three kinds of memory models: Stack, which has the current function pointer an gets executed sequentially. Heap, this stores all the objects, functions basically anything that is initiated is stored here. Queue, all things to be executed is stored here and stack picks up tasks to do from the queue. So, to understand this further, when a function is getting executed its loaded in the stack and if it encounters any setTimeouts then it would be added in the queue and when the current stack gets empty then the new function to be executed from the queue. This code run will explain the process: console.log("Add this code to queue"); setTimeout(function() {                                                //This goes and sits in the queue.                            console.log("Running next event from the queue.");

Inbox From Google: Review

Image
In an another attempt to reorganise the way email communications work Google has come out with Inbox . Here, google has attempted to use machine learning and nlp for better organise email with a more intuitive UX. I believe this time they have utilised there learnings from Wave failure and have not attempted to change the core email component. But as people are slowly getting use to the timeline kind of UX, they have attempted to preserve that in email and top it up with the new features which they have always wanted to try but restricted by the plain UX of email, like "See pull request", "See flight status." Moreover, seems like this being designed specifically for phone devices. Lets see how useful this cool thing becomes. Never the less, please request an invite by sending a mail to inbox@google.com. I have requested mine and write a review as soon as I get an invite . I got an invite and have started using Inbox. I did not find it majorly innovati

Flatten a Binary Search Tree into a LinkedList in Java

package com.biplav.algorithms; public class FlattenBST { public static class TreeNode { public TreeNode left; public TreeNode right; public int value; public TreeNode(TreeNode left, TreeNode right, int value) { super(); this.left = left; this.right = right; this.value = value; } } public static class ListNode { public ListNode next; public int value; public ListNode(ListNode next, int value) { super(); this.next = next; this.value = value; } } public static ListNode flatten(TreeNode root) { ListNode left = root.left != null ? flatten(root.left) : null; ListNode base = new ListNode(null,root.value); ListNode right = root.right != null ? flatten(root.right) : null; base.next = right != null ? right : null; if(left == null) return base; else { ListNode home = left; while(left.next != null) left = left.next; left.next= base; return home; } } /* * 6 * 3 8 * 2 4 7 10 */

Alogrithm to sort a huge list of numbers in O(n) complexity in Java

A large file of numbers can be sorted in O(n) complexity by using a large bit array of the size same as the largest number in the list of numbers to be sorted. It can be done in Java using BitSet. package com.biplav.algorithms; import java.util.Arrays; import com.biplav.ds.BitArray; public class SortMillionNumbersWithGivenMax { //n+m public static int[] sort(int[] list, int max) { int[] sortedList = new int[list.length]; //BitSet bitSet = new BitSet(max); BitArray bitSet = new BitArray(max); for(int n:list) bitSet.set(n); //n int index=0; for(int i =0 ; i<=  max; i++) { //m if(bitSet.get(i)) { sortedList[index++] = i; } } return sortedList; } public static void main(String[] args) { int[] list = new int[]{1,2,3,4,5,9,15,18,25,7,11}; System.out.println(Arrays.toString(sort(list, 27))); } } I have used BitArray defined by me. Its implementation is: package com.biplav.ds; public class BitArray { private by

Fibonacci Series With Matrix Multiplication in Java with recursion.

Attempting to solve the fibonacci series using matrix multiplication applying divide and conquer to reduce the complexity to O(logN) package com.biplav.algorithms; public class FibonacciSeries { //2*2 matrix static int [][] multiply( int [][] X , int [][] Y ) { int [][] response = new int [2][2]; response [0][0] = X [0][0]* Y [0][0] + X [0][1]* Y [1][0]; response [0][1] = X [0][0]* Y [0][1] + X [0][1]* Y [1][1]; response [1][0] = X [1][0]* Y [0][0] + X [1][1]* Y [1][0]; response [1][1] = X [1][0]* Y [0][1] + X [1][1]* Y [1][1]; return response ; } static int [][] power( int [][] X , int N ) { if ( N == 1) { return X ; } else { int [][] X2 = power( X , N /2); return multiply( X2 , X2 ); } } public static int get( int n ) { int [][] X = new int [][] {{1,1},{1,0}}; return power( X , n )[0][0]; } public static void main(String[] args ) { System. out .println(get(4)); } }

Attendance.gov.in is First Modern looking site by Indian Government

Image
The http://attendance.gov.in/ is by far one of the first modern looking website by Indian Government. It uses bootstrap and Free bootstrap 2 theme sb-admin  http://startbootstrap.com/template-overviews/sb-admin/ Impressed by this sudden technology choices shift in the Indian Government websites, I decided to deep dive more on there technology stack and it was good to see there choices are improving. See the builtwith result here:  http://builtwith.com/attendance.gov.in Suprising there is no hint of the organisation created the site. But, I am happy to see the first mobile ready website of Indian Govt! :)

Flipkart Apology Email for bloopers on The Big Billion Day Sale.

Image
Here is the email which flipkart sent today apologising for bloopers on The Big Billion Day Sale. Apologies, from Flipkart regarding The Big Billion Day Unsubscribe Dear Customer,  Yesterday was a big day for us. And we really wanted it to be a great day for you. But at the end of the day, we know that your experience was less than pleasant. We did not live up to the promises we made and for that we are really and truly sorry.  It took enormous effort from everyone at Flipkart, many months of preparation and pushing our capabilities and systems to the limit to be able to create this day. We were looking at fulfilling the dreams of millions of Indian consumers through deals and offers we had painstakingly put together for months.  And though we saw unprecedented interest in our products and traffic like never before, we also realized that we were not adequately prepared for the sheer scale of the event. We didn't source enough products and deals in advance to ca

Tuning ActiveMQ for Performance

Recently I needed to optimize our ActiveMQ configuration for performance. Let me explain our deployment, we had a Jetty based Java app with GWT for the UI. We use gwt-ActiveMq with long polling to send messages to the UI. We were using activemq for everything, for out SM design, or async event listener architecture. We have no plans to scale from more then one box and for us the performance is critical. So things done were: Non-persisten messaging. Its atleast 20x faster. This can be achieved by: Set the NON_PERSISTENT message delivery flag on your MessageProducer. Set the  persistent=false  flag in the   element of the  Xml Configuration  or on the property  BrokerService . Remove the network. We used vm:// as transport so that the entire queue is in memory. No need to go over the network as our broker is embedded in the same context as the producer. You would have to change the broker address in the web.xml of the server as you won't get messages on the UI. Seria

How to configure google app engine when your site is deployed on Heroku and uses CNAME for naked domain?

When we configure Heroku App to our domain we configure it using CNAME records. This means two entry in our DNS records, one for naked domain or xyz.com and other for www.xyz.com. Now, if you want to receive mails over email ids configured with same domain with google apps, gmail then you need to configure MX records for xyz.com. Please see the previous post on this topic  . But there is a problem here, as CNAME record has higher priority, your domain wont return MX rule and always return CNAME rule and MX record validation would fail on the Google Domains page. So, how to solve it: Have A records entry if possible, with heroku this is  not possible. Remove CNAME record from DNS entry for xyz.com and configure google domain, naked redirect to www.xyz.com. Remove CNAME record from DNS entry for xyz.com and use Alias from Domian Control Panel. Remove CNAME record from DNS entry for xyz.com and use Domain Forwarding service for xyz.com to www.xyz.com on the Domain Control Pa

JS Event Bubbling Vs Event Capturing.

In Javascript or HTML DOM to be precise, there are two methods of event propagation, those are: Event Capturing. Event Bubbling. This defines the way event flow in case of multiple event listeners. Suppose you have a img  element inside a div element then and both have an event listener defined, then whose event listener should be called first? In case of event bubbling , the inner most elements event listener will be called first and it would be propagated out. So first the event listener of img will be called followed by the event listener of div element. In case of event capturing , the outer most elements event listener is called first followed by inner elements. So, in above example, div's event listener would be called first followed by inner most element that is img's event listener. The event propagation method can be selected by passing third optional boolean argument to addEventListener method of JS. The syntax is: addEventListner(event,event_h

Twitter typehead.js basic example with token field.

Image
A simple example for twitter typehead.js. The server should return an array of type: [ { name: "asas", value: "1234" }, { name: "asas", value: "121212" } ] Include following CSS and js: < link href = "http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel = "stylesheet" > < link href = "tokenfield-typeahead.css" rel = "stylesheet" > < link href = "bootstrap-tokenfield.css" rel = "stylesheet" >      < script src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js" ></ script >      < script src = "http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js" ></ script >      < script src = "http://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.10.4/typeahead.bundle.min.js" ></ script >       < script src = "bootstrap

Cafes around Indranagar and Koramangala for Work or Chill

Image
I found a nice discussion on PMIT with the requirements of Cafe suggestion in around Koramangala or Indranagar with following: 1. You spot less/same crowd lazing around the whole day.  2. You spot people hanging out with their books. 3. Bean bags around. 4. Lite music, bright light, decent Wi-Fi. 5. Nobody bothers to wake you up (even if you doze off   )  So I decided to put my own list: COSTA COFFEE, Koramangala Address: 80 feet, Road Koramangala. No bean bags. Has WIFI but unreliable. Less Crowded during Weekdays, very crowded during weekends. You can spots likes of Bansals of Flipkart fame at times here. Lite music bright light. One of my Fav to Work COFFEE ON CANVAS, Koramangala Address: 1st Cross, 80 Feet Main Rd, Koramangala 4 Block Bean Bags. Has WIFI but unreliable, Seems to get noisy on weekends and evenings. Light music, bright light. Good food. One of my Fav to Chill Atta Gallata, Koramangala Address: 134, 1st A Main Road KHB Co

Download playlist or songs from 8tracks.

Image
Many times I come across songs on 8tracks which I am not able to find from other sources. Little bit of googling and searching around, I didn't find a reliable way of doing the same. So, I decided to write a script for me to do that. For the benefit of larger audience, I have hosted it on one of my servers. You can access it from: http://beedio.com/8tracks.html Just go there an paste the 8tracks playlist url in the input field and press Fetch. Its a simple Js script which utilises the 8tracks apis to simulate the play of the song and allow a download or play link where the permissions are available. The urls to download or play individual songs would get listed. As it simulates play of song, so this script would take some time to go and fetch all the urls for download. Ideally, one should press fetch and then come back after few hours to the tab to see all the urls in the list. I have tried to follow the norms of 8tracks as much as possible, if any infringement of

Z-Index computed as auto even though it is set with a value on Chrome

This is a little known fact about z-index and usually missed.  If you read the article on w3schools then also you might miss this. It says: Note:  z-index only works on positioned elements (position:absolute, position:relative, or position:fixed). What this essentially mean that the element on which you are applying z-index, you need to have position attribute specified on it. Ideally use one of the above position values. If position attribute in the css style is not set z-index would be computed as auto. Google Chrome is quiet strict about this. Use a syntax like below. img  {      position:  relative;      z-index:  100; } Thats the reason, why many times we don't see z-index working on out elements.  Sources: http://www.w3schools.com/cssref/pr_pos_z-index.asp https://code.google.com/p/chromium/issues/detail?id=39426

Unique unconventional Arty Farty gift options in Bangalore

Image
A list of unique arty-farty  gift options in Bangalore. Caricature from BLOT STUDIO Contact them at: callofthecartoons@gmail.com , blotstudio.2010@gmail.com      Blot Studio is a one stop shop catering to all your creative needs delivered with utmost professionalism. Established in 2002, the studio has catered to various clients - corporates as well as individuals. We rely upon detailed ideation, artistic illustrations and a passionate approach in our efforts to achieve creative brillia nce. Furthermore, we seek to build a collaborative relationship with those with whom we work. At Blot Studio we strive to enhance our vision with every project we undertake. Our studio comprises of artists who are experienced, witty and creative. It shall always be our endeavor to provide quality work within available time frames and budgets. Charcoal Painting by PENCIL CHAAP Phone 9902857298 Email bindra.ashmeet @gmail.com Look at the world with