Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. It's interesting how much lambda, extension and anonymous methods I'm using nowadays

It's interesting how much lambda, extension and anonymous methods I'm using nowadays

Scheduled Pinned Locked Moved The Lounge
csharpphprubylinqcom
25 Posts 18 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Marc Clifton

    For example, here's a setter that updates the underlying field of a data row and fires a change event based on whether the field actually changed (UpdateField, an extension method, returns true) and whether the event has been wired (not null):

    set {row.UpdateField(model, "TableOfContents", value).Then(() => TableOfContentsChanged.IfNotNull(e => e(this))); }

    [edit] For example, the above would otherwise be written as:

    bool changed = row.UpdateField(model, "TableOfContents", value);
    if (changed)
    {
    if (TableOfContentsChanged != null)
    {
    TableOfContentsChanged(this);
    }
    }

    [/edit] Or, here's something that scans the nodes in a tree (Hierarchy being an extension method) and if it finds a match (not null) updates the title in the tree's caption:

    TreeView.Hierarchy().FirstOrDefault(n => n.Tag == rec).IfNotNull(t=>t[0] = rec.GetTitle());

    I'm also noticing how my programming is becoming much more "functional" - I often pass in Func or Action anonymous methods, etc. What about you? Is you C# imperative programming starting to look more and more like function programming? Marc

    Latest Article: C# and Ruby Classes: A Deep Dive
    My Blog

    G Offline
    G Offline
    Gjeltema
    wrote on last edited by
    #13

    I use it all the time now, though not quite as much as you do. I still use imperative styles a lot for ease of debugging for some things, but by and large the declarative syntax has definitely reshaped my coding - by far for the better.

    1 Reply Last reply
    0
    • M Maximilien

      (C++) I find the C++ syntax of Lambdas quite difficult and obscure (and, IMO, the C# syntax is not much better). Maybe, in time, when we install and use VS2012, I will learn to use them and love them, but for now, I don't see a use for them in our production code; and most examples do not convince me of their utility.

      Nihil obstat

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #14

      The C++11 stuff is fantastic. gcc 4.7 and Vs2012 and you're good to go for most of the new stuff

      1 Reply Last reply
      0
      • M Marc Clifton

        For example, here's a setter that updates the underlying field of a data row and fires a change event based on whether the field actually changed (UpdateField, an extension method, returns true) and whether the event has been wired (not null):

        set {row.UpdateField(model, "TableOfContents", value).Then(() => TableOfContentsChanged.IfNotNull(e => e(this))); }

        [edit] For example, the above would otherwise be written as:

        bool changed = row.UpdateField(model, "TableOfContents", value);
        if (changed)
        {
        if (TableOfContentsChanged != null)
        {
        TableOfContentsChanged(this);
        }
        }

        [/edit] Or, here's something that scans the nodes in a tree (Hierarchy being an extension method) and if it finds a match (not null) updates the title in the tree's caption:

        TreeView.Hierarchy().FirstOrDefault(n => n.Tag == rec).IfNotNull(t=>t[0] = rec.GetTitle());

        I'm also noticing how my programming is becoming much more "functional" - I often pass in Func or Action anonymous methods, etc. What about you? Is you C# imperative programming starting to look more and more like function programming? Marc

        Latest Article: C# and Ruby Classes: A Deep Dive
        My Blog

        F Offline
        F Offline
        Forogar
        wrote on last edited by
        #15

        No.

        - Life in the fast lane is only fun if you live in a country with no speed limits. - Of all the things I have lost, it is my mind that I miss the most. - I vaguely remember having a good memory...

        1 Reply Last reply
        0
        • M Marc Clifton

          For example, here's a setter that updates the underlying field of a data row and fires a change event based on whether the field actually changed (UpdateField, an extension method, returns true) and whether the event has been wired (not null):

          set {row.UpdateField(model, "TableOfContents", value).Then(() => TableOfContentsChanged.IfNotNull(e => e(this))); }

          [edit] For example, the above would otherwise be written as:

          bool changed = row.UpdateField(model, "TableOfContents", value);
          if (changed)
          {
          if (TableOfContentsChanged != null)
          {
          TableOfContentsChanged(this);
          }
          }

          [/edit] Or, here's something that scans the nodes in a tree (Hierarchy being an extension method) and if it finds a match (not null) updates the title in the tree's caption:

          TreeView.Hierarchy().FirstOrDefault(n => n.Tag == rec).IfNotNull(t=>t[0] = rec.GetTitle());

          I'm also noticing how my programming is becoming much more "functional" - I often pass in Func or Action anonymous methods, etc. What about you? Is you C# imperative programming starting to look more and more like function programming? Marc

          Latest Article: C# and Ruby Classes: A Deep Dive
          My Blog

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #16

          Maybe I'm getting old - but your first example fails my legibility and debugging test. It's all jolly clever, and all, but if I'm trying to debug that line of code, I have to stop and think FAR harder than I do with the long-winded example.

          MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

          J D M 3 Replies Last reply
          0
          • L Lost User

            Maybe I'm getting old - but your first example fails my legibility and debugging test. It's all jolly clever, and all, but if I'm trying to debug that line of code, I have to stop and think FAR harder than I do with the long-winded example.

            MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #17

            I concur. To me this is yet more code masturbation.

            1 Reply Last reply
            0
            • L Lost User

              Maybe I'm getting old - but your first example fails my legibility and debugging test. It's all jolly clever, and all, but if I'm trying to debug that line of code, I have to stop and think FAR harder than I do with the long-winded example.

              MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

              D Offline
              D Offline
              dusty_dex
              wrote on last edited by
              #18

              This fp style is going to require more code commenting. Not necessarily a bad thing, but sort of defeats the purpose of clean minimal code.

              "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

              P 1 Reply Last reply
              0
              • M Marc Clifton

                For example, here's a setter that updates the underlying field of a data row and fires a change event based on whether the field actually changed (UpdateField, an extension method, returns true) and whether the event has been wired (not null):

                set {row.UpdateField(model, "TableOfContents", value).Then(() => TableOfContentsChanged.IfNotNull(e => e(this))); }

                [edit] For example, the above would otherwise be written as:

                bool changed = row.UpdateField(model, "TableOfContents", value);
                if (changed)
                {
                if (TableOfContentsChanged != null)
                {
                TableOfContentsChanged(this);
                }
                }

                [/edit] Or, here's something that scans the nodes in a tree (Hierarchy being an extension method) and if it finds a match (not null) updates the title in the tree's caption:

                TreeView.Hierarchy().FirstOrDefault(n => n.Tag == rec).IfNotNull(t=>t[0] = rec.GetTitle());

                I'm also noticing how my programming is becoming much more "functional" - I often pass in Func or Action anonymous methods, etc. What about you? Is you C# imperative programming starting to look more and more like function programming? Marc

                Latest Article: C# and Ruby Classes: A Deep Dive
                My Blog

                B Offline
                B Offline
                Brady Kelly
                wrote on last edited by
                #19

                Less like programming and more like true magic. Express the will to produced a certain outcome, without being too particular, and release to Run.

                1 Reply Last reply
                0
                • M Marc Clifton

                  For example, here's a setter that updates the underlying field of a data row and fires a change event based on whether the field actually changed (UpdateField, an extension method, returns true) and whether the event has been wired (not null):

                  set {row.UpdateField(model, "TableOfContents", value).Then(() => TableOfContentsChanged.IfNotNull(e => e(this))); }

                  [edit] For example, the above would otherwise be written as:

                  bool changed = row.UpdateField(model, "TableOfContents", value);
                  if (changed)
                  {
                  if (TableOfContentsChanged != null)
                  {
                  TableOfContentsChanged(this);
                  }
                  }

                  [/edit] Or, here's something that scans the nodes in a tree (Hierarchy being an extension method) and if it finds a match (not null) updates the title in the tree's caption:

                  TreeView.Hierarchy().FirstOrDefault(n => n.Tag == rec).IfNotNull(t=>t[0] = rec.GetTitle());

                  I'm also noticing how my programming is becoming much more "functional" - I often pass in Func or Action anonymous methods, etc. What about you? Is you C# imperative programming starting to look more and more like function programming? Marc

                  Latest Article: C# and Ruby Classes: A Deep Dive
                  My Blog

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #20

                  Marc Clifton wrote:

                  I'm also noticing how my programming is becoming much more "functional" - I often pass in Func or Action anonymous methods, etc. What about you? Is you C# imperative programming starting to look more and more like function programming?

                  Hi Marc, There is a genius, known as Marc Clifton, who used to publish many fascinating articles here on CP. I wonder if he can be extra-judicially rendered out of whatever zone he's in now, back into CP's orbit, and gently seduced into publication mode, and motivated to write an article on "C# as a Functional Language," or something like that. Clifton's previous articles were always fascinating, envelope-pushing, journeys, whether I understood them, or not. I bet Clifton could explain in:

                  TreeView.Hierarchy().FirstOrDefault(n => n.Tag == rec).IfNotNull(t=>t[0] = rec.GetTitle());

                  what the variable 't is used for. yours, Bill

                  "Good people can be induced, seduced, and initiated into behaving in evil ways. They can also be led to act in irrational, stupid, antisocial, mindless, and self-destructive, ways when they are immersed in 'total situations' that impact human nature in ways that challenge our sense of the stability and consistency of individual personality, of character, and of morality."


                  Dr. Philip G. Zimbardo, in "The Lucifer Effect" 2008: ISBN-10: 08129744

                  M 1 Reply Last reply
                  0
                  • D dusty_dex

                    This fp style is going to require more code commenting. Not necessarily a bad thing, but sort of defeats the purpose of clean minimal code.

                    "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

                    P Offline
                    P Offline
                    Peter_in_2780
                    wrote on last edited by
                    #21

                    dusty_dex wrote:

                    This fp style

                    To keep it KSS, isn't there a vowel missing between f and p? Cheers, Peter

                    Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                    D 1 Reply Last reply
                    0
                    • P Peter_in_2780

                      dusty_dex wrote:

                      This fp style

                      To keep it KSS, isn't there a vowel missing between f and p? Cheers, Peter

                      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                      D Offline
                      D Offline
                      dusty_dex
                      wrote on last edited by
                      #22

                      F.P. (functional programming) On second thoughts about all this, perhaps we shouldn't use functional stuff in an imperative language. It's a brainfuck switching between the two styles, and the syntax isn't geared toward clarity.

                      "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

                      1 Reply Last reply
                      0
                      • M Marc Clifton

                        For example, here's a setter that updates the underlying field of a data row and fires a change event based on whether the field actually changed (UpdateField, an extension method, returns true) and whether the event has been wired (not null):

                        set {row.UpdateField(model, "TableOfContents", value).Then(() => TableOfContentsChanged.IfNotNull(e => e(this))); }

                        [edit] For example, the above would otherwise be written as:

                        bool changed = row.UpdateField(model, "TableOfContents", value);
                        if (changed)
                        {
                        if (TableOfContentsChanged != null)
                        {
                        TableOfContentsChanged(this);
                        }
                        }

                        [/edit] Or, here's something that scans the nodes in a tree (Hierarchy being an extension method) and if it finds a match (not null) updates the title in the tree's caption:

                        TreeView.Hierarchy().FirstOrDefault(n => n.Tag == rec).IfNotNull(t=>t[0] = rec.GetTitle());

                        I'm also noticing how my programming is becoming much more "functional" - I often pass in Func or Action anonymous methods, etc. What about you? Is you C# imperative programming starting to look more and more like function programming? Marc

                        Latest Article: C# and Ruby Classes: A Deep Dive
                        My Blog

                        C Offline
                        C Offline
                        c2423
                        wrote on last edited by
                        #23

                        I'm not convinced your first bit of code is better... It's certainly shorter, but much harder to read unless you're a super genius. I think that to be as professional as possible you have to write your code as if the next person to modify it is a useless coder but is a professional killer who knows where you live, and on that test I think your code fails. Can you justify that your code has better performance or some such by doing it this way? Or is it just clever? My prediction is that this will go the way of OO programming - it'll be hot for a while since nobody admits that they simply aren't smart enough to think about {delete as applicable: n levels of inheritance/lambda expressions}. Next step is for everybody to rebel and say that it makes a mess of the code and that you shouldn't ever use it at all, then we'll finally get to some consensus that there is an acceptable level of complexity that you can introduce which is somewhere in the middle of the two extremes.

                        1 Reply Last reply
                        0
                        • B BillWoodruff

                          Marc Clifton wrote:

                          I'm also noticing how my programming is becoming much more "functional" - I often pass in Func or Action anonymous methods, etc. What about you? Is you C# imperative programming starting to look more and more like function programming?

                          Hi Marc, There is a genius, known as Marc Clifton, who used to publish many fascinating articles here on CP. I wonder if he can be extra-judicially rendered out of whatever zone he's in now, back into CP's orbit, and gently seduced into publication mode, and motivated to write an article on "C# as a Functional Language," or something like that. Clifton's previous articles were always fascinating, envelope-pushing, journeys, whether I understood them, or not. I bet Clifton could explain in:

                          TreeView.Hierarchy().FirstOrDefault(n => n.Tag == rec).IfNotNull(t=>t[0] = rec.GetTitle());

                          what the variable 't is used for. yours, Bill

                          "Good people can be induced, seduced, and initiated into behaving in evil ways. They can also be led to act in irrational, stupid, antisocial, mindless, and self-destructive, ways when they are immersed in 'total situations' that impact human nature in ways that challenge our sense of the stability and consistency of individual personality, of character, and of morality."


                          Dr. Philip G. Zimbardo, in "The Lucifer Effect" 2008: ISBN-10: 08129744

                          M Offline
                          M Offline
                          Marc Clifton
                          wrote on last edited by
                          #24

                          BillWoodruff wrote:

                          and gently seduced into publication mode, and motivated to write an article on "C# as a Functional Language,"

                          Given the almost unanimous negative reaction I've received from this post (which was quite surprising) I think you're onto something there! Marc

                          Latest Article: C# and Ruby Classes: A Deep Dive
                          My Blog

                          1 Reply Last reply
                          0
                          • L Lost User

                            Maybe I'm getting old - but your first example fails my legibility and debugging test. It's all jolly clever, and all, but if I'm trying to debug that line of code, I have to stop and think FAR harder than I do with the long-winded example.

                            MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                            M Offline
                            M Offline
                            Marc Clifton
                            wrote on last edited by
                            #25

                            _Maxxx_ wrote:

                            but if I'm trying to debug that line of code, I have to stop and think FAR harder than I do with the long-winded example.

                            I might argue that the point is, that line of code would never need to be debugged, because it will always do exactly what it says it will do. If it fails, then there's a problem with the functions that it is calling, rather than at this level. That said, the other thing I notice when writing code in this form is that my functions / methods are much smaller - as with coding in F#, I notice that I break things down into much smaller units that are easier to test and can therefore be combined to create a richer expression of higher level behaviors. But I'm also surprised by the overall negative reaction to lambdas and anonymous methods that this post generated - this was very illuminating and, as Bill Woodruff suggested, might be worth an article or two. Marc

                            Latest Article: C# and Ruby Classes: A Deep Dive
                            My Blog

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups