Created
April 23, 2019 12:20
-
-
Save francescoagati/6f300f826fdecd29106fb82e97d32e63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Author
francescoagati
commented
Apr 23, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment