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. Mo Closures and Currying

Mo Closures and Currying

Scheduled Pinned Locked Moved The Lounge
comdata-structuresfunctionaltutorialquestion
14 Posts 5 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.
  • P Poopoopeepee0

    kys

    J Offline
    J Offline
    Jeremy Falcon
    wrote on last edited by
    #3

    I realize this is a joke, but talking about CPU registers and protected memory may seem like a lot to a newbie. As a point of reference.

    Jeremy Falcon

    Richard DeemingR 1 Reply Last reply
    0
    • J Jeremy Falcon

      I realize this is a joke, but talking about CPU registers and protected memory may seem like a lot to a newbie. As a point of reference.

      Jeremy Falcon

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #4

      DNFTT. :) Spam and Abuse Watch[^]


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

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

      J 2 Replies Last reply
      0
      • J Jeremy Falcon

        I mentioned to @jmaida I'd give another example of closures in the language that shall not be named (rhymes with GuavaScript). Closures can not only be used to fake OOP, as it were. They can also be applied to functional concepts. In functional programming, there's a concept called currying. And like anything in tech, it sounds way more complicated than it is.

        const fooBar = function(foo) {
        // this is a curried function, essentially a function returning a function
        return function(bar) {
        return foo + bar; // the whole deal with closures and foo should look familiar
        };
        };

        Let's get fancy now with some modern syntax.

        // this is a curried arrow function, notice the double =>, it's a function returning another function
        // foo is treated just the same as in the previous example
        const fooBar = foo => bar => `${foo} and ${bar} sitting in a tree`;

        // these will print the outer and inner functions that was returned
        console.log(fooBar, fooBar());

        // this will call the outer function and then the inner function
        console.log(fooBar('sup')('dawg')); // outputs "sup and dawg sitting in a tree"

        So you're probably thinking, ok but that's whack. What the fudge do people use currying for? Two reasons actually. Let's say you got a function to call with the same parameters over and over again, and it just looks nasty. And nasty code is... well nasty. You can clean it up with a curry.

        // using the example above
        const supDawg = fooBar('sup')('dawg');

        // look ma, no params
        console.log(supDawg); // outputs "sup and dawg sitting in a tree"

        Ok, so that's cool. But let's be real, it's not practical. A practical application of currying would be analogous to an abstract base class in OOP-land and how that helps with reuse. Also, kinda like how an abstract class is never supposed to be directly instantiated, the "abstract" logic should never be directly called outside the scope of the outer function. But, being functional, you get another benefit too.

        // happily ganked this code snippet from SO
        const doTheHardStuff = function(x) {
        // the outer function is the abstract part, should make sense to dyslexics
        // its calling doSomethingComputationallyExpensive, which shouldn't be directly called
        const z = doSomethingComputationallyExpensive(x);

        return function (y) {
        z + y;
        }
        }

        const finishTheJob = doTheHa

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

        There are rare times when I wish currying in C# was as simple as it is in F#. Code rant: Currying in C# with Oliver Sturm[^] Granted, a 2008 post.

        Latest Articles:
        A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework

        J 1 Reply Last reply
        0
        • M Marc Clifton

          There are rare times when I wish currying in C# was as simple as it is in F#. Code rant: Currying in C# with Oliver Sturm[^] Granted, a 2008 post.

          Latest Articles:
          A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework

          J Offline
          J Offline
          Jeremy Falcon
          wrote on last edited by
          #6

          That's cool though, pulling it off in C#. Major nerd points awarded.

          Jeremy Falcon

          1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            DNFTT. :) Spam and Abuse Watch[^]


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

            J Offline
            J Offline
            jmaida
            wrote on last edited by
            #7

            Hey, honestly asking.

            "A little time, a little trouble, your better day" Badfinger

            1 Reply Last reply
            0
            • J Jeremy Falcon

              I mentioned to @jmaida I'd give another example of closures in the language that shall not be named (rhymes with GuavaScript). Closures can not only be used to fake OOP, as it were. They can also be applied to functional concepts. In functional programming, there's a concept called currying. And like anything in tech, it sounds way more complicated than it is.

              const fooBar = function(foo) {
              // this is a curried function, essentially a function returning a function
              return function(bar) {
              return foo + bar; // the whole deal with closures and foo should look familiar
              };
              };

              Let's get fancy now with some modern syntax.

              // this is a curried arrow function, notice the double =>, it's a function returning another function
              // foo is treated just the same as in the previous example
              const fooBar = foo => bar => `${foo} and ${bar} sitting in a tree`;

              // these will print the outer and inner functions that was returned
              console.log(fooBar, fooBar());

              // this will call the outer function and then the inner function
              console.log(fooBar('sup')('dawg')); // outputs "sup and dawg sitting in a tree"

              So you're probably thinking, ok but that's whack. What the fudge do people use currying for? Two reasons actually. Let's say you got a function to call with the same parameters over and over again, and it just looks nasty. And nasty code is... well nasty. You can clean it up with a curry.

              // using the example above
              const supDawg = fooBar('sup')('dawg');

              // look ma, no params
              console.log(supDawg); // outputs "sup and dawg sitting in a tree"

              Ok, so that's cool. But let's be real, it's not practical. A practical application of currying would be analogous to an abstract base class in OOP-land and how that helps with reuse. Also, kinda like how an abstract class is never supposed to be directly instantiated, the "abstract" logic should never be directly called outside the scope of the outer function. But, being functional, you get another benefit too.

              // happily ganked this code snippet from SO
              const doTheHardStuff = function(x) {
              // the outer function is the abstract part, should make sense to dyslexics
              // its calling doSomethingComputationallyExpensive, which shouldn't be directly called
              const z = doSomethingComputationallyExpensive(x);

              return function (y) {
              z + y;
              }
              }

              const finishTheJob = doTheHa

              J Offline
              J Offline
              jmaida
              wrote on last edited by
              #8

              Thank you. Much appreciated. A lot to chew on. BTW contrary to Richard's Demming's jab DNFTT. Smile | :) I am not trolling. I am honestly trying to learn concepts new to me.

              "A little time, a little trouble, your better day" Badfinger

              Richard DeemingR J 3 Replies Last reply
              0
              • Richard DeemingR Richard Deeming

                DNFTT. :) Spam and Abuse Watch[^]


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

                J Offline
                J Offline
                jmaida
                wrote on last edited by
                #9

                I have a ZX Spectrum somewhere in my closet.

                "A little time, a little trouble, your better day" Badfinger

                1 Reply Last reply
                0
                • J jmaida

                  Thank you. Much appreciated. A lot to chew on. BTW contrary to Richard's Demming's jab DNFTT. Smile | :) I am not trolling. I am honestly trying to learn concepts new to me.

                  "A little time, a little trouble, your better day" Badfinger

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

                  jmaida wrote:

                  contrary to Richard's Demming's jab DNFTT

                  You seem to be labouring under the misapprehension that I was referring to you. I was not. Follow the link in my message: there was a troll with the highly amusing username of "poopoopeepee0", whose since-deleted message Jeremy was replying to. When I posted my comment, you hadn't even replied to the thread. So unless that troll was a sock puppet account you had created, I don't understand your confusion. :confused: Edit: Unless you thought I was referring back to your comments in Marc's thread from yesterday[^]? Which I wasn't.


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

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

                  J 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    jmaida wrote:

                    contrary to Richard's Demming's jab DNFTT

                    You seem to be labouring under the misapprehension that I was referring to you. I was not. Follow the link in my message: there was a troll with the highly amusing username of "poopoopeepee0", whose since-deleted message Jeremy was replying to. When I posted my comment, you hadn't even replied to the thread. So unless that troll was a sock puppet account you had created, I don't understand your confusion. :confused: Edit: Unless you thought I was referring back to your comments in Marc's thread from yesterday[^]? Which I wasn't.


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

                    J Offline
                    J Offline
                    jmaida
                    wrote on last edited by
                    #11

                    no sweat. My error. I'm finding some issues with my postings. found this earlier post "I have a ZX Spectrum somewhere in my closet." in a totally unrelated thread. probably user error.

                    "A little time, a little trouble, your better day" Badfinger

                    1 Reply Last reply
                    0
                    • J jmaida

                      Thank you. Much appreciated. A lot to chew on. BTW contrary to Richard's Demming's jab DNFTT. Smile | :) I am not trolling. I am honestly trying to learn concepts new to me.

                      "A little time, a little trouble, your better day" Badfinger

                      J Offline
                      J Offline
                      Jeremy Falcon
                      wrote on last edited by
                      #12

                      You’re totally welcome. Btw, Richard was referring to some posts that were deleted. Was a different dude he was talking about, promise.

                      Jeremy Falcon

                      1 Reply Last reply
                      0
                      • J jmaida

                        Thank you. Much appreciated. A lot to chew on. BTW contrary to Richard's Demming's jab DNFTT. Smile | :) I am not trolling. I am honestly trying to learn concepts new to me.

                        "A little time, a little trouble, your better day" Badfinger

                        J Offline
                        J Offline
                        Jeremy Falcon
                        wrote on last edited by
                        #13

                        Also there a ton of functional concepts worth chatting about. Maybe it’s my C background but I love the functional paradigm more than OOP. It’s like the cool kid that nobody talks about.

                        Jeremy Falcon

                        J 1 Reply Last reply
                        0
                        • J Jeremy Falcon

                          Also there a ton of functional concepts worth chatting about. Maybe it’s my C background but I love the functional paradigm more than OOP. It’s like the cool kid that nobody talks about.

                          Jeremy Falcon

                          J Offline
                          J Offline
                          jmaida
                          wrote on last edited by
                          #14

                          :) I will be getting back after I digest the material you provided. Like going back to school.

                          "A little time, a little trouble, your better day" Badfinger

                          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