Author Archives: Nelson – Page 3

Introduction to memoization with Javascript

Memoization is a function optimization technique used to avoid remaking calculations in subsequent function calls.  The classic example, which we’ll demonstrate here, is the factorial function.  5! = 5 * 4 * 3 * 2 * 1… factorials are recursive in nature if we give it some thought.  5! = 5 * 4! and 4! = 4 * 3! and so on.  By recognizing this pattern, we can also recognize that if we calculate 5! and save the results, then we should not have to recalculate it when we do 7!.  Instead of calculating 7! by 7 * 6 * 5 * 4 * 3 * 2 * 1, we’ll calculate it by 7 * 6 * 5!.

We’ll first examine the naive approach and then the memoization method.  All in Javascript, and all with code samples…
Read more »

Maintain a running list of your external IP address

At home, I have a consumer level cable internet service.  I actually do not know if I have a dynamic IP address or a static IP address because I haven’t really cared enough to ask my ISP.  I would assume that I have a dynamic IP address since most ISPs charge a premium for a static IP address, but since I don’t know for sure I thought I would find out for myself.

Using http://whatismyip.com ‘s “automation” page, Perl, and cron, I am going to maintain a list of my external IP addresses.  Not only will this answer the original question, but I can also gather some additional information, such as how often my IP address changes and whether or not I can force my IP address to change by disconnecting my modem :).

Read more »

Fixing line breaks in HL7 messages in Mirth Connect

Anyone who has worked in the world of healthcare integration or with HL7 knows that if you have seen one HL7 message, you’ve seen one HL7 message.  Now, a common problem with some source systems is that a line break will sneak in the middle of a segment, rendering the whole message invalid.  How many times have you seen this message?

MSH|^~&|DDTEK LAB|ELAB-1|DDTEK OE|BLDG14|200502150930||ORU^R01^ORU_R01|CTRL-9876|P|2.4
PID|||010-11-1111||Estherhaus^Eva^E^^^^L|Smith|19720520|F|||256 Sherwood Forest Dr.^^Baton Rouge^LA^70809||(225)334-5232|(225)752-1213||||AC010111111||76-B4335^LA^20070520
OBR|1|948642^DDTEK OE|917363^DDTEK LAB|1554-5^GLUCOSE|||200502150730|||||||||020-22-2222^Levin-Epstein^Anna^^^^MD^^Micro-Managed
Health Associates|||||||||F|||||||030-33-3333&Honeywell&Carson&&&&MD
OBX|1|SN|1554-5^GLUCOSE^^^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^175|mg/dl|70_105|H|||F

Notice the “Health Associates” segment? Obviously, this segment invalidates the message.  When this message is sent through a Mirth channel that expects incoming HL7, the message will error out when Mirth tries to parse it.  Here is how we can fix that.

Read more »

A visual comparison of the good and bad parts of Javascript

Javascript can be very expressive and powerful in the right hands.  It isn’t without it’s faults, though.  For an actual dissection of both the good parts and the bad parts of the language, I recommend Douglas Crockford’s book, Javascript: The Good Parts.  In the mean time, take a look at this picture.  The difference in page count between the Javascript reference and The Good Parts is pretty accurate :)

Zip code library in CodeIgniter

Recently, a client wanted a member search for his website that included a search by zip code. The ‘easy’ solution would be to implement the search as a LIKE statement in SQL, but the solution is inaccurate, and I like the ‘good’ solutions over the ‘easy’ ones. I looked for a CodeIgniter library that would give me the functionality for which I wanted to implement, but no such thing existed. Then, I came across Micah Carrick’s native PHP library which gave the exact functionality I was looking for. I did end up using his library, but not without modification: I’ve ported the native library to a CodeIgniter library. This is where you can get it.

Read more »

“Presto” math trick in Java

According to Futility Closet, if you start with a 3 digit and place it next to the same number to form a 6 digit number, you can divide the 6 digit number by 7, 11, and then 13 and you will end up with the original 3 digit number and no remainders. For example, by taking the number 412 and making it 412412 and then doing the divisions, you will end up with 412. I wrote a small program in Java to test it.

Read more »

jQuery change event on checkbox

A lot of times, you’ll want to call a javascript function when a checkbox is changed from unchecked to check and vice versa. Up until the release of jQuery 1.4, the obvious way to do it (the jQuery .change() event) did not work properly across all versions of Internet Explorer. Instead, you had to check for the .click() event. This was an accessibility problem because it did not fire when a user changed a checkbox with the space bar instead of by clicking. Well, now we can rejoice. The following code snippet works exactly how you think it would across browsers, including IE.

Read more »

Recursive Collatz conjecture in PHP

I stumbled across a blog post by Brie Gordon about the Collatz conjecture, so naturally I had to Wikipedia it. The simple story is that for any given integer, if it is even you divide it by 2 and if it is odd you multiply it by 3 and add 1. You continue this pattern with the returned numbers, and eventually the number will reach 1. So, to kill some time, I wrote a recursive version in PHP.  Here it is:

function collatz($num)
{   
  if($num == 1)
  {
    echo "<p style='color: red'>" . $num . "</p>n";
    return;
  }
  if($num % 2 == 0) //even
  {
    echo "<p style='color: green'>" . $num . "</p>n";
    return collatz($num / 2);
  }
  else
  {
    echo "<p style='color: red'>" . $num . "</p>n";
    return collatz(3 * $num + 1);
  }
}

Creating a simple, extensible CodeIgniter authentication library

While it is true that there are a whole slew of third party CodeIgniter authentication systems, there are a few compelling reasons to write your own. In my own experience, third party authentication systems are too feature-rich and complex for a particular application or they are not currently being supported, which means they may not work with newer versions of CI or may have outstanding functional and security bugs. Instead of tackling a new codebase to fight these issues, you may be better off rolling your own solution. This tutorial is for those of you who may need some help starting out.
Read more »

External anchors in CodeIgniter

When using CodeIgniter, I almost never use bare anchor elements to create links. Instead, I use the anchor(“controller/function”, “text”) function in the url helper. At first glance, however, it appears that you cannot use the anchor() function to link to external urls and thus have to write the anchor html element yourself.

This is not true. Instead of using the bare html element, I decided that I would extend the helper to include the functionality I wanted. When I looked into the helper, though, it appears that the CodeIgniter team had already done it! However, as of CI 1.7.2, the functionality is undocumented. Luckily, it is easy to do.

<?php echo anchor("http://www.codeigniter.com", "CodeIgniter"); ?>

Read more »