20110820

underscore.js cheat sheet
homepage

collections | arrays | functions | objects | utility

collections

  • each(list, iterator, [context]) 
    runs the iterator on each object in list
  • map (list, iterator,[context]) 
    calls the iterator for each object and returns an array of the results
  • reduce(list,iterator(memo, token), memo,[context]) 
    passes the return value into iterator as memo and then returns the last memo
  • reduceRight(list, iterator, [context]) 
    reverse of reduce
  • detect(list, iterator, [context]) 
    returns the first item that passes the iterator
  • select(list, iterator, [context]) 
    returns all items that pass the iterator
  • reject(list, iterator, [context]) 
    opposite of select
  • all(list, iterator, [context]) 
    returns true if all items pass the iterator
  • any(list, [iterator], [context]) 
    returns true if any items pass the iterator
  • include(list, value) 
    returns true if list contains value
  • invoke(list, methodName, [*args]) 
    invokes the method on each item passing in args
  • pluck(list, propertyName) 
    extracts a list of property values
  • max(list, [iterator], [context]) 
    returns the max value or max iterator result
  • min(list, iterator, [context]) 
    opposite of max
  • sortBy(list, iterator, [context]) 
    sorts list by iterator result
  • groupBy(list, iterator, [context]) 
    groups list by iterator result
  • sortedIndex(list, value, [iterator]) 
    returns the future index of value based on binary search
  • toArray(list, iterator, [context]) 
    converts list to array
  • size (list, iterator, [context]) 
    size of list

arrays

  • first(array, [n])
  • rest(array, [index])
  • last(array)
  • compact(array) 
    removes falsy values
  • flatten(array) 
    flattens array
  • without(array, [*values]) 
    removes the values from the array
  • union(*arrays) 
    returns an array of all unique items
  • intersection(*arrays) 
    array of values in all arrays
  • difference(array, other) 
    values in array but not in other
  • uniq(array, [isSorted]) 
    remove duplicates
  • zip(*arrays) 
    merges the values of each array w/ matching possitions
  • indexOf(array)
  • lastIndexOf(array)
  • range([start], stop, [step]) 
    creates an array

