Skip to main content

Posts

Showing posts from February, 2014

Ruby implementation of the famous Knuth–Morris–Pratt Algorithm

today just decided to write the   Knuth–Morris–Pratt string searching algorithm  , implementation in ruby.  As you can see that the algorithm is divided into two parts , one is the preprocess , which is the crux of the Algo and the actual string searching using the preprocessed metadata. The preprocess function is run only on the patterns, and it calculates π[i] = the longest proper prefix of pattern[0..i] , which is also a suffix of pattern[0..i]. e.g. if the pattern you are searching for is  P = [ ‘A’,'B’,'A’,'B’,'A’,'C’,'A’] the π array will look like π = [ 0,0,1,2,3,0,1] which shows that longest running prefix of the pattern is ABA . Buy doing the above calculations the algo saves itself some of the comparisons which it had to do in the naive , brute force approach. if you get the preprocessing function then the rest of the things are straight forward , the algorithm is intelligent enough to understand that if next char in the seq does...

HATEOAS - Hypermedia as the Engine of Application State

Just came across this term HATEOAS , which some of my team members were using in describing what we did in our previous project wrt to REST. My Last project was about building an e-commerce platform for one of the largest book publisher and distributor in Latin America.As part of that project we had thought to use the LEVEL-3 for rest communication which is using REST as a hypermedia controls.  You can read about different levels of REST at this Martin Fowler’s Blog  . When we said we wanted to use REST as hypermedia controls , the main idea behind this was to be able to navigate the result of a REST call. For Example if there is a product catalogue call which looks like GET /products/123456 So It would return  As you can see that apart from the the properties of the product 123456 it also has a links array. This links array contains the the link description in rel and the location of that document. so essentially what i could do is parse the JSON response , and then look for wha...

The dam which has slowed down Earth's rotation!

Link: The dam which has slowed down Earth's rotation! China'™s Three Gorges Dam is world'€™s largest hydroelectric station. First envisioned in 1919 and finally completed in 2012, the dam spans the Yangtze River and measures 2.3 kilometers (1.4 miles) long and 185 meters (607 feet) tall. Almost 30 million cubic meters (just over 35 million cubic yard…

Using RSVP for multiple ajax promises

So i was working on a bug in my project the other day wherein the requirement so we had to make multiple ajax calls to our backend and then once when “all were done” we had to trigger the success callback, even if any one of the calls fails we do not trigger the success callback. And the asynchronus nature of the the ajax calls make it more difficult. So to solve this problem we had a count variable with a promise, with every callback we were incrementing the callback and then when the count was equal to the number of calls then we would resolve the deffered object which would fire the success callback. This approach worked but was difficult to understand and debug. To simplify this a bit we used the RSVP which was bundled with the ember source. so instead of maintaining the count and then manually resolving the promise we used the Ember.RSVP.all(<array to observe>, function(){ console.log(“success callback”)}) so it resolves the success callback once all t...

Segregation of responsibilty with EmberJS router actions and store

Recently we upgraded our application to ember 1.3.0 and ember-data 1.0.0-beat4.Though it was a challenging task , since most of the api’s , conventions and names had changed so it involved in a lot of effort ( the upgrade deserves a separate blog post ).The one of the many things which has changed in the ember-data and ember 1.0 is the actions hash in the routes . So routes can now have actions which look something like. You can call these actions from Handelbars using {{ my-button action= “ showUser”}} or  from the view code using  this.sendAction( ’ action’, param1, param2); The events gets bubble up from the { Handebars } -> View -> Route  This picture form Emberjs docs explains it in a better way.  you can read  http://emberjs.com/guides/templates/actions/#toc_action-bubbling  for more details about action bubbling. The other change which the ember-data library brings about is making store the centre of all the interactions to the server , Store ...