Life Codecs Ruminations. Reflections. Refractions. Code.

December 9, 2012

Oracle JDK on Debian

Filed under: how to,software dev — Tags: , — Kamal Advani @ 21:56

Just came across JavaPackage in Debian. This allows you to cleanly replace OpenJDK with a complying alternative systemwide, the Debian Way. I was only interested in the Oracle JDK, and that worked a treat, not sure about other JDKs. In essence:

  1. Install the JavaPackage package using apt[itude]
  2. Download the .tar.gz JDK package from Oracle
  3. Run make-jpkg, installed as part of JavaPackage, against it (not as root); this produces a Debian package of the tarball
  4. Install the Debian package using dpkg
  5. Finally, use update-java-alternatives to update various Java-related symlinks systemwide. You can choose to only update a subset (e.g. just use the Oracle JDK for the browser plugins, but nothing else), or go the whole hog — all JDK binaries will then be from the Oracle JDK.

The wiki link has examples and details.

PS. I really miss saying “Sun JDK”.

Android with Ivy and Ant

Filed under: how to,software dev — Tags: , — Kamal Advani @ 20:57

Created this template project because I wanted proper dependency management when developing for Android. I chose Ivy because it was the least intrusive, and Android officially supports Ant, which Ivy cleanly integrates with, rather than Maven.

Honestly, making Eclipse and Maven play with each other seems to be a really unnecessary time-sink for most things, it’s stupid. mvn eclipse:eclipse, in my opinion, is a much better approach than m2eclipse or similar “heavy” integrating plugins. Unfortunately, I could not find a similar thing for ADT. Anyway, Ivy is quite cool as a pure dependency manager once you (mostly) get your head around “configurations”.

Anyway, hopefully it is useful to others besides me.

July 22, 2012

Javascript Core Concepts

Filed under: how to,software dev — Tags: — Kamal Advani @ 19:12

Alex Russell’s Google I/O 2011 Learning to Love Javascript video is probably the best Javascript core concepts videos I have seen to date. While I still find the boilerplate required to model what other languages have built-in annoying, the elegance and hackery of building sophisticated things from a small, composable featureset is very neat.

I also love that syntactic sugar is acknowledged to be of use to avoid this boilerplate and unnecessary overload of existing keywords. 60000 incompatible ways to do modules has always been one of my pet peeves for something so fundamental. Javascript in this sense has a case of The Lisp Curse. I do understand that essentially the JS people wish to avoid changing syntax for compatibility, however I think at some point this reticence gets a bit much. Given the alternative browser languages coming up, CoffeeScript being the most mainstream, followed by the Dart initiative, I’d say we have hit that point. Admittedly, Dart is somewhat different, apart from syntactic constraints, it is also attempting to make semantic changes for performance optimisation purposes. CoffeeScript is much more about solving the problem at hand.

“De-Sugaring” as discussed, is the way to go to keep the language core small while allowing more syntactic support. Something like CoffeeScript essentially does this — people should still learn the Javascript core, IMO, before learning CoffeeScript to appreciate the elegance of both languages.

More generally, I had an aha-moment when closures were restated as behaviour that hold on to some state — an inversion of how most of us OO-language users think. This fits very well with my renewed understanding of closures as “closing over” a bunch of state — closed over vars do not go out of scope while the closure is alive. That local vars went out of scope once the function/method completed in many languages, in retrospect, has been the main thing that stopped me from fully “getting” closures when I first looked at it. Then again, it is a subtlety that I had failed to fully appreciate even in Java which does have limited closure support via returning class instances from functions that can reference local vars, so long as they are marked final. By implication, the objects referenced by those vars do not go out of scope whilst the class instance returned holding on to them is alive.

Second satori: As stated above, “the elegance and hackery of building sophisticated things from a small, composable featureset is very neat”, but the syntax is annoying — a language with macro facilities (various LISPs), provides both this composable core, and a unified, extensible syntax. Pretty amazing. In case of LISP, non-syntax really. If only I could get used to the gazillion parentheses of LISP.

Somewhat less elegant than macros (but I love the idea nevertheless) is Groovy‘s parser/compiler hooks which essentially allow you to transform your own syntax/constructs into plain ol’ Groovy at various compilation phases of the program. This is how Groovy allows arbitrary DSLs that do not fit its grammar, a famous example being the Spock Testing Framework.

July 8, 2012

Defaulting to Dvorak on Linux

