KBD

Keith Devens .com

Tuesday, October 7, 2008 Flag waving
A Lisp programmer knows the value of everything, but the cost of nothing. – Alan Perlis

Tag: Programming languages

Parents:

Children:

Page 1 →

Daily link icon Tuesday, October 7, 2008

  1. Kirk Allen Evans's Blog : Getting Path from an XmlNode

       (0) Tags: [C#, XML]

Daily link icon Monday, October 6, 2008

  1. Worlds: Controlling the Scope of Side Effects | Lambda the Ultimate. It's a short paper, to read.

       (0) Tags: [Programming languages]

Daily link icon Monday, September 29, 2008

  1. jQuery: » jQuery, Microsoft, and Nokia:

    Both Microsoft and Nokia are taking the major step of adopting jQuery as part of their official application development platform. Not only will they be using it for their corporate development but they will be providing it as a core piece of their platform for developers to build with... This means that jQuery will be distributed with Visual Studio (which will include jQuery intellisense, snippets, examples, and documentation).

       (0) Tags: [Javascript]

Daily link icon Wednesday, September 3, 2008

  1. Moritz Lenz's blogs on Perl 6, intended for people familiar with Perl 5, have done a lot to get me re-interested in Perl 6, despite how long it's taken to develop.

    My understanding is that a full release of Perl 6 is probably another year away, but it may have been worth the wait. Perl 6 looks like a really fun language.

       (0) Tags: [Perl]
  2. [squeak-dev] Using V8 for other languages (via):

    One thing is clear: JavaScript is the assembly language of the Internet, at least for a few years now.

    Edit: here was the thing I'd read on TraceMonkey a while back (via Simon).

    Edit: One more post on TraceMonkey (to read).

       (0) Tags: [Javascript]

Daily link icon Thursday, August 21, 2008

W3C Selectors API

Just learned about the W3C Selectors API from Simon's blog. Turns out a native implementation is forthcoming in Firefox 3.1 (as well as Opera, IE 8, and WebKit), but in the meantime many Javascript libraries already implement the spec, Mootools, jQuery, and Prototype to name a few.

WebKit provides a speed test of a native implementation of querySelectorAll vs popular Javascript libraries (obviously the native implementation won't work for you unless you're using a Firefox beta). It's based on Mootools' test.

  1. Looks like Python 3.0 is implementing John Lim's suggested syntax for octal literals (with a slight difference). I'd prefer John's Oc to Python 3.0's Oo, but it doesn't matter.

    They've made a lot of nice cruft-removing changes to Python for 3.0.

       (0) Tags: [Python]

Daily link icon Wednesday, August 20, 2008

More fun with extension methods

or shoehorning in what the C# library should have included in the first place.

public static void Each<T>(this IEnumerable<T> source, Action<T> a){
  foreach (var item in source)
    a(item);
}

public static void Each(this IEnumerable source, Action<object> a){
  foreach (var item in source)
    a(item);
}

See my previous extension method, Join.

Bad C# design

I should be able to do something like:

var table = new HtmlTable {
  Rows = { // <-- error is here
    from XmlNode p in node.SelectNodes("./property")
    select new HtmlTableRow{
      Cells = {
        new HtmlTableCell{ InnerText = Server.HtmlEncode(p.Attributes["description"].Value) },
        new HtmlTableCell{ InnerText = Server.HtmlEncode(p.InnerText) }
      }
    }
  }
};

But I can't, because the LINQ code isn't "expanded" so the compiler complains that while it expects an HtmlTableRow element between the braces assigned to Rows, you're giving it a collection. But, you can't assign the collection to Rows directly because it's a read-only property.

Bad class design (Rows being read-only... same as Cells fwiw) or bad language design in that there's no way to "expand" the results of the LINQ query to do what seems natural in that spot?

Instead, I have to do:

var rows = 
  from XmlNode p in node.SelectNodes("./property")
  select new HtmlTableRow{
    Cells = {
      new HtmlTableCell{ InnerText = Server.HtmlEncode(p.Attributes["description"].Value) },
      new HtmlTableCell{ InnerText = Server.HtmlEncode(p.InnerText) }
    }
  };

var table = new HtmlTable();
foreach (var r in rows)
  table.Rows.Add(r);

which is redundant.

Daily link icon Wednesday, August 13, 2008

  1. Javascript Drag and Drop. Old, but pretty concise tutorial.

       (1) Tags: [Javascript]

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 Friday, August 8, 2008

  1. Sketchy LISP, An Introduction to Functional Programming in Scheme (via).

       (0) Tags: [Books, Scheme]

Daily link icon Thursday, August 7, 2008

  1. Raphaël—JavaScript Library (via).

    Raphaël is small JavaScript library that should simplify your work with vector graphics on the web. In case you want to create your own specific chart or image crop-n-rotate widget, you can simply achieve it with this library.

    Raphaël uses SVG and VML as a base for graphics creation. Because of that every created object is a DOM object so you can attach JavScript event handlers or modify objects later. Raphaël’s goal is to provide adapter that will make drawing cross-browser and easy. Currently library supports Firefox 3.0+, Safari 3.0+, Opera 9.5+ and Internet Explorer 6.0+.

       (0) Tags: [Javascript]

Daily link icon Thursday, July 24, 2008

  1. Second p0st: PHP hackery: quick and dirty anonymous objects. Eew.

       (0) Tags: [PHP]
  2. SitePen Blog » window.name Transport (via Keith and Simon). To read.

       (0) Tags: [Javascript, To Read]
  3. LINQ to Objects - 5 Minute Overview - Hooked on LINQ. Decent tutorial. Has examples of grouping and joins.

       (0) Tags: [C#, LINQ]
  4. C# 3.0: The Evolution Of LINQ And Its Impact On The Design Of C# (via). Very interesting explanation of how LINQ came about.

       (0) Tags: [C#, LINQ]
  5. New "Orcas" Language Feature: Extension Methods - ScottGu's Blog. Very informative post.

    Edit: He also covers C# 3's query syntax (i.e. LINQ).

       (0) Tags: [C#, LINQ]

Joining together the results of a LINQ query into a string

Say you want to search a string with a regular expression and return all the matches concatenated together, using LINQ:

var str = "some string";
var matches = Regex.Matches(str, @"REGEX");

Three ways to concatenate:

Using String.Join (simplest):

var foo = String.Join("", (from Match match in result select match.Value).ToArray());

Using an accumulator:

var foo = (from Match match in matches select match.Value)
    .Aggregate(new StringBuilder(), (sb, s) => sb.Append(s)).ToString();

Using a loop:

var sb = new StringBuilder();
foreach (var s in (from Match match in result select match.Value)){
    sb.Append(s);
}
var foo = sb.ToString();

First seems most concise, and it's easier to specify a join character with. Apparently there's no way to massage an IEnumerable(T) into a String.Join, so unfortunately you need to pass it an array, which makes the IEnumerable fill out all its values into an array (so it can't be lazy), and then String.Join makes another pass over that array and copies the values into a string. So it uses double the space and double the time.

The second is more obscure, but only makes one pass over the result, though it's harder to specify a join character if desired.

The third is presumably most efficient, but it's more verbose.

In conclusion, there should be a String.Join(IEnumerable<T>).

Edit: Though it's impossible to define a "static" extension method (like, another variation of String.Join), you can define a Join method on IEnumerable like so:

static class Extentions{
    public static string Join(this IEnumerable source, string separator){
        var sb = new StringBuilder();
        bool first = true;
        foreach(var foo in source){
            if(!first)
              sb.Append(separator);

            sb.Append(foo.ToString());
            first = false;
        }
        return sb.ToString();
    }
}

The first example above now becomes:

var foo = (from Match match in result select match.Value).Join("");

or even more concisely:

var foo = Regex.Matches(str, @"REGEX").Join("");
// (apparently the ToString on a Match object returns its Value)

Very cool.

Note: Works with custom ToString()s as expected:

class Custom{
    public string Value { get; set; }
    public override string ToString(){
        return "VALUE: "+Value;
    }
}
var list = new List<Custom> {
    new Custom { Value = "one" },
    new Custom { Value = "two" }
};

Console.WriteLine(list.Join(", "));

prints "VALUE: one, VALUE: two" as expected.

Final note: I would have used FirstOrDefault in my Join extension method instead of a first boolean, but the example of the regular expression object was chosen because it's not a generic, so I can't use IEnumerable<T>, only IEnumerable.

Daily link icon Wednesday, July 23, 2008

  1. Simple Top-Down Parsing in Python (via). To read.

       (0) Tags: [Python, To Read]

Daily link icon Saturday, July 19, 2008

  1. FirePHP - Firebug Extension for AJAX Development (via).

       (0) Tags: [Firefox, PHP]

Daily link icon Thursday, July 17, 2008

  1. Overview of C# 3.0

       (0) Tags: [C#]
  2. Blue programming language

       (0) Tags: [Programming languages]

Daily link icon Saturday, July 5, 2008

  1. Running C and Python Code on The Web at Toolness, via.

       (0) Tags: [C, Flash, Python]

Daily link icon Monday, May 12, 2008

  1. John Resig - Processing.js (via):

    I've ported the Processing visualization language to JavaScript, using the Canvas element.

    Impressive, to say the least.

       (0) Tags: [Javascript]

Daily link icon Thursday, April 24, 2008

  1. Generator Tricks for Systems Programmers (via). Presentation on Python's generators.

       (0) Tags: [Python]

Daily link icon Wednesday, April 23, 2008

  1. JavaScript: The Good Parts, by Douglas Crockford (via).

       (0) Tags: [Books, Javascript]
  2. Reading binary files using Ajax « nagoon97’s Weblog (via).

       (0) Tags: [Ajax]

Daily link icon Tuesday, April 15, 2008

  1. Javascript getElementsByClass function. I know there's one built into prototype, but I didn't want to include the whole library since I wasn't already using it. This seems to work.

       (4) Tags: [Javascript]

Daily link icon Monday, April 7, 2008

  1. JSLint, The JavaScript Verifier (via).

       (0) Tags: [Javascript]
Page 1 →
October 2008
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



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

Recent comments XML

new⇒Timesheet Calculator

Hadn't seen it before now, but my​company already uses a time​tracking prog...

Keith: Oct 7, 10:44am

Girls, please don't get breast implants

Hey everyone, 

I am new to this​blog and I have enjoyed reading all​your...

Sarah.M.: Oct 6, 9:45am

obout inc - ASP.NET controls

I like there components. I've got​it to work locally on my pc.​However I'm ...

Jeff: Oct 2, 4:43pm

Dumb substring behavior in C# (and Java)

Yes, the Substring function is not​helpful when you hit the length​problem,...

Mike Irving: Oct 2, 7:56am

Generated in about 0.352s.

(Used 12 db queries)

mobile phone