What Lambda expression corresponds to this LINQ query?

R

raylopez99

Yeah, you seem to always have that problem. Noted.
Here is a short but complete program ordering all the even numbers
first, then all the odd ones (within the range 0-9):

using System;
using System.Linq;

public class Test
{
    static void Main()
    {
        var orderByParity = Enumerable.Range(0, 10)
                                      .OrderBy(r => r%2!=0);

        foreach (int x in orderByParity)
        {
            Console.WriteLine(x);
        }
    }

}

Note that *ordering* isn't *filtering*. If you want to only show the
odd values, you need to use Where rather than OrderBy.

Thank you! I ran this and it works, and I learned something. Isn't
that what Usenet is for? Or is it only for flaming?


Let me ask you then: I'm going through your book and your writings
(finished Extension methods in the cheat sheet this afternoon; good
job). It is a good book and I understand it. Now, if somebody
finishes and understands "C# in Depth" by Jon Skeet--how far along
from 0% (no clue about C#) to 100% (guru) will they be? Riddle me
this please? I will trap you in your own words. If you say "20%" and
essentially adopt Goran's position that to get to 80% you need at
least two years experience in C#, then you implicitly agree your book
is not "in depth". On the other hand, if you agree, then perhaps C#
does not have as much 'depth' as other languages. I will eagerly
await your reply Jon.

You didn't understand what declaring a variable meant.

NO! Don't rehash this stuff Jon. It shows you are so juvenile. No
wonder you can't hold a steady job. What the debate was--and I recall
it clearly--was semantics. I even have your most pithy reply in my
notes (unlike you, I take notes):

QUOTE
class *object* isn't being declared - a *variable* is being declared.
Just because there's a variable doesn't mean any object is necessarily
being created. The variable may have a null value, or refer to an
object which was created elsewhere. For instance:
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = sb1;
That has declared two variables, but only one object has been created.
(instantiated object sb1 is one variable, and sb2 is another variable)
UNQUOTE
You thought it
was a forward declaration of a class. That's pretty fundamental.

No, you're the one that doesn't know what a forward declaration is
(used in C++) or what a copy constructor is (id), but let's not rehash
this old stuff, please.
You
believed that GUI applications were so completely different to console
apps that they would handle parameter passing differently. Those two
are just off the top of my head

Actually, they do! Try easily passing parameters (variable 1, 2, etc)
in-between Main() and other translation units, and you'll be hard
pressed unless you use using classes. Not true with Windows apps:
you simply use a parametricized constructor along the lines of Form2
myform2 = new Form2(variable1,varia2, etc);

I've seen plenty of people understand lambda expressions, order-by
projections etc with no problem. I certainly wouldn't say that all of
them are "gurus".

I don't have any problem either. Your problem is that you like to
talk down to people just because they don't have your narrow
expertise. You're not an expert in my field either.
Assuming you mean StaticRandom, you'll see that it uses the standard
library's random number generator anyway. It just does so in a way
which makes it threadsafe.

Who cares about 'threadsafe'? Parallel programming is so stupid. In
the year 2012+ the compiler will break up your program into non-
overlapping parallel threads; no need to bother yourself with that
now. let the compiler handle it.

RL
 
R

raylopez99

Another misunderstanding. A dictionary is *not* an ordered pair. It's a
mapping.

A pedants point. Noted. I was using an ordered pair to mean
mapping. And binary trees (ordered dictionaries) are not so bad in
performance: a chart in Albahari et al (C# Nutshell, an excellent
book, you should read it and review it) shows SortedDictionary<K,V> (a
Red-Black balanced tree) is about five times slower than Dictionary,
which is not radically slower to me (in my mind's eye).

RL
 
J

Jon Skeet [C# MVP]

raylopez99 said:
A pedants point.

Um, it's the fundamental point of a dictionary type. That's far from a
pedantic point.
Noted. I was using an ordered pair to mean
mapping. And binary trees (ordered dictionaries) are not so bad in
performance: a chart in Albahari et al (C# Nutshell, an excellent
book, you should read it and review it)
http://msmvps.com/blogs/jon_skeet/archive/2008/03/31/book-review-c-3-0-
in-a-nutshell.aspx

shows SortedDictionary<K,V> (a
Red-Black balanced tree) is about five times slower than Dictionary,
which is not radically slower to me (in my mind's eye).

They have different complexities. They may be 5 times slower for a
given size of data, but over time a SortedDictionary<K,V> will become
slower and slower at insertion. In addition, SortedDictionary requires
keys to be comparable, whereas Dictionary only requires them to be
equatable. Basically, if you really want the dictionary to be sorted in
key order, SortedDictionary and SortedList are great. (The name
"SortedList" is more to do with the implementation than the API - it's
still a dictionary.) If you don't need that property, Dictionary is
your friend.

As the MSDN docs state, SortedDictionary<K,V> has O(log n) complexity
for both retrieval and insertion. Dictionary<K,V> has near O(1)
retrieval and amortised O(1) insertion.

Here's a little test program:

using System;
using System.Collections.Generic;
using System.Diagnostics;

public class Test
{
const int Size = 10000000;
const int Iterations = 10;

static void Main()
{
var sorted = new SortedDictionary<int, int>();
var unsorted = new Dictionary<int, int>(Size);

for (int i=0; i < Size; i++)
{
sorted = i;
unsorted = i;
}

TimeRetrieval(sorted);
TimeRetrieval(unsorted);
}

static void TimeRetrieval(IDictionary<int, int> dictionary)
{
Stopwatch sw = Stopwatch.StartNew();
int total = 0;
for (int i=0; i < Iterations; i++)
{
for (int j=0; j < Size; j++)
{
total += dictionary[j];
}
}
sw.Stop();
Console.WriteLine("{0}: {1}ms", dictionary.GetType().Name,
sw.ElapsedMilliseconds);
}
}

The results:
SortedDictionary`2: 56779ms
Dictionary`2: 3585ms


(I haven't replied to your other post due to its inflamatory nature, by
the way. As I've said before: if you want sensible discussion, you
should post sensibly and politely.)
 
R

raylopez99

The results:
SortedDictionary`2: 56779ms
Dictionary`2: 3585ms

Well that's 15 times, not 5 or 6, but I still trust p. 257 of Albahari
et al.
(I haven't replied to your other post due to its inflamatory nature, by
the way. As I've said before: if you want sensible discussion, you
should post sensibly and politely.)

Well you're a smarter SOB than I thought Jon.

Plus I had you on a Procrustean Bed--either way you answered I was
going to skewer you, you dumb SOB.

Thanks for your help BTW, without you, I would not be learning C# as
effectively (nor posting here as much).

RL
 
J

Jon Skeet [C# MVP]

raylopez99 said:
Well that's 15 times, not 5 or 6, but I still trust p. 257 of Albahari
et al.

That just gives a list of results for a single test - it doesn't
indicate complexity. If you increase the size of the dictionary
further, the gap would widen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top