Bags

  1. 3 months ago
    Edited 3 months ago by Zero

    If you put in a bag a number seven 7, a number two 2, a plus sign +, and a number three 3, a human mind would instantly know what can be done with these. We have an operator, three operands... we can sum the numbers, and get 12.

    Computers don't like bags. A typical programming language needs a particular syntax. For example (+ 7 2 3), or +(7, 2, 3), or 7 + 2 + 3. If you don't follow the syntax, you get a syntax error.

    Let's do bags. Let's get rid of syntax.

    It's quite easy to do if you're sure you have only one operator, and the right operands. But if we put a string in our first bag, how do we add numbers and strings? And if we put a second operator, for example *, should we sum numbers or multiply them? Our bag seems unusable in these situations.

    To solve the several operators issue, we need ordering.

    Imagine we have 50 possible functions. If we order them, we can choose what to do when there are several functions in a bag. Our 50 functions list looks like:

    1. sum
    2. multiply
    3. max
    4. min
    5. ...etc.

    If there are several functions in a bag, the highest rank function is selected. Problem solved.

    Now what if we put strings in a bag that's about to multiply things. Well we can just ignore them. Problem solved.

    What good are bags anyway?

    They're good at connecting things without relying on a pre-existing infrastructure.

  2. Edited 3 months ago by Zero
    var log = function(x) { console.log(JSON.stringify(x,null,4));};
    
    var functionList = {
    
        sum: function(arg) {
            var result = 0;
    
            for (a in arg) {
                n = parseFloat(arg[a].value);
                if (!isNaN(n) && isFinite(n)) result += n;
            }
            return result;
        },
    
        avg: function(arg) {
            var result = 0;
            for (a in arg) {
                n = parseFloat(arg[a].value);
                if (!isNaN(n) && isFinite(n)) result += n;
            }
            return result/arg.length;
        },
    
        max: function(arg) {
            var result = null;
            for (a in arg) {
                n = parseFloat(arg[a].value);
                if (!result) result = n;
                else if (!isNaN(n) && isFinite(n) && (result<n))
                    result = n;
            }
            return result;
        },
    
        min: function(arg) {
            var result = null;
            for (a in arg) {
                n = parseFloat(arg[a].value);
                if (!result) result = n;
                else if (!isNaN(n) && isFinite(n) && (result>n))
                    result = n;
            }
            return result;
        }
    
    }
    
    
    var functionOrder = [
        "sum",
        "avg",
        "max",
        "min"
    ]
    
    
    var testBag = [
        {type:"number", value:5},
        {type:"function", value:"max"},
        {type:"number", value:8},
        {type:"number", value:6},
        {type:"function", value:"avg"},
        {type:"number", value:2},
        {type:"number", value:4},
        {type:"number", value:1}
    ]
    
    
    
    function highestRankFunction(bag) {
    
        var highest = functionOrder.length+1;
    
        for (var b in bag) {
            if ((bag[b].type=="function")
            && (functionOrder.indexOf(bag[b].value)>-1))
               if (functionOrder.indexOf(bag[b].value)<highest)
                    highest = functionOrder.indexOf(bag[b].value);
        }
        return functionOrder[highest];
    }
    
    
    function solveBag(bag) {
    
        var fun = highestRankFunction(bag);
    
        return functionList[fun].call(null,bag)
    }
    
    
    
    log("Result: "+solveBag(testBag));
    
    log('done');
    
 

or Sign Up to reply!