Filed under: how to,software dev — Tags: , , — Kamal Advani @ 18:15

This is (mostly) a Debian (or derivatives) specific guide to get the Dvorak keyboard layout across the system. Unfortunately, the power of a Unix is also sometimes its curse, in this case, at least as far as I know, there’s no central place to say “use Dvorak”, and have the system honour it. But broadly speaking, there are 3 input areas:

  1. Virtual Console
  2. Display Manager
  3. X session / Desktop Environment (DE)

A ‘modern’ DE like KDE or Gnome, will easily allow you to configure Dvorak through some GUI. I am documenting the case where it is DE-agnostic, and focuses on the X session starting in Dvorak to begin with. So unless there’s a DE override, this should work across all DEs.


Virtual Console

To use Dvorak on the virtual console, on the console issue loadkeys dvorak. This requires the various console keymaps installed. Under Debian, ensure the package console-data is installed; if you issue the command and get a file dvorak not found error or some such, you know you are missing the aforementioned package.

I have not been able to persist a Dvorak-by-default-console setting unfortunately. Will Google some more. There must be a nicer Debian Way, besides just issuing loadkeys directly in some init script. At a per user session, putting it in ~/.profile (or your shell’s equivalent) should work.


Display Manager

Important: This will start your display manager (e.g. GDM, LightDM, XDM) in Dvorak mode! For a shared computer you may not want this!

Edit /etc/default/keyboard, and set/replace XKBLAYOUT=”dvorak”, here’s mine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# !/bin/sh
# If you change any of the following variables and HAL and X are
# configured to use this file, then the changes will become visible to
# X only if HAL is restarted.  In Debian you need to run
# /etc/init.d/hal restart

# The following variables describe your keyboard and can have the same
# values as the XkbModel, XkbLayout, XkbVariant and XkbOptions options
# in /etc/X11/xorg.conf.

XKBMODEL="pc105"
XKBLAYOUT="dvorak" # was "us", replaced with "dvorak"
XKBVARIANT=""
XKBOPTIONS=""

# If you don't want to use the XKB layout on the console, you can
# specify an alternative keymap.  Make sure it will be accessible
# before /usr is mounted.
# KMAP=/etc/console-setup/defkeymap.kmap.gz
BACKSPACE="guess"

Note the comment on HAL, I missed it and simply restarted my machine to get it to reload, very lame.


X Session/Desktop Environment

To simply enable a per user X session to start with Dvorak, I set setxkbmap dvorak in ~/.xsessionrc before spawning the window manager. Here’s the full contents of ~/.xsessionrc file (for my own reference):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# see: http://productforums.google.com/forum/#!msg/chrome/t3vPnF_CTBE/UBba0Dp5w3AJ
nvidia-settings -a InitialPixmapPlacement=0

# enable dvorak by default
setxkbmap dvorak

# rotate portrait monitor output, use 'xrandr -q' to find out output names.
xrandr --output DVI-I-1 --rotate left

# compositing manager
xcompmgr -c &

# accented chars support
xmodmap -e "keysym Alt_R = Multi_key"

# Using xfce4-paneli (and thus all panel plugins) with openbox
# openbox is a lighter wm compared to xfwm
exec dbus-launch openbox-session

Note that you may have to configure your display manager to launch an appropriate X client script, I use Xsession with LightDM.

A slight digression: Many larger DEs do various magic to start-up, I really dislike this lack of transparency — I can see the value of it for absolute beginners of course, but you grow out of it pretty quickly once you come back to the Unix Way. To that effect, I modified LightDM — the display manager I use — to use Xsession. Here are my relevant changes to /etc/lightdm/lightdm.conf:

1
session-wrapper=/etc/X11/Xsession

This will find a ~/.xsessionrc, and execute that post starting the X server. Note that my script is documented above, it’s nice having a central script to hook in everything you need to start your X session with, including the window manager itself.

Switching to Dvorak

Filed under: how to,software dev — Tags: , — Kamal Advani @ 17:19

In the past 4 months or so, I have switched to the Dvorak Simplified Keyboard layout. This post documents the specifics of the journey.

Motivation*

*Added at Will’s request.

  • I never really learnt to touchtype properly. Thought this would be an interesting skill to have.
  • I was a little bored, and feeling a little crazy.
  • I wanted to be a l33t dvorakista. Work in progress.

