Skip to content

Instantly share code, notes, and snippets.

@jdonaldson
Last active February 25, 2017 22:41
Show Gist options
  • Save jdonaldson/fb62be1129034c0c77b37429ea10c9d9 to your computer and use it in GitHub Desktop.
Save jdonaldson/fb62be1129034c0c77b37429ea10c9d9 to your computer and use it in GitHub Desktop.
import haxe.ds.Either;
class Main {
static function main() {
trace("HI");
var t : WildMap = ["a" => 1, "b"=> 2];
var f = function(x : Map<Int,String>){
trace(x);
for (k in x.keys()){
trace(k);
}
}
f(t);
}
}
abstract WildMap (Either<Map<String,Int>,Map<Int,String>>) {
public inline function new(e : Either<Map<String,Int>,Map<Int,String>>) this = e;
public var intmap(get,never):Map<Int,String>;
public var stringmap(get,never): Map<String,Int>;
inline function get_intmap() switch this {
case Left(v) : {
var result = new Map<Int,String>();
for (i in v.keys()){
result.set(v.get(i), i);
}
return result;
}
case Right (v) : return v;
}
inline function get_stringmap() : Map<String,Int> switch this {
case Left(v) : return v;
case Right(v) : {
var result = new Map<String,Int>();
for (i in v.keys()){
result.set(v.get(i), i);
}
return result;
}
}
@:from static function fromStringMap( v:Map<Int,String> ):WildMap return new WildMap( Right(v) );
@:from static function fromIntMap( v:Map<String,Int> ):WildMap return new WildMap(Left(v));
@:to public function toStringMap() return get_stringmap();
@:to public function toIntMap() return get_intmap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment