This is an interesting functional programming challenge I was asked to implement in an interview a few years ago. I found it very interesting, so I thought I would post it.
Design a function that you can keep calling in a chain as long as you keep passing in arguments. During this time, it doesn't do anything except collect those arguments somehow. When you call the function without passing an argument, that's when it calls a real function, such as console.log
, with all of the arguments you've passed in so far.
Here is a code example:
foo('a'); // nothing happens
foo('a')('b'); // nothing happens
foo('a')(); // logs 'a' (calls console.log('a'))
foo('a')('b')() // logs 'a b' (calls console.log('a', 'b'))
foo('a')('b')('c')() // logs 'a b c' (calls console.log('a', 'b', 'c'))'
Can you create a function similar to question 1, except make it "stateless"?
Different function calls shouldn't share state with each other.
For example:
const a = foo('a');
const ab = a('b');
const abc = ab('c');
a(); // logs 'a' (calls console.log('a'))
abc(); // logs 'a b c' (calls console.log('a', 'b', 'c'))
ab(); // logs 'a b' (calls console.log('a', 'b'))
If you're ready for the challenge, have fun! I've included my solutions in this repo.
Prerequisites: Installation requires NPM which is included with Node. You can install Node by downloading the installer from the website.
- Clone the repo
git clone https://github.com/sargalias/deep-copy-obj.git
- Install NPM packages
npm install
npm run test
This project is licensed under the MIT License - see the LICENSE.md file for details.