Learning

  • To learn, I don’t think you can get any better than Dan Wood’s ABCD. It has been a joy to use!
    It is lowtech, so you will need to gauge your own accuracy; for me this worked out better. What you type out is also fairly entertaining!
  • ABCD does not list finger positions visually, so google a guide accordingly.
  • ABCD — it rocks. This is the only guide I used during the learning phase.
  • For general Dvorak information, check out Marcus Brooks’ Dvorak Page, very useful.

Practicing

  • To practice short daily-use words, I use 10FastFingers.
  • For much longer words, dvorak.nl fits the bill rather well.

Migration Strategy and Execution

  • Practice at home for about 2-3 weeks for 30 mins to an hour. It will take you at least that much time to complete an ABCD lesson at first!
  • After 3 weeks, use Dvorak exclusively at home, at the cost of feeling like a retard when chatting with friends. And crying each time I had to code.
  • After a month or so, switch to Dvorak at work. I am a programmer, so this was risky, I had to be confident enough to use it. Code completion was indispensable. Having a dev/team/architect lead who also uses Dvorak is likely even more indispensable. (Is “indispensable” subject to gradation? Also google “indispensIble” vs. “indispensAble”, what a mess.)
  • Around 2 to 2.5 months later, finally feel comfortable typing with Dvorak, though by no means fast. Around this time, reached 40-45wpm. Enough to stick to Dvorak!
  • Due to disuse, Qwerty muscle-memory began to fade!
  • 3.5 months or so later, likely covering my previous qwerty speeds. Able to chat without being considered a keyboard noob.
  • Qwerty muscle-memory gone.

Tips and Personal Ideosynchrasies

  • I found using sophisticated typing tutors, especially those with beeps very annoying. Visual cues are fine and useful, but beeps are frustrating and counterproductive IMO.
  • It will be painful at first, have no doubt about it.
  • It helped that I never learnt to formally touchtype, so this was a skill learnt from scratch.
  • Actually memorising the layout was extremely intuitive to me, all my attempts to learn qwerty touchtyping had failed in the past — it just made no sense. Dvorak actually has, for the most part, a logical system to the keyboard.
  • Shortcuts are one case where I have had to visually mind-map qwerty keys to Dvorak, so I do not have to move to the home keys just to execute a shortcut! It honestly is not that much effort though.
  • Passwords are another pain-point, you need to really let go and have your muscles do their thing, instead of relying on visual feedback too much — which you obviously don’t have when entering passwords.
  • Shell-foo: special characters and shell commands, some not being English words per se takes practice. I am still not as fast as I wish on the shell, unless I am in the zone.

January 27, 2010

fold / reduce

Filed under: how to,software dev — Tags: , , — Kamal Advani @ 01:51

Recently, I’ve been messing around with a fair bit of Groovy, a little bit of Scala, and a background-favourite (whatever that means), Python.

All 3 languages have functional traits, one functional idea is reduction where a collection of items is operated upon and finally reduced (hence the name) into a single value. From a practical viewpoint, this could be an overall analysis derived for all items in that collection – a sales percentage, a sum, or some other statistic. I am not a functional programming guru, so apologies for the rudimentary description. I was curious to see how each language handles it, and how I might write an implementation of reduce().

In particular I came across Scala’s List.foldLeft(). It also has a corresponding foldRight (and both functions have shorter operator notations :/ and :\ — which look like emoticons as I type this). Right and left determine where in the list it starts converging/reducing. Left starts from the start of the list (if you imagine the list horizontally rather than vertically, with the first/top most item on the left), while right starts at the bottom/end of the list. Depending on values in the list, and/or operations performed on them, the traversal order could obviously impact the result. Thus we note that fold-left and fold-right are basically types of reduce.

On to some code. I’ll keep it simple, albeit a little boring and predictable, the reduction will yield a sum of all elements in the list (yawn…). We’ll see some interesting properties of this simplicity however, and why it matters. I’ll start off with using the stock API for each language, and then roll my own. In all cases, essentially the following happens:

  • Decide on a collection of items (usually repeatably ordered!) to reduce
  • Apply the fold-left/reduce call which takes in an initial value to be used as we wish, in our case it will be a total result, initialised to zero
  • The fold-left/reduce call also takes a closure which is called for each item in the collection. The closure must return/yield a single value each time it is called (i.e. for each item in the collection), and takes 2 parameters, the running total and the current item being iterated over in the collection. I say ‘running total’ for our example, but more generally: for the first item in the collection, it is passed in our initial value, for subsequent items it is passed in the last ‘reduced’ value, i.e. the value that it yielded in the previous iteration.
  • A value is finally yielded and returned to the caller after the last item in the collection has been visited. In our case, the total.

