|
SpaceOut has a side-effect and is not functional by definition
It seems you're spacing out
|
|
|
|
|
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
|
|
|
|
|
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.
|
|
|
|
|
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 (x: xs) = qsort smaller ++ [x] ++ qsort bigger
where
smaller = [a | a <- xs, a <= x]
bigger = [a | a <- xs, a > x]
|
|
|
|
|
|
Quote:
public static Byte[] GetBytes( this String @inVal){
Byte [] outBytes = new Byte[@inVal.Length];
int loopCount = 0;
foreach (Char c in @inVal){
outBytes[loopCount] = Convert.ToByte(c);
loopCount++;
}
return outBytes;
}
That should probably be:
public static Byte[] GetBytes( this String @inVal ) => System.Text.Encoding.UTF8.GetBytes(@inVal);
Remember, some characters aren't single bytes.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: Remember, some characters aren't single bytes
Very good.
A very amateur mistake by me.
I was excited about the idea and didn't think about the code that is already there for me.
Lame!
Thanks again!
|
|
|
|
|
Why is an @ sign being used for @inVal? Aren't those only used for naming a variable after a reserved keyword?
|
|
|
|
|
icemanind wrote: Why is an @ sign being used for @inVal?
I wondered the same thing.
This was the choice of the author/presenter and I hadn't seen that before either.
He actually had his param named @this so it was even worse to me in that way.
I didn't know C# allowed a var to be named with a beginning @ sign either, but apparently you can.
|
|
|
|
|
I knew you could have an @ sign in it. And it makes sense for @this because "this" is a reserved keyword in C#. Without the @ sign, naming a variable "this" would cause an error. But there is no keyword called "inVar", which is why I was confused about that one.
|
|
|
|
|
Honestly, it was me being ignorant.
However, you've made me think about this and I get the point now.
It sounds like I'm being sarcastic but I'm really not.
I know this thread has been odd but it has really made me rethink and firstThink a number of things.
|
|
|
|
|
Hi, Raddevus,
Really enjoying this discussion !
It would be interesting if you could, somehow:
public static T TeeHee<T>(this T tee, params Func<T, T>[] funcs)
{
foreach (var func in funcs)
{
}
return tee;
} But, the obvious problem is that all the funcs have to have the same return type.
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
|
|
|
|
|
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,
i => $"The answer is {i}",
i => new Answer(i)
);
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
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
Replace params Func<T, T>[] funcs simply with <IEnumerable<Action<T>>> and you're good to go.
Marc
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
|
|
|
|
|
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
|
|
|
|
|
|
Thanks very much. I will read that later today.
I was looking for just such a resource.
Love it when the answers just crash into me without me having to exert any energy.
|
|
|
|
|
I'll get a static initialised without a explicit ctor if it kills me!
header:
struct client {
static client dummy;
std::string username;
int id;
} cpp:
client client::dummy = std::move(*[=] { client * c = new client();
c->username = "<DUMMY>";
c->id = -1;
return c;
}()
); I'm not going to use this code but I wonder about it... memory leak?
|
|
|
|
|
And you are sure you didn't miss a way to write it more unreadable?
It does not solve my Problem, but it answers my question
modified 19-Jan-21 21:04pm.
|
|
|
|
|
I'm used to C# so tried
return new client(){username = "<DUMMY>"; } but didn't get told off about it until compile-time
|
|
|
|
|
Why does the struct have to have a static copy of itself in it? Why can't you just do:
struct client {
std::string username;
int id; }
static client theInstance;
theInstance.username = "<DUMMY>";
...
|
|
|
|