Thursday, June 28, 2012

Coin problem, how do you solve it?

My son has this typical homework problem.  If you have 10c, 20c and 1 dollar.  How many ways can you make $2?

I am pretty sure there should be a combination formula for it, but I forgot.  So I tried google but seems there is no formula

http://mathforum.org/library/drmath/view/57913.html

Is there really no formula?

Anyway so he started to make a table of combination.  I told him I better start to see if I can code it, maybe it would help him to verify if the table of combination is correct.  I came up w/ this.  I hope its right otherwise his table of combination is wrong and my code is wrong.

val twoDollars = 200

val coinsCombo = for ( oneDollar <- 0 to (twoDollars/100);
 twentyCents <- 0 to (twoDollars/20);
 tenCents <- 0 to (twoDollars/10);
 if (oneDollar * 100 + twentyCents * 20 + tenCents * 10 == twoDollars) 
) yield (oneDollar, twentyCents, tenCents)
 
coinsCombo foreach println

println(coinsCombo.size)

Explaining C++, Java and Scala to my son

Last night my son was asking about computers again.  I think the conversation came about, what language is on my screen.  I told him its Java and Scala.  I talked about what is assembly, C, C++ and Objective-C and how it relates to Scratch and Kojo.

He asked about why it was named as C++, then I showed him a Java typical loop which is based C where we increment a counter.

int c = 0;
while (c <= 10) {
 System.out.println(c);
 c++;
}



He asked how the loop will be done in Scala.  I started w/ a for comprehension, as he is familiar w/ loops on Scratch and Kojo.

for (c <- 0 to 10) {
 println(c)
}
Then he asked me about the for, <-, etc.

I then changed the code into

0 to 10 foreach println


His reaction "that is so awesome it's like english!"

Sunday, June 3, 2012

misconception about h2 in memory db

Seems sometimes I have seen h2 url connection as "jdbc:h2:mem", which is different from "jdbc:h2:mem:".  The former will create a persistent db in the current path db called mem, the later will create a in memory db.  The official docs explains in in more detail h2 docs.

A good way to know if its in memory or not is just check your current path.  If you have mem.h2.db and  mem.trace.db on the file system, then you are not using the in memory db.

Most of the time though more than 1 connection is needed.  The first reaction is to remove the extra ":", rather than looking at the docs.  The correct fix will be to name the memory db such as "jdbc:h2:mem:foo".  Or if you use JPA sometimes the abstraction is just to thick, and most developers forget how connections are managed.  A connection is closed per test, in general this is the last connection which leads to new db across a test suite.  So to keep the db alive through out the lifetime of the JVM just add ";DB_CLOSE_DELAY=-1"

Sunday, May 27, 2012

Short comparison of Ember.js and GWT

Its been a while since I posted anything here, really hard to make continuous post.  Well try again for the n-th time.

I have been using Google Web Toolkit (GWT) for while, as this is what we use at work.  Its pretty good and has lots of positives, however it also has it's own negatives.

Lets go with the positives first:
  • For some people that knows Java they don't have to learn JS to develop full featured web apps
  • Existing java tooling works on GWT projects.  Re-factoring, IDE support, etc.
  • Performance of GWT is still the best among any JS frameworks
  • Pretty good documentation and examples
Now for the negatives:
  • Not learning JS, or some people will need to learn Java to develop JS code  
  • GWT seems to be on its own eco system outside of the wider JS eco system
  • Sometimes hard to control the html, making iterative changes with a web designer significantly more harder.  This is even using ui binder
  • REST/JSON still seems to be 2nd citizen in GWT.  There is autobeans in GWT now, however still lots of plumbing to do or use 3rd party library.
