Interesting / strange code picked up from pluralsight training (functional programming)
-
[Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages](https://www.codeproject.com/Articles/1200375/Class-less-Coding-Minimalist-Csharp-and-Why-Fsharp#Second,Fluency3) Shameless plug, but if you want to look at doing functional programming in C# beyond just Action and Func, you might want to take a look at that.
Latest Article - Contextual Data Explorer Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802
-
I thought a side effect would be to affect some property of the object?? I don't see where SpaceOut is be doing that. Let me know what you're thinking so I can learn. thx
In functional terms, writing to the console is a side-effect. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
In functional terms, writing to the console is a side-effect. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Ah, yes. That explains. thanks, very much. However, I still kind of secretly like that SpaceOut method, but don't tell everyone because they'll think I'm not all functional-minded. :) Seriously though, that was good to learn because it makes me think the right way about functional programming that it should be just like a mathematical expression. Do the thing, return the value. :thumbsup: ;)
-
Func<T, TResult>
is covariant[^] on the return type, so you could pass in functions which returned a more derived type than the declared return type.public static T TeeHee<T>(this T tee, params Func<T, object>[] funcs)
{
foreach (var func in funcs)
{
object x = func(tee);
// ???
}return tee;
}
42.TeeHee(
i => i, // Func<int, int>
i => $"The answer is {i}", // Func<int, string>
i => new Answer(i) // Func<int, Answer>
);The more important question would be, what are you intending to do with the returned values?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Another brilliant gem ! thanks, Bill
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
-
Replace
params Func[] funcs
simply with>>
and you're good to go. MarcLatest Article - Contextual Data Explorer Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802
Nice ! thanks, Bill
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
-
You're saying so you could run a number of methods (funcs) (in the foreach loop), right? That would be interesting. Once you start doing this stuff it inspires you to see everything this way. :thumbsup:
This thread, and the responses to it, are the kind of back-and-forth that makes CP so valuable to me ! thanks, again, to your ever curious mind for starting it. cheers, Bill
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
-
Ah, yes. That explains. thanks, very much. However, I still kind of secretly like that SpaceOut method, but don't tell everyone because they'll think I'm not all functional-minded. :) Seriously though, that was good to learn because it makes me think the right way about functional programming that it should be just like a mathematical expression. Do the thing, return the value. :thumbsup: ;)
I can recommend learning Haskell, a pure functional language. In Haskell all I/O is considered a side-effect (database actions, drawing, printing to console, writing a file, etc.). If you're doing any I/O in Haskell your function must return the special System.IO object. Here's a nice example of Hello World in Haskell[^]. I don't know Haskell very well myself, just followed a course at University, but it certainly changed how I think about my code and it taught me some nice functional concepts :) My favorite example of how nice, readable and succinct a functional programming and Haskell can be is an implementation of the quick sort algorithm:
qsort [] = []
++ qsort bigger
where
smaller = [a | a <- xs, a <= x]
bigger = [a | a <- xs, a > x]Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
-
I can recommend learning Haskell, a pure functional language. In Haskell all I/O is considered a side-effect (database actions, drawing, printing to console, writing a file, etc.). If you're doing any I/O in Haskell your function must return the special System.IO object. Here's a nice example of Hello World in Haskell[^]. I don't know Haskell very well myself, just followed a course at University, but it certainly changed how I think about my code and it taught me some nice functional concepts :) My favorite example of how nice, readable and succinct a functional programming and Haskell can be is an implementation of the quick sort algorithm:
qsort [] = []
++ qsort bigger
where
smaller = [a | a <- xs, a <= x]
bigger = [a | a <- xs, a > x]Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
Sander Rossel wrote:
I can recommend learning Haskell...
This gave me the same kind of cold shivers I got when I first looked at my first PERL script back in 1998. X|
use strict;
uuse warnings;while ($_ = ) {
chomp $_;
if ($_ =~ /MATCH/) {
say $_;
}
}Plain as the nose on your face, ain't it? :~ Haskell indeed. I'm easing into this functional stuff so let's back away from the extremes. :laugh: Seriously, thanks for your notes and recommendations.
-
Like Richard, I was thinking of an extension method. What you have there is pretty hard to read, took me a while to figure out what it does (due to the nested Tee, which is not a very helpful name either). I'm all for chaining though, that's what LINQ does too (which is pretty functional). Applying functional principles (but remembering C# is not a functional language) really helped me write cleaner and more succinct code. Although not everyone agrees with me, some people prefer their foreach loops and can't read lambda's :sigh: One thing I've learned, and taken to heart, a function has input and predictable output (no side-effects or state!). At some point you're going to have state and output, of course, but that's reserved for special classes. To give an example (from the top of my head, ignore bad practices such as public fields):
public class BadClass
{
public string s;
public void BadClass(string s)
{
this.s = s;
}public void DoubleString()
{
s = s + s;
}
}// Usage
var bc = new BadClass("Hello");
bs.DoubleString();
Console.WriteLine(bc.s); // HelloHellopublic class GoodClass
{
public void DoubleString(string s)
{
return s + s;
}
}// Usage
var gc = new GoodClass();
var s = gc.DoubleString("Hello");
Console.WriteLine(s); // HelloHelloIt's a bit contrived, but you'd be amazed at how often I've seen the BadClass implementation (equivalent) of this. People just love their state and side effects. But then again, I've worked on old VB applications with old VB programmers... As coincidence would have it Packt offers a free Functional C#[^] eBook today, may be interesting (haven't read it myself).
Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
I just ask that, if you've no state, make the damn method static. The amount of times I had to rewrite stuff because people fumbled around with instances of a stateless object and later added state to that object, breaking half the code depending on execution order is uncanny. It's fine to be an instance method if you class has an immutable state. At least nobody would come and remove a
const
orreadonly
from a field without the knowledge that it would break everything... I hope... -
How is that any different from calling
Console.WriteLine("test");
etc.? It's just more code to callConsole.WriteLine
and you aren't chaining anything or using theTee
output :confused: Even worse, anAction<T>
assumes a side-effect because it doesn't return a value. Or isTee
used to "hide" this side-effect? If I read the wiki page I'd suspectTee
does theConsole.WriteLine
and you can pass in aFile.WriteText
or something similar, but even then I fail to see howTee
is helping you. You could just as well call both methods.Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
Sander Rossel wrote:
How is that any different from calling
Console.WriteLine("test");
etc.? It's just more code to callConsole.WriteLine
and you aren't chaining anything or using theTee
output :confused:Using a H
ello World
example would be so much more useful!CQ de W5ALT
Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software