Skip to content

Programming-Duck/challenge-act-when-called-without-arguments

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Call when no arguments

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.

Question, part 1

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

Question, part 2

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.

Running the tests

Prerequisites: Installation requires NPM which is included with Node. You can install Node by downloading the installer from the website.

  1. Clone the repo
git clone https://github.com/sargalias/deep-copy-obj.git
  1. Install NPM packages
npm install

Running the project for development

npm run test

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

About

An interesting functional programming challenge I was asked to implement in an interview.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published