-1

So I have a small JavaScript function that I need to figure out how to code, as a challenge. Basically:

function me() { // imp this function code }

var isSame1 = me("123")("321") === "123 321";
var isSame2 = me("321")("123") === "321 123";

Desired output is we want both isSame vars to be true. So from what I understand thus far, the me() function needs to return a function initially (some form of recursion I'd imagine) and then somehow a string in order to concat the resulting strings (the real example has some string manipulation during the me() function but I don't need help with that part).

I feel like there is a JavaScript feature that I am not seeing clearly here. I am aware that I can return a function as an object and call it, which is a really neat feature, but the string handling/passing to the other function and then returning it in the end is what is confusing me.

Can anyone point me in the right direction for what to look up. Don't want it to be answered completely for me, just want to be given the right research area.

Gerneio

Gerneio
  • 1,290
  • 10
  • 13
  • 1
    https://stackoverflow.com/questions/111102/how-do-javascript-closures-work?rq=1 – Alexander Nied Apr 02 '18 at 19:12
  • Yes you are right `me` returns a function, which is passed a string argument. This function itself should return a string representing the string after `===`. – George Apr 02 '18 at 19:12
  • 4
    This is basically about [currying](https://blog.benestudio.co/currying-in-javascript-es6-540d2ad09400) – VLAZ Apr 02 '18 at 19:12
  • @AlexanderNied Thanks, that's exactly what I needed! For the rest of y'all, Currying seems applicable too, but too new of a feature, from my understanding. – Gerneio Apr 02 '18 at 19:20
  • @Gerneio currying is not a "feature" - it's a technique. It's also not new - it literally predates JS. Finally, Alexacnder Neid's link is only partially useful in that it's sort of related to how currying is implemented. – VLAZ Apr 02 '18 at 19:23
  • @vlaz maybe so, but it got my exactly what I needed. – Gerneio Apr 02 '18 at 19:25
  • @Gerneio - no problem, glad to provide a hint. But heads up, the statement that currying is "too new of a feature" is not accurate -- currying is not a feature, per se, but a design pattern in functional programming. The linked blog in Patrick Robert's answer simply shows how to do currying in the ES6 "flavor" of JavaScript. – Alexander Nied Apr 02 '18 at 19:25
  • @AlexanderNied cool, thanks for the info, saw the ES6 and tested that type of implementation in Google Script and it did not run, so made an assumption too quickly. – Gerneio Apr 02 '18 at 19:28
  • Also, @vlaz is correct-- I shared that post bc it shared a version of what you were looking for, not because your solution is specifically related to closures. Probably a better link to have shared would have been https://stackoverflow.com/questions/36314/what-is-currying . Sorry to have conflated those ideas for you. – Alexander Nied Apr 02 '18 at 19:29

1 Answers1

1

Currying in JavaScript is quite easy. Just return a scoped function from me().

For example, to implement curried addition using a closure, you could write a function like this:

function add (a) {
  return b => a + b
}

console.log(add(3)(4))

Or see below for the solution to the challenge.

Spoiler (full implementation):

 function me (a) {
   return b => `${a} ${b}`
 }
 
 console.log(me(123)(321))
 console.log(me(321)(123))

Hope this helps you find what you're looking for.

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
  • Currying seems to be a newer language feature, closures is more applicable overall, thanks though! – Gerneio Apr 02 '18 at 19:19
  • @Gerneio currying is not a new language feature. You can curry using arrow functions or normal functions, it just happens to be easier using arrow functions syntactically speaking. – Patrick Roberts Apr 02 '18 at 19:20
  • No, currying is passing parameters with separate invocations, e.g. `(...)(...)` rather than `(..., ...)`. Closures are nested function scopes as a result of a function returning another function, which is how you implement a curried function. – Patrick Roberts Apr 02 '18 at 19:23
  • @Gerneio the two are completely different things. Currying changes a function to accept *fewer* arguments than it already does but instead of the "normal" result, it gives you a new function that will accept the remainder. This is *also* called partial application but these two still aren't synonymous. – VLAZ Apr 02 '18 at 19:26
  • For currying vs partial application: http://2ality.com/2011/09/currying-vs-part-eval.html – Alexander Nied Apr 02 '18 at 19:30
  • @AlexanderNied thanks, I actually tried to google and link but couldn't find it. – VLAZ Apr 02 '18 at 19:33
  • Currying simply returns a function, which is then called with a subsequent set of arguments. It doesn't reduce the number of arguments, it simply provides an opportunity to execute code before the next function call, or to execute it immediately. For instance if you called `me('foo')` it would only return a function to be executed at a later point. You'd be executing it immediately upon return if you did `me('foo')('bar')` (the second set of parentheses is executing the returned function). – BotNet Apr 03 '18 at 14:52