functions

  • bind(function, object) 
    the var this inside the function will be object
  • bindAll(object, [*methodNames) 
    this is always the object pass for all methods in the object
  • memoize(function) 
    caches the result of the function
  • delay(function, wait, [*argumens]) 
    like setTimeout
  • defer() 
    like setTimeout with 0
  • throttle(function, wait) 
    the returned function can only be called once every wait
  • debounce(function, wait) 
    return function will only be invoked after not being called the duration of wait
  • once(function) 
    function that can only be called once
  • after(count, function) 
    a function that will be invoked after being called count times
  • wrap(function, wrapper) 
    passes function into wrapper
  • compose(function g, function h, * functions) 
    returns f(g(h(*)))

objects

  • values(obj) 
    returns all the values
  • function(obj) 
    returns the name of every function
  • extend(destination, *sources) 
    move source members to destination
  • defaults(destination, *sources) 
    move source members not in destination to destination
  • clone(obj) 
    shallow copy
  • tap(obj, interceptor) 
  • isEqual(objA, objB) 
    compares values of objects
  • isEmpty(obj) 
    true if no values
  • isElement(obj)
    isArray(obj)
    isArguments(obj)
    isFunction(obj)
    isString(obj)
    isNumber(obj)
    isBoolean(obj)
    isDate(obj)
    isRegExp(obj)
    isNaN(obj)
    isNull(obj)
    isUndefined()

utility

  • identity(value) 
    useless
  • times(n, block) 
    invokes the block n tiems
  • mixin(object) 
    adds functions to _
  • uniqueId([prefix]) 
    creates unique dom id
  • template(template, [context]) 
    ejs template

My team recently switched to tfs and after attending a Kiln demo, I wondered what it would be like to use mercurial with tfs. I recently came across this Stackoverflow question which lead me to this los techies blog post Using Mercurial as a local repository for Team Foundation Server / Start Front’N.

In the post, Eric Hexter gives an excellent break down of how to setup mercurial for use with tfs. The only thing I would like to add, is to make sure you grab the correct version of the makewritable plugin for your mercurial version. Rookie mistake on my end, but it still cost me an hour.

Here is my version of the pull.ps1:

function Get-ScriptDirectory
{
        $Invocation = (Get-Variable MyInvocation -Scope 1).Value
        Split-Path $Invocation.MyCommand.Path
}
$originPath = "C:\tfs-working-dir"
$workingPath = Get-ScriptDirectory
$tf = "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe"

cd $originPath
&$tf get
hg commit -A -m "from tfs"
cd $workingPath
hg pull --rebase

and my version of the push.ps1:

function Get-ScriptDirectory
{
        $Invocation = (Get-Variable MyInvocation -Scope 1).Value
        Split-Path $Invocation.MyCommand.Path
}

$originPath = "C:\tfs-working-dir"
$workingPath = Get-ScriptDirectory
$tf = "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe"
$tfpt = "C:\Program Files (x86)\Microsoft Team Foundation Server 2010 Power Tools\tfpt.exe"

hg push
cd $originPath
&$tfpt scorch /noprompt /exclude:.hg,*.user
hg update -C -y
&$tfpt online /adds /deletes /exclude:.hg`,.hgignore`,*.ps1`,bin`,obj`,*.user`,*.suo`,App_Data
&$tf checkin
cd $workingPath        
20100305

With my netbook’s copy of the Windows 7 beta about to expire, I decided to give Ubuntu another shot. My previous experiences with Linux have been quite painful and in some cases it was easier to install a copy of Windows ME than force Linux to work right.

I downloaded a copy of Ubuntu 9.10 Netbook Remix for the net book and installed it off of a thumb drive. It installed without any issue and was the first time ever my wireless card worked out of the book with Linux.

It still has that slightly buggy Linux feel.
For example:

  • Firefox is set to open the home page when it loads but it always loads the last session.
  • Adding programs to the startup list doesn’t always seem to stick. I have had to add the same programs about three times each now.
  • Launchy doesn’t seem to want to open some files correctly and I get text files instead of Firefox.
  • Certain applications are always asking my permission to connect to unverified servers every time they open as Pandora, and I really don’t need that kind of security.
  • A GUI task manager because some programs keep locking up.
  • Eclipse can’t update and install any plugins such as Java projects.

Adobe has done a really good job with air and flash. Installing air and flash was a breeze and was the only way I could get a decent twitter client.

My two biggest complaints with it are the cartoony oversized controls (that has always bothered me with Linux) and the lack of polish.

I’m going to keep it and am enjoying it greatly; however, it’s not ready for the type of users that don’t care about learning how computer their computer works. My wife would be lost in Linux even though she has been using a mac for three years now. She doesn’t need to spend time customizing everything to work right.

Over the summer I picked up a ASUS EEE 1000HE netbook. The first thing I did when I got it was install win 7 and upgrade the RAM to 2gigs.

asus 1000 he

It advertises 9.5 hours of battery life and with win7 installed, I was not expecting to get even close, but I usually get over 8. Now, that’s with the monitor dimmed and the turbo button set to auto-mode. Yes, it has a turbo button. I was a little disapointed at first but it’s a nice throw back to the earlier days of computers. The auto-mode doesn’t get in the way much. Most of the time I am typing or reading the minilappy doesn’t need to do much.

As a dev machine, it’s decent. I have it running Visual Studio 2008 with Coderush. The screen real estate is a little tight. When Coderush runs a template, it tends to lag a little, but that’s mainly do to the hard drive. Also VS lags a lot when programming XAML.

For surfing the web, it’s awesome. Chrome is as zippy as ever on this little machine. And small size and ridiculously long battery life makes it an awesome machine for living on the couch.

At conferences, it’s great. Usually power outlets are a rare commodity. With 8+ hrs of batter life and a little prior planning, I free from searching for a place to recharge all day.

Overall, I wouldn’t use this as my main machine, but as a secondary, it is awesome.

code reference for INotifyPropertyChanged


public event PropertyChangedEventHandler PropertyChanged;
private void RaiseEventPropertyChanged(PropertyChangedEventArgs e)
{
    PropertyChangedEventHandler temp = PropertyChanged;
    if (temp != null)
    {
        temp(this, e);
    }
}
private void OnPropertyChanged(string propertyName)
{
        // perform pre event work
        // create args and raise event. 
        PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
        RaiseEventPropertyChanged(e);
}


show all