FP question: P1->R->R into P1->R? can it be done?

52 views
Skip to first unread message

S.A. Asselbergs

unread,
Oct 14, 2016, 7:27:36 PM10/14/16
to Haxe
Hi

How to get magicFunction(function(p1:Int):Int return p1, r) returning
function(p1:Int, r:Int):Int return p1 + r; ?
Is it even possible?

//P->R into P->R->R
In P1->R, P1 is the parameterised type of parameter p1 and R is the parametrised type of the return value of the function
In P1->R->R, P1 and the second R are same as above, but the first R the parameterised type of a value where I can store the result of the same function earlier, like this

var funct=function(p1:Int, r:Int):Int return p + r;
trace(funct(1,funct(1,funct(1,0)));  // 1,2,3


Cheers, Simon

S.A. Asselbergs

unread,
Oct 14, 2016, 7:49:51 PM10/14/16
to Haxe
If that is mathematically possible, any P->R can be turned into P->R->R and snap into a recursive loop via code below. That would be awesome! :-)

I use this code to snuggle P->R->R into a recursive function , which turns the example function into a counter

var funct=function(p1:Int, r:Int):Int return p + r;
trace (func.recurseToArray(1, function(r) return r <= 10, 0)); // [1,2,3,4,5,6,7,8,9,10]

    public static function recursion<P1,R>(operation:P1->R->R, p1:P1, condition:R->Bool, result:R, ?beforeRecursion:R->R):R {
        if (beforeRecursion == null){
            beforeRecursion = function(result:R):R{
                return result;
            };
        }       
        var op = operation(p1, result);
        if (condition(op)){
            return recursion(operation, p1, condition, beforeRecursion(op), beforeRecursion);
        }
        return result;             
    }
    public static function recurseToArray<P1,R>(operation:P1->R->R, p1:P1, condition:R->Bool, result:R, ?resultArr:Array<R>, ?beforeRecursion:R->R):Array<R> {
        if (resultArr == null){
            resultArr = new Array<R>();
        }
        if (beforeRecursion == null){
            beforeRecursion = function(result:R):R{
                return result;
            };
        }
        var op = operation(p1, result);
        if (condition(op)){           
            resultArr.push(op);
            return recurseToArray(operation,p1,condition,beforeRecursion(op),resultArr,beforeRecursion);
        }
        return resultArr;
    }   

S.A. Asselbergs

unread,
Oct 15, 2016, 2:30:53 AM10/15/16
to Haxe
It is allready solved. I can now hook StringInstance.charCode(index:Int):String into my recursive P1->R->R code.

//FunctionExtentions.hx

//This code transforms any P->R into P->R->R

    public static function recursableL < P1, R > (funct:P1->R, p1:P1, r:R, f1:R->R->R):P1->R->R{
        var ffunction (p1:P1, r:R):R{
            return f1(funct(p1), r);
        };
       return f;
    }   
    public static function recursableR < P1, R > (funct:P1->R, p1:P1, r:R, f1:R->R->R):R->P1->R{
        var f=function (r:R, p1:P1):R{
            return f1(r,funct(p1));
        };
        return f;
    }

//Some neat usage of my recursive P1->R->R code from my StringExtentions.hx
    public static function indicesOf(t:String, substring:String):Array<Int>{
        var condition = function(index:Int):Bool{
            return ((t.indexOf(substring, index) >-1) && index+substring.length<t.length);       
        }       
        var beforeRecursion = function(index:Int):Int{
            return t.indexOf(substring, index) + substring.length;
        }       
        return t.indexOf.recurseToArray(substring, condition, 0, new Array<Int>(), beforeRecursion);
    }

//So basically I can turn even the smallest standard haxe functions into such a nicely readable thing :-D
// all fits in one return line ;-p
Reply all
Reply to author
Forward
0 new messages