Delegates tutorial meeting specific criteria?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sherifffruitfly

    Delegates tutorial meeting specific criteria?

    I'm looking for a tutorial/sample specifically aimed at showing why
    life is better with them than without them. Ideally, what I'd like to
    see in the tutorial I'm looking for:

    1) No discussion of events.
    2) The "non delegates way" and "the delegates way" of dealing with a
    problem both presented.
    3) A clear explanation of why "the delegates way" is either clearer,
    more robust, or more easily maintainable/extensible.
    4) One other thing which I'm forgetting atm. :)


    I've googled lots, and haven't found even a good approximation to what
    I'm looking for.

    Thanks for any suggestions!
  • sherifffruitfly

    #2
    Re: Delegates tutorial meeting specific criteria?

    On Feb 11, 6:56 pm, Jesse McGrew <jmcg...@gmail. comwrote:
    Here's one example: a map function.
    Thanks Jesse - that's the kind of thing I'm looking for - I'll study
    the example.

    Also makes me think of Mathematica - your "Map" looks suspiciously
    similar in concept to theirs - I'll check theirs out and see if any
    inspiration is to be had.

    Thanks again!

    Comment

    • Jesse McGrew

      #3
      Re: Delegates tutorial meeting specific criteria?

      On Feb 11, 7:27 pm, sherifffruitfly <sherifffruit.. .@gmail.comwrot e:
      On Feb 11, 6:56 pm, Jesse McGrew <jmcg...@gmail. comwrote:
      >
      Here's one example: a map function.
      >
      Thanks Jesse - that's the kind of thing I'm looking for - I'll study
      the example.
      >
      Also makes me think of Mathematica - your "Map" looks suspiciously
      similar in concept to theirs - I'll check theirs out and see if any
      inspiration is to be had.
      Yeah, it's a common concept. In fact, .NET already includes the method
      Array.ConvertAl l, which does exactly the same thing.

      Some of the other static methods on Array are also good examples of
      what delegates are good for: see Exists, Find, TrueForAll, etc. where
      you supply a delegate to select the elements you care about.

      Jesse

      Comment

      • Marc Gravell

        #4
        Re: Delegates tutorial meeting specific criteria?

        I'm looking for a tutorial/sample specifically aimed at showing why
        life is better with them than without them. Ideally, what I'd like to
        see in the tutorial I'm looking for:
        How about LINQ? (in particular, LINQ-to-objects). The following uses
        lambda notation, but (in this context) this is just a delegate:

        var query = source.Where(x =x.Bar == bar)
        .GroupBy(x =x.Category).Se lect(y =new
        {
        Name = y.Key,
        Total = y.Sum(z =z.Bloop),
        Count = y.Count()
        });

        which is identical to the alternative syntax:

        var query2 = from x in source
        where x.Bar == bar
        group x by x.Category into y
        select new
        {
        Name = y.Key,
        Total = y.Sum(z =z.Bloop),
        Count = y.Count()
        };

        This (both: they are identical) is doing a filter with a predicate
        (via a delegate), grouping by a selection (via a delegate), and
        performing a projection (via a delegate). To do that without delegates
        (or expressions) requires looping, running your own filter, worrying
        about the dictionary, performing the aggregation etc.

        But even better: with this approach, we can swap the filter at runtime
        - i.e. if we used:

        Func<Foo, boolfilter;
        switch(somethin g) {
        case [etc]:
        filter = whatever;
        break;
        ...
        }

        We could then use [etc].Where(filter).[etc], and we have a runtime
        filter. Very easy and extensible.

        Marc

        Comment

        Working...