Last active
May 24, 2019 17:34
-
-
Save jdonaldson/a03722daad4e2842aa509ea910b60bc6 to your computer and use it in GitHub Desktop.
Utility Classes for Haxe
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
using haxe.macro.Context; | |
import haxe.macro.Expr; | |
/** | |
Simple config merge routine. Supply a series of anonymous objects, and this macro will merge them into a single instance. | |
This is useful for providing a series of overrides for config, so I've gone with that name. | |
The benefit is that the macro preserves all of the typing info, so you still get completions, etc. on the merged object. | |
**/ | |
class Config { | |
public static macro function merge<T>(arr : Array<ExprOf<T>>) : Expr { | |
var fields = new Array<ObjectField>(); | |
var seen = new Map<String,Int>(); | |
var type = Context.followWithAbstracts(Context.typeof(a)) | |
for (a in arr){ | |
switch (type){ | |
case TAnonymous(b) : { | |
var k = b.get(); | |
for (f in k.fields){ | |
if (!seen.exists(f.name)){ | |
var name = f.name; | |
fields.push({ | |
field : name, | |
expr : macro ${a}.$name | |
}); | |
seen.set(name,1); | |
} | |
} | |
} | |
default : { | |
Context.error('unsupported: $type', a.pos); | |
} | |
} | |
} | |
return {expr : EObjectDecl(fields), pos : Context.currentPos()}; | |
} | |
} |
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
/** | |
This is the bog standard lazy iterator with map/filter/reduce/each. | |
**/ | |
class Lazy<T> { | |
public var next : Void -> T; | |
public var hasNext : Void -> Bool; | |
public function new(iter : Iterator<T>){ | |
next = iter.next; | |
hasNext = iter.hasNext; | |
} | |
public function map<TMap>( f : T->TMap ) : Lazy<TMap> { | |
return new Lazy({ | |
hasNext: this.hasNext, | |
next : function() { return f(this.next()); } | |
}); | |
} | |
public function filter( f : T->Bool ) : Lazy<T> { | |
var buffer : T = null; | |
if (this.hasNext()){ | |
var hasNextf = function(){ | |
do buffer = this.next() | |
while (!f(buffer) && this.hasNext()); | |
return this.hasNext(); | |
}; | |
var nextf = function() { | |
return buffer; | |
} | |
return new Lazy({ | |
hasNext : hasNextf, | |
next : nextf | |
}); | |
} else { | |
return new Lazy(this); | |
} | |
} | |
public function fold<TSeed>(f : T->TSeed->TSeed, seed : TSeed) { | |
var result = seed; | |
for (v in this){ | |
result = f(v,result); | |
} | |
return result; | |
} | |
public function each(f : T->Void) : Void | |
for (v in this) f(v); | |
public static function lazy_iterator<T>(itb : Iterable<T>) : Lazy<T> | |
return new Lazy(itb.iterator()); | |
public static function lazy<T>(itr : Iterator<T>) : Lazy<T> | |
return new Lazy(itr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment