KBD

Keith Devens .com

Saturday, March 20, 2010 Flag waving
And this too, shall pass. – often attributed to Abraham Lincoln

Tag: Code

Daily link icon Wednesday, August 13, 2008

Code to get the browser viewport size in javascript

function getViewport(){
  var e = window, a = 'inner';
  
  if(!('innerWidth' in e)){
    var t = document.documentElement
    e = t && t.clientWidth ? t : document.body 
    a = 'client';
  }
  
  return {width: e[a+'Width'], height: e[a+'Height']}
}

Modified slightly from a comment here.

Edit: the code in that comment didn't fully implement the original, and broke when I tried it in IE. So I've updated the code above.

Daily link icon Wednesday, September 12, 2007

while(<>){...} in Ruby

Re: translate Perl diamond operator to Ruby.

Perl:

while(<>){
  ...
}

Ruby:

ARGF.each do |$_|
  ...
end

At this point I question whether I'd ever use Perl for anything again. Until now, Perl filled a niche where if the code I wanted to write would fit in 10 lines or so, and did a lot of string manipulation, I'd turn to Perl. Otherwise Python. Now I think I'll just use Ruby for everything Smiley

Daily link icon Friday, July 13, 2007

Javascript toggle functions

Some more handy Javascript (depends on Prototype):

function toggleByClass(id, className, callback){
    // shows the element with id 'id'
    // and hides all other elements with class 'className'
    $A($$('.'+className)).each(function(element){
        $(element).style.display = "none";
    })
    if(id) // call it with a false id to hide all
        $(id).style.display = "block";
    if(callback)
        callback(id, className);
}

function toggleById(id1, id2){
    var a = $(id1).style;
    var b = $(id2).style;
    if(a.display == 'none' || a.display == ''){
        a.display = 'block';
        b.display = 'none';
    }else{
        a.display = 'none';
        b.display = 'block';
    }
}

Daily link icon Thursday, July 12, 2007

The annoying way to .clone() a list in C#

List<Type> temp = list.ConvertAll<Type>(delegate(Type a){return a;});

You'd think a generic type like List would have a useful clone or copy method, but noo. (Type is just a placeholder... insert your type here)

  1. I dunno if I have a recent version of my blogging bookmarklet on my site anywhere... last time I tried to find it I think the version I found was outdated. So here it is: Blog.

       (0) Tags: [Bookmarklets, Code, This website]

Daily link icon Thursday, June 7, 2007

  1. Because I'm sure it'll come in handy later, here's a javascript object clone function:

    function clone(obj){
        if(obj == null || typeof(obj) != 'object')
            return obj;

        var temp = {};
        for(var key in obj)
            temp[key] = clone(obj[key]);
        return temp;
    }

    Update: From feedback in the comments, a better version is:

    function clone(obj){
        if(obj == null || typeof(obj) != 'object')
            return obj;

        var temp = new obj.constructor(); // changed (twice)
        for(var key in obj)
            temp[key] = clone(obj[key]);

        return temp;
    }
       (29) Tags: [Code, Javascript]

Daily link icon Wednesday, January 24, 2007

Obfuscated code of the day

results = commands.collect{|i| host.cmd(i)}
  .collect{ |i| i.collect{ |i| i.chomp }[1..-2] }

(reformatted slightly)

What this code does: takes a list of commands to run over a telnet session, runs them all, takes the output for each command, splits it into an array of lines (removing any stray newlines on each line), strips off the first and last lines of the output (since the first line is just an echo of the command and the last line is the command prompt again), and returns an array of lines of the output for each command in an array.

Daily link icon Wednesday, May 31, 2006

A couple of solutions...

def isPowerOf2(num):
  return num == 1 or num == 2 or (num%2 == 0 and isPowerOf2(num/2))
def isPowerOf2(num):
  count = 0
  while(num > 0):
    count += num%2
    num >>= 1
  return count == 1

Update: From David, in the comments:

def isPowerOf2(num):
  return (num & (num - 1) == 0)

I've never gotten to do enough bit twiddling to have let me think of that.

Daily link icon Friday, March 24, 2006

  1. Here's some quick Perl I used to unescape some form-encoded data:

    while(<>){
        s/\+/ /g;
        s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
        print;
    }
       (0) Tags: [Code, Perl]
March 2010
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

I hate ASP.NET

I hate ASP... I was doing wonders​with PHP, then suddenly one of my​clients...

Johnies: Mar 17, 6:14am

Quantum physics and free will

I knew you were going to say that....

Tom Massey: Mar 15, 9:26pm

Generated in about 0.139s.

(Used 10 db queries)