• For loops in Javascript

    I have been writing some Javascript code lately. As a fan of Python, I was hoping that modern Javascript offered an equivalent to Python’s for x in y construct. After much reading and some confusion, I now know that JS offers many ways to do a for loop, and that these differ in non-intuitive ways. So, here is an overview of the for loops I’ve encountered.

    The classic C-style loop with a counter and a condition

    This style of loop is common in other languages, and will be familiar to many. The syntax is verbose and error prone. Not recommended.

    var arr = [1,2,3];
    for (var i=0; arr.length>i;i++) {
      console.log(i);
    }
    // Output:
    // 1
    // 2
    // 3
    

    The for .. of loop

    Although somewhat awkwardly named, this is very similar to Python’s for x in y, and exactly was I was looking for. Recommended for most purposes, but only works in browsers compatible with ES2015, and thus only safe to use if you compile your code with Babel (or something similar).

    let arr = [1,2,3];
    for (let i of arr) {
      console.log(i);
    }
    // Output:
    // 1
    // 2
    // 3
    

    The for .. in loop

    Looks good, but might not work as you expect. for .. in loops over the properties of an object, in arbitrary order. Stay away from this one unless you know what you are doing!

    let arr = [1,2,3];
    for (let i in arr) {
      console.log(i);
    }
    // Output:
    // 0 (array index, not value!)
    // 1
    // 2
    

    The forEach loop

    forEach is not a statement, but a method of an Array object. Nonetheless, it can serve as a for loop, and allows one to apply a function to each element for an array. forEach does differ from a loop statement in that a break statement has no effect inside of a forEach loop. The only way to break out of forEach is to throw an exception.

    let arr = [1,2,3];
    arr.forEach(i => console.log(i));
    // Output:
    // 1
    // 2
    // 3
    

    Map

    Like forEach, map is an Array method. It works like forEach, but returns a new array, with the provided function applied to each element of the original array.

    let arr = [1,2,3];
    console.log(arr.map(i => i*2));
    // Output
    // [2, 4, 6]
    
  • Multiple languages and compose key on OS X

    I speak three languages, and I write in all three almost every day.

    Two of these languages, Icelandic and Danish, have special characters that are not found on standard English keyboards, and thus require their own keyboard layout. Switching between keyboard layouts depending on what you are writing is a terrible user experience, and nobody should have to deal with that.

    Imagine my joy when I discovered the compose key, which allowed me to write in all three languages on my English keyboard layout. By using the compose key to combine two or more characters into one, I could type compose, /, o and get ø, or compose, a, e and get æ.

    The problem is that only Linux has a built in compose key. There is a solution for Windows, but it’s more complicated on OS X. I used an app called Ukelele to create a custom keyboard layout with key sequences that emulated a compose key, and used that for years. However, this solution stopped working when I upgraded to OS X El Capitan recently.

    Turns out that there is now a better solution. It’s still not as easy as on Linux and Windows, but using Karabiner with custom key mappings essentially gives you a compose key on OS X. I found a repository on GitHub by gnarf that provides exactly that, with nice example mappings for various emoji and the like. I forked and modified the mappings to include Nordic and German characters, and my compose key is working again.

    Find my mappings on GitHub.

  • GPCRdb published in Nucleic Acids Research

    A new paper is out in Nucleic Acids Res. that describes a major new release of GPCRdb.

    The new release has been completely re-written and consolidated into an integrated system using Python 3, Django and PostgeSQL.

    GPCRdb is now open source and the source code and source data are available on BitBucket. A virtual machine config for development is provided in the documentation, making it easy for anyone to start contributing to the project.

    Read the paper HERE.

  • Generic GPCR residue numbering

    A paper on generic residue numbering schemes for GPCRs has been published in Trends Pharmacol. Sci.

    The paper reviews commonly used generic numbering schemes, and introduces a novel, structure-based scheme that accounts for structural irregularities revealed by recent GPCR crystal structures.

    Complementary tools to look up generic numbers and number PDB structures can be found in GPCRdb.

    Read the paper HERE.

  • New fragment-based pharmacophore method published

    A paper describing a novel pharmacophore method for G protein-coupled receptors has been published in Methods.

    The method uses residue-ligand fragments from X-ray crystal structures to build pharmacophore models for GPCRs, and even allows for modeling of receptors with no structural or ligand data available.

    Read the paper HERE.

subscribe via RSS