It's interesting how much lambda, extension and anonymous methods I'm using nowadays
-
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 BlogI forced myself to use lambda expressions just to try the new fancy stuff few years ago but it’s not my thing. IMO readability of the code suffers from this syntax. Anonymous methods are the same thing as functionality, but a little more readable. I’m using them, but not for the sake of it, just where it’s proper. I think the only case where I use lambdas and vars now is wen working with “link to memory” (a beautiful technology).
There is only one Vera Farmiga and Salma Hayek is her prophet! Advertise here – minimum three posts per day are guaranteed.
-
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 BlogI use Lambdas a lot. One heck of a lot. I'm also getting into Rx as well, and they play a huge part there.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
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 BlogSort of, yes. But it's quicker for me to think imperative first and make sure my code does what I expect. Then I look at my code to see if it would benefit from a functional makeover. Surprisingly it's not in most cases, but I have started to avoid mutable states more these days.
"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.
-
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 BlogMarc Clifton wrote:
Is you C# imperative programming starting to look more and more like function programming?
To steal a coworkers comment on the Office07 ribbon from a few years ago: "It's growing on me ... like a fungus."
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
-
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++) 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
-
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 BlogYes, absolutely! Since the lambda syntax made anonymous functions elegant, I've noticed a complete change in the C# I write. Also, I learned about Expression Trees from working with EF and now rely on being able to create and modify them to write framework code.
-
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 BlogI admit that it has changed a lot. Unfortunately I haven't had the chance to work with it since I switched a bit to the C++/platform-independent stuff. It is an interesting field so I will learn the whole Lambda thing when I have to work with C#.Net again. For the moment I am developing a platform-independent generic queuing system. If it works as expected it will redefine the whole way my team is handling threads.
cheers Marco Bertschi
Software Developer & Founder SMGT Web-Portal CP Profile | My Articles | Twitter | Facebook | SMGT Web-Portal
-
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 BlogMarc Clifton wrote:
What about you? Is you C# imperative programming starting to look more and more like function programming?
I do, but I've always been a fan of functional programming, just haven't had the time to do anything useful in one (although, the more I play with it, the more I realize Ruby is pretty much a functional language with some procedural syntax sugar).
-
(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
-
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++) 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
-
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 -
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 BlogMaybe 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')
-
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')
I concur. To me this is yet more code masturbation.
-
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')
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.
-
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 BlogLess like programming and more like true magic. Express the will to produced a certain outcome, without being too particular, and release to Run.
-
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 BlogMarc 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
-
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.
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
-
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
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.
-
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 BlogI'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.