KBD

Keith Devens .com

Thursday, January 8, 2009 Flag waving
That is because you crazy! – Ikea
← 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

Gustaf Lindqvist (http://lindq.se) wrote:

Well with a lamba expression, this is what I came up with:

private static string JoinNvcToQs(NameValueCollection qs)
{
  return string.Join("&", Array.ConvertAll(qs.AllKeys, key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(qs[key]))));
}
∴ Gustaf Lindqvist | 30-Nov-2008 4:38pm est | http://lindq.se | #10982

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)
:

January 2009
SunMonTueWedThuFriSat
 123
45678910
11121314151617
18192021222324
25262728293031



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⇒The Elegant Universe

Well I have finally found the crazy​guy that preaches useless nonsence​in A...

Joseph Baxter: Jan 7, 11:07pm

I hate Norton Antivirus


SYMANTEC is very​cunning..
Symantec now have a​redeemable cash back offe...

CAN: Jan 4, 6:25pm

Spider solitaire

Hi everyone!

Glad to have found​this site.  I have enjoyed reading​the c...

flwrchld53: Jan 4, 5:30pm

The Escaped Prisoner: When God Is a Monster

if islam is afraid of one woman, it​is sad. it is sad that a lot of​muslim ...

alex: Jan 2, 1:56pm

Generated in about 0.192s.

(Used 8 db queries)

mobile phone