Using GWT to architect a full blown web app is great.  Using established patterns such as Model View Presenter (MVP), a cousin of the more popular Model View Controller (MVC) pattern.  For the past few years there has been a lot of JS MVC frameworks coming out.  A quick look of the Todo MVC project will give us a lot of different implementation.  (The GWT implementation hasn't been pulled yet).  One of the JS MVC framework that's been gathering a bit of attention is Ember.js.

I gave ember.js a try over the weekend, its been initially a bit frustrating.  Like always when you learn something new, sometimes we just need to unlearn our own old learnings.  Ember documentation is a bit confusing, as its formatted on how to use the different features.  Some of the materials I have found to helpful in getting me running are:


After things started to fall in place, ember.js seems to be pretty straight forward.  Compared to GWT there is really significantly less abstractions.  As ember.js really tries get you in the middle of JS, HTML and CSS.  I am still unsure on how unit testing will be done, haven't look at ember data yet, etc.  However with this initial code:

var App = Em.Application.create();

App.Plant = Ember.Object.extend({
 id: 0,
 name: null,
 description: null
});

App.PlantsView = Em.View.extend({
 plantsBinding: 'App.PlantsController.content'
});


App.PlantsController = Em.ArrayProxy.create({
 content: [],
 
 init: function() {
  var me = this;
  var url = 'http://localhost/plants';
  $.getJSON(url,function(data){
    me.set('content', []);
    $(data.plants).each(function(index,value){
        var plant = App.Plant.create({
            id: value.id,
            name: value.name,
            description: value.description,
        });
        me.pushObject(plant);
    });
  });
 }
 
});

  

As you can see its really straight forward to get data from a rest/json server.  I have an equivalent GWT project, but its too big to post here.  However the equivalent GWT functionality would include.  A model class, a ui binder file, an entry point class, a presenter interface, a view interface, a presenter implementation, a view implementation, http get code, etc.

This doesn't mean longer code is worse code, I am not in the camp of Java is bad too much typing.  I still use Java, although less these days as Scala has pretty much filled the space.  Even with the above code, one can see JS short coming of not having name spaces and no real support for OOP.  However one can see also there is a lot less boiler plate and more natural integration with JS and HTML.  When I mean less boiler, I do not mean about Java's verbose code but the bindings are straight forward.  More natural integration, I just used jQuery's getJSON function straight out of the box and handlebars' template inside html.  GWT has JSNI to call jQuery, but that is less natural and not used often.

Ember.js seems to be promising for organizing web apps.  Then coupled with other mature JS frameworks like jQuery, I think it may make GWT a bit heavy for some use cases.  I am still thinking of looking at Scala+GWT, aside from getting to use Scala to do JS I don't see a lot of difference from normal GWT.

Sunday, September 18, 2011

Verify TRIM support on Linux

Wasted my time today puzzling why the trim support of my ssd does not work, or I thought it wasn't but was working alright. When you google "linux trim verify" the link below and most people link to the site below. Apparently the tests is not fully correct. Most of the time it works for others, but not me. Maybe it has to do that I formatted my ext4 w/ -E stripe-width=128
Anyway this is a better test I found, but I can't seem to see the link anymore. Using my bash history for reference.
dd if=/dev/urandom of=tmpfile bs=1M count=10 && sync
hdparm --fibmap tmpfile
hdparm --read-sector [address between begin_LBA and end_LBA of previous command] # expecting random numbers here
rm tmpfile && sync && sleep 120
hdparm --read-sector [address between begin_LBA and end_LBA of previous command] # expecting zeroes
The real key is the sector address is somewhere in between and NOT the start sector, as its possible the trim command will not set things to zeroes if files overlap. As it had happened to me 9 out of 10 tries using the test below I get random numbers still. The test above I get zeroes consistently.

Manual setup of UEFI, GPT and GRUB2

I recently got a Crucial m4.  I then have to transfer my existing HDD on it.  Its easier to do a clean install of Oneiric and it would pretty much do UEFI + GPT + GRUB2 for you.  However if you want to manually transfer everything I did the following steps:


  • Boot on Oneiric live CD or another boot this that will boot in UEFI mode. Its important that boot is on UEFI mode and not BIOS mode
  • Go to a shell and install gdisk as we want to partition the SSD w/ GPT and not MBR. fdisk only supports MBR
    apt-get install gdisk
    
  • Partition a EFI system partition.  This would hold grub2 files later and other boot loaders if needed.  I partitioned mine on sda1 as type EF00 (EFI System) w/ the size of 200mb.  Yes a bit bigger than what is needed however most people recommend 200mb as some boot loaders needs a bigger space.
  • Format the partitions.  The EFI System partition is formatted as vfat, as per EFI spec.
    mkfs.vat /dev/sda1
    mkswap /dev/sda2
    mkfs.ext4 -E stripe-width=128 /dev/sda3
    mkfs.ext4 -E stripe-width=128 /dev/sda4
    
    I put in a stripe width of 128 k as there are some recommendation this is optimal for SSD. However test suggest that your mileage may vary.
  • Mount and create the dirs on EFI system partition
    mkdir /boot/efi
    mount /dev/sda1 /boot/efi
    mkdir -p /boot/efi/efi
  • Install grub efi
    apt-get install grub-efi-amd64
    
  • Setup grub on the EFI system partition
    modprobe dm-mod
    grub-install --boot-directory=/boot/efi/efi --bootloader-id=GRUB2 --no-floppy --recheck
    cp /usr/share/grub/unicode.pf2 /boot/efi/efi/grub/
    cp /boot/grub/grub.cfg /boot/efi/efi/grub/grub.cfg
    
    I copied my old grub.cfg and update the UUID to point to the new SSD
  • Setup the EFI boot entry
    modprobe efivars
    efibootmgr --create --gpt --disk /dev/sda --part 1 --write-signature --label "GRUB2" --loader "\\EFI\\grub\\grub.efi"
    
    GRUB2 is the entry that would appear on the EFI bootloader.
The following resource helped me a lot
https://help.ubuntu.com/community/UEFIBooting#Install_GRUB2_in_.28U.29EFI_systems

I am still working out on the graphics output,  This would only give you a text based grub.

Saturday, January 8, 2011

HP 6910p 3G on linux

I got the 3G hs2300 on the 6910p running before, but it was a long time ago. I would need internet access again on the road. I detached the battery and put in the sim card to test. It seems modem can't be detected. Doesn't appear on lsusb, not much clue on dmesg, I verified that its turned on bios, started to look around forums, tried various modprobe, was going to start using usb-modeswitch.

I then decided to reboot to Windows after several years of not going there. HP wireless assistant tells that its disabled as that the gsm sim is accessible and suggest to close or put the battery back. I put the battery back, HP wireless assistant changes status to still disabled but now suggest to fix via Device Manager. No luck, Device Manager can't find drivers, etc. So did not want to spend it running on Windows and maybe putting the battery back is enough to get it running on Linux again.

Reboot to linux, network manager picks it up. Nothing to setup aside from APN, etc. No driver install, etc. Just put the battery back. Hopefully this post saves someone a few hours diagnosing the problem. Just put the battery back and linux should see the hs2300 3G modem.