Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:06:26+00:00 2026-06-03T10:06:26+00:00

Sometimes you have a function that will work for flat arguments. For example: send(player,message)

  • 0

Sometimes you have a function that will work for flat arguments. For example:

send(player,message)

But what if, instead, you have collections of players / messages?

message = ['Welcome!','Check our site for events.']
players = [Player1,Player2,Player3]

Writting for-loops will reduce readability and won’t work if you don’t know statically if your argument is a collection or object. Rewritting the function is sometimes not viable or too laborous and promotes duplicate code. What is a simplier solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:06:33+00:00Added an answer on June 3, 2026 at 10:06 am

    You can write a decorator that will transform your function into a function that will take the cartesian product of it’s own arguments and call the original function on it.

    function send(player,message) {
        console.log('To ',player,': ',message);
    }
    cartesian(send)(['Player1','Player2','Player3'],['Welcome!','Check our site.']);
    //Output:
    //To Player1 : Welcome!
    //To Player1 : Check our site.
    //To Player2 : Welcome!
    //To Player2 : Check our site.
    //To Player3 : Welcome!
    //To Player3 : Check our site.
    

    This implements the decorator (“cartesian“) on Javascript:

    function cartesian_product(arr){
        //cartesian_product( [[1,2],[3,4]] ) = [[1,3],[1,3],[2,3],[2,4]]
    
        function partial_product(arr,i){
            //partial_product([[1,2],3],0) = [[1,3],[2,3]]
            var result = []
            for (j=0; j<arr[i].length; ++j){
                arr_changed = arr.slice();
                arr_changed.splice(i,1,arr[i][j]);
                result.push(arr_changed);
            };
            return result;
        };
    
        var result = [arr.slice()];
        for (var x=0; x<arr.length; ++x){
            for (var y=0; y<result.length; ++y){
                if (result[y][x] instanceof Array) {
                    result.splice.apply(result,[y,1].concat(partial_product(result[y],x)));
                }
            }
        }
        return result;
    };
    
    function cartesian(func){
        //cartesian(func)([1,2],[3,4]) = [func([1,3]),func([1,4]),func([2,3]),func([2,4])]
        _this = this;
        return function(){
            var args_list = cartesian_product(Array.prototype.slice.call(arguments));
            var return_values = []
            for (var i=0; i<args_list.length; ++i){
                return_values.push(func.apply(_this,args_list[i]))
            }
            return return_values;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that takes the exact same args, but sometimes I'd like
I have a click function bound to many elements. It is possible that sometimes
I have a function that returns one or more variables, but as it changes
I have a function, which is called sometimes with regular, sometimes dynamic arrays. If
I'm sometimes confused to using which one of them, say I have a function
I have a problem i can't find the wrong in this function it sometimes
I've been using debug() more often now, but sometimes I wonder which functions have
I see on sites that they sometimes have a statistic showing how many views
I have a large mathematical expression that has to be created dynamically. For example,
I have a LoadingStatus Function that has two options SHOW or HIDE. The Show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.