Posts

Gson's missing get element function - Part II

The Gson missing recursive get function - Review. A while back, probably more than a year ago, I wrote an article Gson's Missing recursive get function  where I discussed the shortcomings of the current gson get function, whereby it only traverses top-level entry key for the search key. In this article I'll introduce a new functionality that allows for even deeper traversal of same name nodes until a primitive value is found (if needed). {      "result"   :   {          "total_items"   :   1 ,          "total_pages"   :   1 ,          "items_per_page"   :   25 ,          "current_page"   :   1 ,          "items"   :   {                  "username"   :   "test" ,                  "items"   :  {                   items: "12345"                 },                  "secret"   :   "" ,                  "application"   :

Simple Introduction to Map Reduce

Image
The term MapReduce actually is a compound word, which simply is a programming model/architecture used for processing large data sets in parallel, normally in a distributed setting. Image Sourced from : https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkIa1Hh1GzJ-2yyiUOMsSuAptPuXWcI6blhfCE0gXZ4zCggqQKnqkYmQwuJOTZOwDXsif5UWfrpnCmMKoSGTzk8wLgvJt1MeZbr5YBMh5pgnIeNcHyM5frpDW35UcYNUgjgSrMKBcY9oVr/s1600/WordCountFlow.JPG The above figure shows the typical phase in a MapReduce program. Phase 1 : In the initial phase the file contents are been read by the program into an InputStream. Phase 2 : In  this phase  (Some applications will combine phase 2 & 3 (mapping)) each line of the input file is read into a separate mapper instance that will be executed in parallel, sometimes in a distributed setting. Phase 3 : In this phase e ach line from the previous phase is then fed into a Map function that tokenize each term/data item  and thereby converting it into

Awesome Leap Year Calculation in JavaScript

What is a Leap Year (Wikipedia Definition) A  leap year  (or  intercalary  or  bissextile year ) is a year containing one additional day (or, in the case of  lunisolar calendars , a month) in order to keep the  calendar year  synchronized with the  astronomical  or  seasonal year . Because seasons and astronomical events do not repeat in a  whole number  of days, a calendar that had the same number of days in each year would, over time, drift with respect to the event it was supposed to track. By occasionally inserting (or  intercalating ) an additional day or month into the year, the drift can be corrected. A year that is  not  a leap year is called a  common year .   [1] Leap Year Algorithm Below a simple algorithm for determining a leap year is introduced. if year is divisible by 400 then is_leap_year else if year is divisible by 100 then not_leap_year else if year is divisible by 4 then is_leap_year else not_leap_year How to Determi

A better JavaScript typeof function

Often times during our JavaScript development we may need to know the type of an "object" in JavaScript, using the  typeof functionality provided by JavaScript works sometimes and other times it might fail. Some examples are provided below. typeof 2               : returns "number" typeof ""           : returns "string" typeof function(){}   : returns "function" typeof /\d/                 : returns "object"                     // fail !!! this is actually a regexp I have a written a simple replacement function for typeof that pretty much returns the same results, but it's more robust and always returns the correct value var itypeof = function (val) {     return  Object.prototype.toString.call(val).replace(/(\[|object|\s|\])/g,"").toLowerCase() } itypeof(2)                          : returns "number" itypeof ("")                        : returns "string" itypeof (

Pseudo-Random UUID Generation with mask support

There are a lot of cases where we may want to generate a pseudo- random universally unique identifier ( UUID ) , sometimes called GUID . I previously wrote an article on creating Pseudo Random numbers in JavaScript, which generates a uuid of length X where x is a numeric value supplied. http://pragmatic-coding.blogspot.com/2012/01/javascript-pseudo-random-id-generator.html The problem with the above generator is that is does not allow developers the flexibility of specifying the format of the uuid as shown below: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxx0' 'xxxx-xxxx-xxxx-4xxx-yxxx-xxxxxxxxxxx' 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxx-xxxxxx' Above I’ve shown you 3 different uuid formats that 3 different developers wanted, so an algorithm had to be devised that allow developers the flexibility of changing the data formats. One other thing to note about the above is the x values, which represents the values you want to be randomly generated. All o

JavaScript Module Pattern: 2 Forms

JavaScript Module Pattern: 2 Forms The module pattern is a common JavaScript coding pattern. It's generally well understood, but there are a number of advanced uses that have not gotten a lot of attention. In this article, I'll review the basics and cover some truly remarkable advanced topics, including one which I think is original. In the following guide I will explain two different variation of module pattern that I use during development, namely: Loose Augmentation Function Import Augmentation One limitation of the module pattern so far is that the entire module must be in one file. Anyone who has worked in a large code-base understands the value of splitting among multiple files. Luckily, we have a nice solution to  augment modules . First, we import the module, then we add properties, then we export it. Here's an example, augmenting our  MODULAR  from above: // global object being added to the user space var modular = ( function (