Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created April 23, 2019 12:20
Show Gist options
  • Save francescoagati/6f300f826fdecd29106fb82e97d32e63 to your computer and use it in GitHub Desktop.
Save francescoagati/6f300f826fdecd29106fb82e97d32e63 to your computer and use it in GitHub Desktop.
import haxe.macro.Expr;
import haxe.macro.Context;
using haxe.macro.ExprTools;
using haxe.macro.MacroStringTools;
class FunctionThread {
public static macro function thread(exprs:Array<Expr>) {
var exprs = [ for (expr in exprs) macro var _ = $expr ];
exprs.push(macro _);
return macro $b{exprs};
}
public static macro function threadr(exprs:Array<Expr>) {
exprs.reverse();
var exprs = [ for (expr in exprs) macro var _ = $expr ];
exprs.push(macro _);
return macro $b{exprs};
}
}
import FunctionThread.thread;
import FunctionThread.threadr;
class Test {
static function sum(a,b) return a+b;
static function getPointAsObject()
return {x:100,y:300};
static function getPointAsArray()
return [100,300];
static function main() {
var x = 0;
var res = thread(
sum(x,1),
sum(_,1),
sum(_,2),
sum(3,_)
);
trace(res);
var x = 0;
var res = threadr(
1,
sum(_,1),
sum(1,2)
);
trace(res);
var x = 0;
var res = thread(
getPointAsObject(),
sum(_.x,_.y),
sum(_,1),
sum(_,2),
sum(3,_)
);
trace(res);
var x = 0;
var res = thread(
getPointAsArray(),
sum(_[0],_[1]),
sum(_,1),
sum(_,2),
sum(3,_)
);
trace(res);
}
}
@francescoagati
Copy link
Author

// Generated by Haxe 4.0.0-rc.2+032d959
(function ($global) { "use strict";
var HxOverrides = function() { };
HxOverrides.iter = function(a) {
	return { cur : 0, arr : a, hasNext : function() {
		return this.cur < this.arr.length;
	}, next : function() {
		return this.arr[this.cur++];
	}};
};
var Test = function() { };
Test.sum = function(a,b) {
	return a + b;
};
Test.getPointAsObject = function() {
	return { x : 100, y : 300};
};
Test.getPointAsArray = function() {
	return [100,300];
};
Test.main = function() {
	var x = 0;
	var _ = Test.sum(x,1);
	var _1 = Test.sum(_,1);
	var _2 = Test.sum(_1,2);
	var _3 = Test.sum(3,_2);
	var res = _3;
	console.log("root/program/Test.hx:23:",res);
	var x1 = 0;
	var _4 = Test.sum(1,2);
	var _5 = Test.sum(_4,1);
	var _6 = 1;
	var res1 = _6;
	console.log("root/program/Test.hx:32:",res1);
	var x2 = 0;
	var _7 = Test.getPointAsObject();
	var _8 = Test.sum(_7.x,_7.y);
	var _9 = Test.sum(_8,1);
	var _10 = Test.sum(_9,2);
	var _11 = Test.sum(3,_10);
	var res2 = _11;
	console.log("root/program/Test.hx:42:",res2);
	var x3 = 0;
	var _12 = Test.getPointAsArray();
	var _13 = Test.sum(_12[0],_12[1]);
	var _14 = Test.sum(_13,1);
	var _15 = Test.sum(_14,2);
	var _16 = Test.sum(3,_15);
	var res3 = _16;
	console.log("root/program/Test.hx:53:",res3);
};
Test.main();
})({});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment