KBD

Keith Devens .com

Wednesday, July 9, 2008 Flag waving
REMEMBERS- HEEL BARES DURID! BARE DURIDS IS STORNG FREND! – Alamo
← Uncrittable druid w/o pvp gearThe FAIL Blog →

Daily link icon Wednesday, February 13, 2008

Join a NameValueCollection into a querystring in C#

C# is ugly as heck:

private static string JoinNvcToQs(System.Collections.Specialized.NameValueCollection qs){
    return string.Join("&", Array.ConvertAll<string, string>(qs.AllKeys, delegate(string key){
        return string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(qs[key]));
    }));
}

In Ruby, this would be (essentially):

e = URI.escape
qs.collect{ |k,v| e(k)+"="+e(v) }.join("&")
← Uncrittable druid w/o pvp gearThe FAIL Blog →

Comments XML gif

Revence (http://freeshells.ch/~revence) wrote:

I think your Ruby code is lacking a .call on the use of the e variable. Maybe you meant:

e = URI.method :escape
qs.collect {|k, v| e.call(k) + '=' + e.call(v)}.join('&')

For one, I don't know how anybody stands using these Java-esque languages. I mean, if you can't get type-inference (the ML-family way), you are better off without static types. Yet even Ruby feels a tad unwieldy (and unsafe) when juxtaposed with a more-functional alternative like SML, OCaml, or (my fave) Haskell.

e = (flip Network.URI.escapeString) reserved
concat $ intersperse "&" [k ++ "=" ++ v | (k, v) <- Data.Map.toList qs]

That is the equivalent in Haskell. I assumed that qs would be a map (corresponds to a pure hashtable). It's pretty.

∴ Revence | 13-Feb-2008 11:44pm est | http://freeshells.ch/~revence | #10525

Keith (http://keithdevens.com/) wrote:

Yeah I didn't actually execute that Ruby code. I assumed you could take a reference to the method like that, but of course you can't since Ruby lets you call methods without parens. Anyway, the example doesn't change much. Thanks for the correction.

I mean, if you can't get type-inference (the ML-family way), you are better off without static types.

Totally. Like I tried using Array.ConvertAll without the <string, string> on the end, and I got a compiler error saying it couldn't figure out what it was supposed to map to, even though AllKeys returns a string[], the delegate takes a string as a parameter, and the delegate returns a string.

Keith | 14-Feb-2008 12:40am est | http://keithdevens.com/ | #10526

Revence (http://freeshells.ch/~revence) wrote:

And I swear I would love to use the .NET delegates. A lot. But the syntax is hideous in C#, it was, for long, scarier than C's function pointers. I'm resigned to doing F# if I have to do .NET programming. It's not that often, but still. I'm scarred from having looked C# in the face after a long, long time in the Python/Ruby/Haskell world. Smiley

∴ Revence | 14-Feb-2008 2:51am est | http://freeshells.ch/~revence | #10527

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

July 2008
SunMonTueWedThuFriSat
 12345
6789101112
13141516171819
20212223242526
2728293031 



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

getElementsByClass function

http://pitfalls.wordpress.com/2008/​07/07/querying-it-jquery-way-getele​ments...

maxgandalf: Jul 7, 5:50am

Generated in about 0.216s.

(Used 8 db queries)

mobile phone