Scala:

1
assert(List(1, 2, 3).foldLeft(0) { (total, item) => total + item } == 6)

Groovy:

1
2
3
4
5
6
7
assert [1, 2, 3].inject(0) { total, item -> total + item } == 6
/*
Note: inject() is on the (GDK) Collection supertype, java.lang.Collection.inject(), see:
http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html#inject(java.lang.Object value, groovy.lang.Closure closure)
This is interesting, because a HashSet is a collection, and it's ordering is arbitrary, which means
you want to be careful if your collection is a Set, and your operation expects an ordering!
*/

Python:

1
2
# reduce(func, sequence, init)
assert reduce(lambda total, item: total + item, [1, 2, 3], 0) == 6

A Few Notes on Syntax (skip this section as needed):
For the Scala and Groovy versions, note the syntactic sugar, both inject() and foldLeft() are functions that take 2 parameters, the initial value and a closure. It’s just that the closure can be defined as if it is ‘attached’ or ‘hooked to’ to the function rather than passed in, rather nice. With Groovy, the closure arg has to be the last one to allow this syntax (otherwise you can pass it in ‘foo(“bar”, { /*closure */ }, “meow”)’ like a regular parameter. In Scala, it depends on how the closure parameter is defined – in the case of foldLeft it is in its own parameter list, and so is not part of the initial set of args. I’m still a Scala n00b however, so I’ll stop there!

The Python case looks a little funnier, I am using the lambda form to create a new anonymous function, I could have also defined a function in the scope, and passed its name in as the first argument. This would be needed if I had something more involved in the function which the lambda form could not accomodate or accomodated hell-ishly (its body has to be a single expression only, no statements). Python doesn’t support more complicated anonymous functions as a language construct (there are code-level hacks though).

Implementing fold-left:
Now let’s try re-implementing fold-left using Python for clarity. Python has the idea of ‘sequences’ – positionally-ordered collections (from Learning Python 3E) – lists, strings, and tuples. However I am not sure why there’s no specific ‘sequence’ type, because this would have allowed setting a foldLeft/reduce call on this supertype leading to a more OO feel where you could do sequence.foldLeft(…). Instead the approach is much more procedural as you saw above. I’ll stick to the procedural approach for my implementation so arbitrary sequences can be passed in generically. I have not had a look at the source code for foldLeft in the languages above, I shall soon, however here’s my go at it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def foldLeft(sequence, reducefunc, initval):
    if not sequence: return initval
    for i, item in enumerate(sequence):
        return foldLeft(sequence[i+1:], reducefunc, reducefunc(initval, item))

def foldLeftMutable(sequence, reducefunc, initval):
    if not sequence: return initval
    reduced = initval
    for item in sequence:
        reduced = reducefunc(reduced, item)
    return reduced
 
# Test it.  
adderfunc = lambda total, item: total+item
# immutable version    
assert foldLeft([1, 2, 3, 4, 5, 6], adderfunc, 0) == 21
assert foldLeft([], adderfunc, 0) == 0
assert foldLeft(None, adderfunc, 0) == 0
# mutable version
assert foldLeftMutable([1, 2, 3, 4, 5, 6], adderfunc, 0) == 21
assert foldLeftMutable([], adderfunc, 0) == 0
assert foldLeftMutable(None, adderfunc, 0) == 0

There are 2 versions of the fold-left functions above, foldLeft() and foldLeftMutable(). The latter holds mutable state, albeit localised to the function (the localvar reduced), so probably no big deal (considered a non-functional implementation though?). Both functions have O(N) complexity[1]. I initially thought I had something exponential for the recursive version, but then realised that I was returning at each iteration, phew! Of course, O(N) also assumes that reducefunc here is O(1)! foldLeft() though holds zero mutable state even within the function. The sequence is sliced resulting in a new sequence each time before the recursive call. This is probably overkill, resulting in new objects each time – but kinda cool anyway from an immutability viewpoint. Needless to say that immutability is based on the assumption that items in the sequence are also immutable, or if mutable, are not being messed around with by the passed in reducefunc argument (in this case adderfunc). But as far as the direct objects that foldLeft() deals with – the sequence, initval, and reducefunc – it in no way changes them.

foldLeft() is, however, I think tail-recursive (another thing I need to read up on!) so it could just be converted to a plain iterative version by the bytecode compiler which would result in something similar to foldLeftMutable(), any specific comments on this would be highly appreciated. Anyway, bed time.

Footnotes:
[1] I just realised that slicing the list to produce a new list is likely to be O(N) rather than a constant time operation, which would invalidate my claim that the recursive version is also O(N), grrrr. Now I really am off to sleep.

January 26, 2010

GNU Screen Config

Filed under: how to,software dev — Tags: , — Kamal Advani @ 22:39

I have been using GNU Screen – an awesome terminal multiplexer – casually for quite a while now, but never really customised it much, since I still use a fair bit of GUI applications, even if I do frequent the terminal a lot. Today however I bit the bullet and set up a Screen config, nothing fancy, just a set of terms initialised to stuff I often use with proper term titles (I know, how did I live without them… since I rarely turn off my computer it’s only an issue when I do, since otherwise my term stays on). For my reference, and in case someone finds it useful, here’s the contents of my ~/.screenrc file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Status line
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'

#
# Default screens
#

# Root shell
screen -t rootsh su

# Utility shells
screen -t sh1
screen -t sh2
screen -t sh3

# eclipse workspace shell
chdir ~/eclipse_workspaces
screen -t eclipsews

# interpreters
chdir ~
screen -t python2.6 python2.6
screen -t groovysh groovysh
screen -t scalash scala
screen -t js js

# books dir
chdir ~/documents/ebooks
screen -t ebooks

# tmp dir
chdir /tmp
screen -t tmp

# launcher shell to launch other gui programs
# allows all sorts of logging junk to stay in one shell
chdir ~
screen -t launchsh

The ‘hardstatus’ details are based on this Red Hat zine article on GNU screen. I have yet to look up in the man page what exactly each formatting code/escape strings mean, all I know is it’s a decent set-up :P.

Of course, you’ll need the relevant programs installed if you use this as is – I just need my interpreter shells running to test snippets of code in different programming languages. You can also do stuff like set-up ssh sessions, send commands to running screens – almost collaborative – haven’t explored this much.

February 17, 2009

WordPress’ Tag Cloud Limit

Filed under: how to,software dev — Tags: , , , — Kamal Advani @ 04:34

So I got sick of wordpress seemingly not updating my tag cloud – after googling – turns out the tag cloud is limited to 45 items, and the admin interface (using WordPress 2.7.1 at time of writing) has no way of updating it. If you wish to up this limit, you:

  • Navigate to /path/to/wordpress/install/wp_includes
  • Backup the file widgets.php as widgets.php.orig in case you mess up (and no I did not).
  • Edit widgets.php, around line 1867 you’ll find a function wp_widget_tag_cloud($args) which looks like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Display tag cloud widget.
 *
 * @since 2.3.0
 *
 * @param array $args Widget arguments.
 */

function wp_widget_tag_cloud($args) {
        extract($args);
        // [... snip ...]
        echo $before_widget;
        echo $before_title . $title . $after_title;
        wp_tag_cloud(); // REPLACE ME!! ME!!! (and no this comment did not exist in the source)
        echo $after_widget;
}
  • Replace the commented call (can you guess which one?) with:
    wp_tag_cloud(array(‘number’ => 78))
    replacing 78 with the no. of tags you want to display.

If you’re so inclined and have shell access to your install, you can instead apply this patch which sets the number to 101, my setting, and if your file is different (say due to a different version of wordpress), it probably will fail :P.

Enjoy. I’m going to tag this post, among others, with ‘tag’, bet you didn’t see that coming ;).

– Kamal

January 12, 2009

Change Favicon In Joomla 1.5

Filed under: how to,software dev — Tags: , , , — Kamal Advani @ 17:52

While there are some extensions to do this (okay, I found one but it was broken), in Joomla 1.5 (decent product, a good balance between ease of use and uber flexibility), it seems that each template (in /templates/) have a favicon.ico file. Simply replace this file (keeping the same name obviously), with your own version. Google favicons and how to create them if you don’t know what they are :P. Note that favicons are usually cached by the browser, you’d either need to do several reloads, or clear your browser’s cache before the icon shows up/refreshes.

– Kamal

Powered by WordPress