Tortured Silence

the cars honk
the lights flash
the refuse stinks
the flies hover
the mosquitoes bite

but he does not know
for he is

where the mind is afraid
and the head must not fly

where nothing is free

where the world is fractured and broken by the glory of barriers

where words have forgotten the depth of truth

where tireless defrauding stretches its arms towards perfection

where clear streams of reason are but superfluous and do not lead anywhere

where the mind is led forward – by the overarching ‘me’ – into ever-malevolent thought and action

what a heaven of freedom, my Father, into which my country awakes

[This poem is about an addict lost in a drug-induced slumber full of nightmares. It is largely a modification – gasp – of what is arguably Rabindranath Tagore’s most famous poem. Oh, and the picture is one of my favorite Edward Gorey works.]

Tutorial: Using a Button To Switch Pages in a ViewPager [Android Studio]

OK, so Android Studio (like its ancient forefather Eclipse) supports the ViewPager navigation type, which allows you to set up multi-page navigation without any work. However, I’ve noticed that many people don’t quite seem to get the hang of swiping and give up on trying to switch pages. It’s best to provide an alternate form of navigation for them – in the form of the ubiquitous button, linked to a function that switches the active page. As an example, I’m going to make a function that makes the ViewPager jump to the second page.

  1. In the little fragment_main_dummy.xml file hidden away in src/main/java/res/layout (or whatever the name of the file – in which you want the button to be – is), add a new Button component (using the Design view if you like that). Switch to Code view and locate the Button component.before
  2. At the end of the Button component, before the ending /> press Enter and add this line of code:
    android:onClick="jumpToPage"

    after

  3. Save the file. Now, the last part of this can be done in two ways:
    1. First method: Put your cursor somewhere on the android:onClick line and press Alt-Enter (Option-Enter on Macs). Select “Create onClick event handler”.
      onclick
      Select MainActivity when asked which activity you want to create the onClick handler in.choose_activity
      It makes a nice function called jumpToPage in your main Activity class.
      fun
      Place your cursor just after the opening brace and press enter. Add this line to the function body:
    2. mViewPager.setCurrentItem(1);

      Save the file.

    3. Second method: Switch to your MainActivity.java file. At the end of the onCreateOptionsMenu function, type this in (or copy-paste if you’re lazy):
      public void jumpToPage(View view) {
          mViewPager.setCurrentItem(1);
      }
      

      Save the file.

  4. This is what you should have in MainActivity.java now:
    jmpfun

You’re done!

[In response to a friend’s request, who asked for the pictures :D]

A Simple Regex for Rails Email Validation

Here’s my quick-and-dirty Rails email regex:

/^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i

Declare the regex somewhere near the beginning of your User model, like so:

EMAIL_REGEX = /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i

A lot of code is yet to be written. Onward!

[Check this regex out on Rubular. It’s the best Rails regex tool in history!]

Remember those neighbours who turned a 3KHz sound on? Well, they did it again yesterday.

Enter my little 2.1 setup with a not-so-little subwoofer playing the live performance of Fear of the Dark at full volume.
They won’t do it again.

Find The First 1000-digit Fibonacci Number [Project Euler Problem 25]

We all know how the Fibonacci numbers are defined: the nth Fibonacci number is equal to the sum of the two before it, with initial conditions F1 = F2 = 1. Translated into my mathematical language, that is:

Fn = Fn – 1 + Fn – 2

This is an example of a recurrence relation.

Using the theory of generating functions we can turn this recurrence relation into a “closed-form” or explicit formula:Image

[This book taught me generating functions – it’s awesome! You can buy it from that link and earn me a coffee :D]

Here, that odd-looking symbol φ (shame on you) is the magical Golden Ratio (read up on it!), equal to:

Image

Oh, another thing. The number of digits in a number is equal to the ceiling of its logarithm in base 10Here’s why.

Manipulating these leads us to an expression for the index of the first Fibonacci number with d digits:

Fn = (d − 1 + log10(5) /2) / log10(φ)

And with that, here’s the two lines of code! (Code!) (OMG code!)

from math import log10, sqrt, ceil
print(int(ceil((999 + log10(5)/2.0) /
          log10((1 + sqrt(5))/2.0))))

And that’s how I became the 70181st person to solve this problem!

[image credit: Wikimedia]

Why the GIL is a Tough Nut to Crack

Jeff Knupp on http://www.jeffknupp.com/blog/2012/03/31/pythons-hardest-problem/

After holding off for a long time (due to the scourge of half-yearly exams), I’ve finally started learning Scheme!

“A dreamer is one who can only find his way by moonlight, and his punishment is that he sees the dawn before the rest of the world.”

Oscar Wilde

A nice Haskell wallpaper

A nice Haskell wallpaper

I look at this when I rock Haskell. Win :)

Trying to survive an incessant 3KHz sound, and waiting to buy The Road To Reality.