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. Other Discussions
  3. The Weird and The Wonderful
  4. Interesting / strange code picked up from pluralsight training (functional programming)

Interesting / strange code picked up from pluralsight training (functional programming)

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpcomfunctionaltutorial
49 Posts 12 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.
  • R raddevus

    I was watching a very good intro to Functional programming on PluralSight (Functional Programming with C# | Pluralsight[^]) and the author / presenter created the following method (mine has altered var names).

    public static class Helper{
    public static T Tee(
    T @inVal,
    Action act){
    act(@inVal);
    return @inVal;
    }
    }

    Now you can call that method like the following:

    Helper.Tee("test", Console.WriteLine);
    Helper.Tee(3.238, Console.WriteLine);
    Helper.Tee (new {garbage="super"},Console.WriteLine);

    Here's the output:

    test
    3.238
    { garbage = super }

    It's loosely based on the following idea (why it's named Tee): tee (command) - Wikipedia[^] Just thought it was an interesting example and it made me think differently about things. After all these years of OOP I'm beginning to see the real value in the Functional paradigm*. *Obviously the included sample is not a huge example of Functional programming in and of itself.

    B Offline
    B Offline
    Bernhard Hiller
    wrote on last edited by
    #3

    What a weird, but interesting, idea. I think a better example than Helper.Tee("test", Console.WriteLine); could be string result = Helper.Tee("test", Console.WriteLine).ToUpper(); That shows the "T" character of that function more clearly: since it returns the input parameter, you can chain a few functions.

    Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

    R 1 Reply Last reply
    0
    • Sander RosselS Sander Rossel

      How is that any different from calling Console.WriteLine("test"); etc.? It's just more code to call Console.WriteLine and you aren't chaining anything or using the Tee output :confused: Even worse, an Action<T> assumes a side-effect because it doesn't return a value. Or is Tee used to "hide" this side-effect? If I read the wiki page I'd suspect Tee does the Console.WriteLine and you can pass in a File.WriteText or something similar, but even then I fail to see how Tee 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

      R Offline
      R Offline
      raddevus
      wrote on last edited by
      #4

      I like your questions because I'm learning the concepts and determining how these things are important too. And I can tell by your questions that you have more functional experience than I do. Here's an example that may make more sense -- it's like a before and after test.

      Helper.Tee(Helper.Tee(" _ before after _ ", Console.WriteLine)
      .ToUpper().Trim()
      .Substring(8,6),
      Console.WriteLine);

      That results in an output like the following:

      _ before after _
      AFTER

      Because the Tee method returns the value methods can be chained just like the normal string methods and so you can see the BEFORE version of your string and then the AFTER version. I don't know if that is helpful either, but it's interesting. This was a very small portion of an example showing how to create fluent APIs.

      R Sander RosselS 2 Replies Last reply
      0
      • R raddevus

        I was watching a very good intro to Functional programming on PluralSight (Functional Programming with C# | Pluralsight[^]) and the author / presenter created the following method (mine has altered var names).

        public static class Helper{
        public static T Tee(
        T @inVal,
        Action act){
        act(@inVal);
        return @inVal;
        }
        }

        Now you can call that method like the following:

        Helper.Tee("test", Console.WriteLine);
        Helper.Tee(3.238, Console.WriteLine);
        Helper.Tee (new {garbage="super"},Console.WriteLine);

        Here's the output:

        test
        3.238
        { garbage = super }

        It's loosely based on the following idea (why it's named Tee): tee (command) - Wikipedia[^] Just thought it was an interesting example and it made me think differently about things. After all these years of OOP I'm beginning to see the real value in the Functional paradigm*. *Obviously the included sample is not a huge example of Functional programming in and of itself.

        S Offline
        S Offline
        Super Lloyd
        wrote on last edited by
        #5

        this example is relatively convoluted and pointless. you find better and common use of functional programming in LINQ to Object! ;P

        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

        R 1 Reply Last reply
        0
        • B Bernhard Hiller

          What a weird, but interesting, idea. I think a better example than Helper.Tee("test", Console.WriteLine); could be string result = Helper.Tee("test", Console.WriteLine).ToUpper(); That shows the "T" character of that function more clearly: since it returns the input parameter, you can chain a few functions.

          Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

          R Offline
          R Offline
          raddevus
          wrote on last edited by
          #6

          You are exactly correct about the return being the important part because it allows you to chain the methods. In another post I mentioned that you can do a "before and after" type of test which might look like:

          Helper.Tee(Helper.Tee(" _ before after _ ", Console.WriteLine)
          .ToUpper().Trim()
          .Substring(8,6),
          Console.WriteLine);

          That results in an output like the following:

          _ before after _
          AFTER

          1 Reply Last reply
          0
          • S Super Lloyd

            this example is relatively convoluted and pointless. you find better and common use of functional programming in LINQ to Object! ;P

            A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

            R Offline
            R Offline
            raddevus
            wrote on last edited by
            #7

            Yes, it does look that way...and I'm learning this stuff myself. However, I've provided a slightly better example in reply to Sander at: The Weird and The Wonderful[^] That at least makes a bit of sense. :)

            S 1 Reply Last reply
            0
            • R raddevus

              Yes, it does look that way...and I'm learning this stuff myself. However, I've provided a slightly better example in reply to Sander at: The Weird and The Wonderful[^] That at least makes a bit of sense. :)

              S Offline
              S Offline
              Super Lloyd
              wrote on last edited by
              #8

              it's cute, you are learning new stuff. that is good. :) The Tee code sample is contrived though, simply because it can be rewritten much more mean and lean like

              var value = ...;
              action(value)
              // ... continue ...

              it was, to put it bluntly, grossly over engineered, and should not be done in real work project! :-o However here is something like the given Tee function that could be more useful

              public static class Helper
              {
              public static IEnumerable<T> Tee(this IEnumerable<T> enumerable, Action<T> action)
              {
              foreach (var e in enumerable)
              {
              action(e);
              yield return e;
              }
              }
              }

              A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

              R 1 Reply Last reply
              0
              • S Super Lloyd

                it's cute, you are learning new stuff. that is good. :) The Tee code sample is contrived though, simply because it can be rewritten much more mean and lean like

                var value = ...;
                action(value)
                // ... continue ...

                it was, to put it bluntly, grossly over engineered, and should not be done in real work project! :-o However here is something like the given Tee function that could be more useful

                public static class Helper
                {
                public static IEnumerable<T> Tee(this IEnumerable<T> enumerable, Action<T> action)
                {
                foreach (var e in enumerable)
                {
                action(e);
                yield return e;
                }
                }
                }

                A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                R Offline
                R Offline
                Richard Deeming
                wrote on last edited by
                #9

                It might help if you gave the method a name! :-D


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                S 1 Reply Last reply
                0
                • R raddevus

                  I like your questions because I'm learning the concepts and determining how these things are important too. And I can tell by your questions that you have more functional experience than I do. Here's an example that may make more sense -- it's like a before and after test.

                  Helper.Tee(Helper.Tee(" _ before after _ ", Console.WriteLine)
                  .ToUpper().Trim()
                  .Substring(8,6),
                  Console.WriteLine);

                  That results in an output like the following:

                  _ before after _
                  AFTER

                  Because the Tee method returns the value methods can be chained just like the normal string methods and so you can see the BEFORE version of your string and then the AFTER version. I don't know if that is helpful either, but it's interesting. This was a very small portion of an example showing how to create fluent APIs.

                  R Offline
                  R Offline
                  Richard Deeming
                  wrote on last edited by
                  #10

                  This definitely looks like a case for an extension method[^]. :)

                  " _ before after _ "
                  .Tee(Console.WriteLine)
                  .ToUpper().Trim()
                  .Substring(8, 6)
                  .Tee(Console.WriteLine);


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  R 2 Replies Last reply
                  0
                  • R Richard Deeming

                    It might help if you gave the method a name! :-D


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    S Offline
                    S Offline
                    Super Lloyd
                    wrote on last edited by
                    #11

                    oops.. corrected! :-\

                    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                    R 1 Reply Last reply
                    0
                    • R Richard Deeming

                      This definitely looks like a case for an extension method[^]. :)

                      " _ before after _ "
                      .Tee(Console.WriteLine)
                      .ToUpper().Trim()
                      .Substring(8, 6)
                      .Tee(Console.WriteLine);


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      R Offline
                      R Offline
                      raddevus
                      wrote on last edited by
                      #12

                      Ah, yes, that would be even better. Makes far more sense that way. :thumbsup:

                      1 Reply Last reply
                      0
                      • R Richard Deeming

                        This definitely looks like a case for an extension method[^]. :)

                        " _ before after _ "
                        .Tee(Console.WriteLine)
                        .ToUpper().Trim()
                        .Substring(8, 6)
                        .Tee(Console.WriteLine);


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        R Offline
                        R Offline
                        raddevus
                        wrote on last edited by
                        #13

                        That was interesting to me so I altered the Tee method to make it an extension method. Simply add the _this_ to the first param T and your code works now.

                        public static class Helper{
                        public static T Tee(
                        this T @inVal,
                        Action act){
                        act(@inVal);
                        return @inVal;
                        }
                        }

                        " _ before after _ "
                        .Tee(Console.WriteLine)
                        .ToUpper().Trim()
                        .Substring(8,6)
                        .Tee(Console.WriteLine);

                        1 Reply Last reply
                        0
                        • S Super Lloyd

                          oops.. corrected! :-\

                          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                          R Offline
                          R Offline
                          raddevus
                          wrote on last edited by
                          #14

                          Ohh...look...I've really gone crazy with this now.

                          public static class Helper{
                          public static T Tee(
                          this T @inVal,
                          Action act){
                          act(@inVal);
                          return @inVal;
                          }

                          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;
                          }
                          
                          public static String DisplayBytes(this byte\[\] inBytes){
                          	String outVal = String.Empty;
                          	foreach (Byte b in inBytes){
                          		outVal += Convert.ToString($"{b} ");
                          	}
                          	return outVal;
                          }
                          

                          }

                          Try it like this and you get before and after again:

                          "What up!"
                          .Tee(Console.WriteLine)
                          .GetBytes()
                          .DisplayBytes()
                          .Tee(Console.WriteLine);

                          Output looks like:

                          What up!
                          87 104 97 116 32 117 112 33

                          :cool: Well, it's fun.

                          L R 2 Replies Last reply
                          0
                          • R raddevus

                            Ohh...look...I've really gone crazy with this now.

                            public static class Helper{
                            public static T Tee(
                            this T @inVal,
                            Action act){
                            act(@inVal);
                            return @inVal;
                            }

                            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;
                            }
                            
                            public static String DisplayBytes(this byte\[\] inBytes){
                            	String outVal = String.Empty;
                            	foreach (Byte b in inBytes){
                            		outVal += Convert.ToString($"{b} ");
                            	}
                            	return outVal;
                            }
                            

                            }

                            Try it like this and you get before and after again:

                            "What up!"
                            .Tee(Console.WriteLine)
                            .GetBytes()
                            .DisplayBytes()
                            .Tee(Console.WriteLine);

                            Output looks like:

                            What up!
                            87 104 97 116 32 117 112 33

                            :cool: Well, it's fun.

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

                            It doesn't copy stdin to stdout. The only thing in common with "tee" is in the amount of parameters.

                            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                            R 1 Reply Last reply
                            0
                            • L Lost User

                              It doesn't copy stdin to stdout. The only thing in common with "tee" is in the amount of parameters.

                              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                              R Offline
                              R Offline
                              raddevus
                              wrote on last edited by
                              #16

                              Yes, you are correct. I think the point that the author/presenter was attempting to make is that you can output the value and continue processing the value as input to yet another function. That's why the author/presenter named the method Tee (which I hadn't seen before). I looked it up and found the associated wiki article and just thought that was an interesting piece of history.

                              L 1 Reply Last reply
                              0
                              • R raddevus

                                Yes, you are correct. I think the point that the author/presenter was attempting to make is that you can output the value and continue processing the value as input to yet another function. That's why the author/presenter named the method Tee (which I hadn't seen before). I looked it up and found the associated wiki article and just thought that was an interesting piece of history.

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

                                raddevus wrote:

                                I think the point that the author/presenter was attempting to make is that you can output the value and continue processing the value as input to yet another function.

                                Like a decorator..

                                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                                R 1 Reply Last reply
                                0
                                • L Lost User

                                  raddevus wrote:

                                  I think the point that the author/presenter was attempting to make is that you can output the value and continue processing the value as input to yet another function.

                                  Like a decorator..

                                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                                  R Offline
                                  R Offline
                                  raddevus
                                  wrote on last edited by
                                  #18

                                  Eddy Vluggen wrote:

                                  Like a decorator..

                                  cue Madonna

                                  ...touched for the very first time...

                                  :laugh: I could not pass that up. Ignoring the bad joke (if possible) I think your point is really interesting, because it is like a decorator. Also, in an effort to completely beat this dead horse, how about the following addition? If, nothing else, the added method has a great name : see SpaceOut.

                                  public static class Helper{
                                  public static T Tee(
                                  this T @inVal,
                                  Action act){
                                  act(@inVal);
                                  return @inVal;
                                  }

                                  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;
                                  }
                                  
                                  public static String DisplayBytes(this byte\[\] inBytes){
                                  	String outVal = String.Empty;
                                  	foreach (Byte b in inBytes){
                                  		outVal += Convert.ToString($"{b:D3} ");
                                  	}
                                  	return outVal;
                                  }
                                  
                                  public static String SpaceOut(this string @inVal){
                                  	StringBuilder spacedItem = new StringBuilder();
                                  
                                  	foreach (Char c in @inVal){
                                  		spacedItem.Append($" {c}    ");
                                  	}
                                  	Console.WriteLine(spacedItem.ToString());
                                  	return @inVal;
                                  }
                                  

                                  }

                                  Now you can do this:

                                  "What up!"
                                  .Tee(Console.WriteLine)
                                  .SpaceOut()
                                  .GetBytes()
                                  .DisplayBytes()
                                  .Tee(Console.WriteLine);

                                  And you will get the following:

                                  What up!
                                  W h a t u p !
                                  087 104 097 116 032 117 112 033

                                  Additionally interesting (or not) is that SpaceOut simply passes the input string along with no change since you only want the input to be printed with the extra spaces but don't want the output altered in this case. I got a million of 'em! :laugh:

                                  L N Sander RosselS 3 Replies Last reply
                                  0
                                  • R raddevus

                                    Eddy Vluggen wrote:

                                    Like a decorator..

                                    cue Madonna

                                    ...touched for the very first time...

                                    :laugh: I could not pass that up. Ignoring the bad joke (if possible) I think your point is really interesting, because it is like a decorator. Also, in an effort to completely beat this dead horse, how about the following addition? If, nothing else, the added method has a great name : see SpaceOut.

                                    public static class Helper{
                                    public static T Tee(
                                    this T @inVal,
                                    Action act){
                                    act(@inVal);
                                    return @inVal;
                                    }

                                    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;
                                    }
                                    
                                    public static String DisplayBytes(this byte\[\] inBytes){
                                    	String outVal = String.Empty;
                                    	foreach (Byte b in inBytes){
                                    		outVal += Convert.ToString($"{b:D3} ");
                                    	}
                                    	return outVal;
                                    }
                                    
                                    public static String SpaceOut(this string @inVal){
                                    	StringBuilder spacedItem = new StringBuilder();
                                    
                                    	foreach (Char c in @inVal){
                                    		spacedItem.Append($" {c}    ");
                                    	}
                                    	Console.WriteLine(spacedItem.ToString());
                                    	return @inVal;
                                    }
                                    

                                    }

                                    Now you can do this:

                                    "What up!"
                                    .Tee(Console.WriteLine)
                                    .SpaceOut()
                                    .GetBytes()
                                    .DisplayBytes()
                                    .Tee(Console.WriteLine);

                                    And you will get the following:

                                    What up!
                                    W h a t u p !
                                    087 104 097 116 032 117 112 033

                                    Additionally interesting (or not) is that SpaceOut simply passes the input string along with no change since you only want the input to be printed with the extra spaces but don't want the output altered in this case. I got a million of 'em! :laugh:

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

                                    :laugh:

                                    raddevus wrote:

                                    I got a million of 'em! :laugh:

                                    Is that due to .NET being functional, or due to OO and your result being an object? :-\

                                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                                    R 1 Reply Last reply
                                    0
                                    • R raddevus

                                      Eddy Vluggen wrote:

                                      Like a decorator..

                                      cue Madonna

                                      ...touched for the very first time...

                                      :laugh: I could not pass that up. Ignoring the bad joke (if possible) I think your point is really interesting, because it is like a decorator. Also, in an effort to completely beat this dead horse, how about the following addition? If, nothing else, the added method has a great name : see SpaceOut.

                                      public static class Helper{
                                      public static T Tee(
                                      this T @inVal,
                                      Action act){
                                      act(@inVal);
                                      return @inVal;
                                      }

                                      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;
                                      }
                                      
                                      public static String DisplayBytes(this byte\[\] inBytes){
                                      	String outVal = String.Empty;
                                      	foreach (Byte b in inBytes){
                                      		outVal += Convert.ToString($"{b:D3} ");
                                      	}
                                      	return outVal;
                                      }
                                      
                                      public static String SpaceOut(this string @inVal){
                                      	StringBuilder spacedItem = new StringBuilder();
                                      
                                      	foreach (Char c in @inVal){
                                      		spacedItem.Append($" {c}    ");
                                      	}
                                      	Console.WriteLine(spacedItem.ToString());
                                      	return @inVal;
                                      }
                                      

                                      }

                                      Now you can do this:

                                      "What up!"
                                      .Tee(Console.WriteLine)
                                      .SpaceOut()
                                      .GetBytes()
                                      .DisplayBytes()
                                      .Tee(Console.WriteLine);

                                      And you will get the following:

                                      What up!
                                      W h a t u p !
                                      087 104 097 116 032 117 112 033

                                      Additionally interesting (or not) is that SpaceOut simply passes the input string along with no change since you only want the input to be printed with the extra spaces but don't want the output altered in this case. I got a million of 'em! :laugh:

                                      N Offline
                                      N Offline
                                      Nelek
                                      wrote on last edited by
                                      #20

                                      You look like a kid with brand shiny new shoes :rolleyes: :rolleyes: ;P ;P :laugh: :laugh:

                                      M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                                      R 1 Reply Last reply
                                      0
                                      • R raddevus

                                        I like your questions because I'm learning the concepts and determining how these things are important too. And I can tell by your questions that you have more functional experience than I do. Here's an example that may make more sense -- it's like a before and after test.

                                        Helper.Tee(Helper.Tee(" _ before after _ ", Console.WriteLine)
                                        .ToUpper().Trim()
                                        .Substring(8,6),
                                        Console.WriteLine);

                                        That results in an output like the following:

                                        _ before after _
                                        AFTER

                                        Because the Tee method returns the value methods can be chained just like the normal string methods and so you can see the BEFORE version of your string and then the AFTER version. I don't know if that is helpful either, but it's interesting. This was a very small portion of an example showing how to create fluent APIs.

                                        Sander RosselS Offline
                                        Sander RosselS Offline
                                        Sander Rossel
                                        wrote on last edited by
                                        #21

                                        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); // HelloHello

                                        public class GoodClass
                                        {
                                        public void DoubleString(string s)
                                        {
                                        return s + s;
                                        }
                                        }

                                        // Usage
                                        var gc = new GoodClass();
                                        var s = gc.DoubleString("Hello");
                                        Console.WriteLine(s); // HelloHello

                                        It'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

                                        B S 2 Replies Last reply
                                        0
                                        • R raddevus

                                          I was watching a very good intro to Functional programming on PluralSight (Functional Programming with C# | Pluralsight[^]) and the author / presenter created the following method (mine has altered var names).

                                          public static class Helper{
                                          public static T Tee(
                                          T @inVal,
                                          Action act){
                                          act(@inVal);
                                          return @inVal;
                                          }
                                          }

                                          Now you can call that method like the following:

                                          Helper.Tee("test", Console.WriteLine);
                                          Helper.Tee(3.238, Console.WriteLine);
                                          Helper.Tee (new {garbage="super"},Console.WriteLine);

                                          Here's the output:

                                          test
                                          3.238
                                          { garbage = super }

                                          It's loosely based on the following idea (why it's named Tee): tee (command) - Wikipedia[^] Just thought it was an interesting example and it made me think differently about things. After all these years of OOP I'm beginning to see the real value in the Functional paradigm*. *Obviously the included sample is not a huge example of Functional programming in and of itself.

                                          I Offline
                                          I Offline
                                          icemanind
                                          wrote on last edited by
                                          #22

                                          Why is an @ sign being used for @inVal? Aren't those only used for naming a variable after a reserved keyword?

                                          R 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