Click here to Skip to main content
15,887,027 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Space junk Pin
Mike Hankey6-Mar-24 3:15
mveMike Hankey6-Mar-24 3:15 
GeneralRe: Space junk Pin
dandy726-Mar-24 6:14
dandy726-Mar-24 6:14 
GeneralRe: Space junk Pin
Member 45835016-Mar-24 20:38
Member 45835016-Mar-24 20:38 
GeneralRe: Space junk Pin
Paul Kemner7-Mar-24 3:13
Paul Kemner7-Mar-24 3:13 
GeneralRe: Space junk Pin
MikeTheFid7-Mar-24 3:19
MikeTheFid7-Mar-24 3:19 
GeneralRe: Space junk Pin
BernardIE53177-Mar-24 5:55
BernardIE53177-Mar-24 5:55 
GeneralRe: Space junk Pin
Mike Hankey7-Mar-24 11:01
mveMike Hankey7-Mar-24 11:01 
GeneralMo Closures and Currying Pin
Jeremy Falcon5-Mar-24 4:26
professionalJeremy Falcon5-Mar-24 4:26 
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.
JavaScript
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.
JavaScript
// 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.
JavaScript
// 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.
JavaScript
// 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 = doTheHardStuff(10)
finishTheJob(20) // doSomethingComputationallyExpensive is only called once
finishTheJob(30) // doSomethingComputationallyExpensive is only called once
Now, as you might imagine, that gets hard core what the fudge. But when you need currying, it's super cool. It's also useful in distributed programming when you want to cut down on network requests without polluting a global/module namespace and not relying on static variables, etc. So, yay currying and closures.
Jeremy Falcon


modified 5-Mar-24 13:15pm.

GeneralRe: Mo Closures and Currying Pin
Jeremy Falcon5-Mar-24 7:05
professionalJeremy Falcon5-Mar-24 7:05 
GeneralRe: Mo Closures and Currying Pin
Richard Deeming5-Mar-24 7:07
mveRichard Deeming5-Mar-24 7:07 
GeneralRe: Mo Closures and Currying Pin
jmaida5-Mar-24 16:21
jmaida5-Mar-24 16:21 
GeneralRe: Mo Closures and Currying Pin
jmaida5-Mar-24 16:48
jmaida5-Mar-24 16:48 
GeneralRe: Mo Closures and Currying Pin
Marc Clifton5-Mar-24 10:45
mvaMarc Clifton5-Mar-24 10:45 
GeneralRe: Mo Closures and Currying Pin
Jeremy Falcon5-Mar-24 10:51
professionalJeremy Falcon5-Mar-24 10:51 
PraiseRe: Mo Closures and Currying Pin
jmaida5-Mar-24 16:22
jmaida5-Mar-24 16:22 
GeneralRe: Mo Closures and Currying Pin
Richard Deeming5-Mar-24 21:47
mveRichard Deeming5-Mar-24 21:47 
GeneralRe: Mo Closures and Currying Pin
jmaida6-Mar-24 11:37
jmaida6-Mar-24 11:37 
GeneralRe: Mo Closures and Currying Pin
Jeremy Falcon6-Mar-24 15:57
professionalJeremy Falcon6-Mar-24 15:57 
GeneralRe: Mo Closures and Currying Pin
Jeremy Falcon6-Mar-24 16:00
professionalJeremy Falcon6-Mar-24 16:00 
GeneralRe: Mo Closures and Currying Pin
jmaida6-Mar-24 16:07
jmaida6-Mar-24 16:07 
GeneralI'm still here PinPopular
#realJSOP5-Mar-24 4:16
mve#realJSOP5-Mar-24 4:16 
JokeRe: I'm still here Pin
Richard Deeming5-Mar-24 4:23
mveRichard Deeming5-Mar-24 4:23 
GeneralRe: I'm still here Pin
PIEBALDconsult5-Mar-24 4:27
mvePIEBALDconsult5-Mar-24 4:27 
GeneralRe: I'm still here Pin
MarkTJohnson5-Mar-24 5:07
professionalMarkTJohnson5-Mar-24 5:07 
GeneralRe: I'm still here Pin
#realJSOP5-Mar-24 6:09
mve#realJSOP5-Mar-24 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.